2

symfony2とdoctrine2を使用したフォームの継承に問題があります。これが事です:

users名前やメールアドレスなどのフィールドがいくつかあるだけで、特別なものは何もありません。

Ferncoder\Devis\UserBundle\Entity\users:
  type: entity
  table: null
  fields:
    id:
      type: integer
      id: true
      unsigned: true
      nullable: false
      generator:
        strategy: AUTO
    name:
      type: string
      length: '45'
      nullable: false

このテーブルにはエンティティcraftsMansとの関係OneToOneがあるusersエンティティがあります。

Ferncoder\Devis\UserBundle\Entity\craftsMan:
  type: entity
  oneToOne:
    user:
      targetEntity: users
      joinColumn:
        name: user_id
        referencedColumnName: id
  table: null
  fields:
    id:
      type: integer
      id: true
      unsigned: true
      nullable: false
      generator:
        strategy: AUTO

usersType2つのフォームクラスとを生成しましcraftsmanTypeた。私のcraftsManを作成するために、私は次のようにフォームを作成しました:

class craftsManType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('user', new usersType())
            ->add('company', 'text')
            ->add('fieldOfActivities', 'text')
            ->add('legalForm', 'text')
            ->add('siret', 'text')
            ->add('website', 'text')
            ->add('description', 'textarea')
            ->add('services', 'textarea')
            ->add('employee', 'text')
            ->add('phone', 'text')
        ;
    }

フィールドの値を取得するまで、フォームはかっこよく見えます。問題は、私の'user'フィールドがエンティティではなく配列として返されることです。私は何かを逃しましたか?フィールドからユーザーオブジェクトを生成するにはどうすればよいですかuser。オブジェクトを作成することは、すべての属性を設定することが最善の方法であるとは思えません。

たぶん私は間違っています。

ありがとう、

4

2 に答える 2

1

配列ではなくオブジェクトを返すようにdata_classオプションを設定する必要があります。以下のドキュメントでは、フォームが別のフォームに埋め込まれている場合は、data_classを使用する必要があると説明しています。

ドキュメント:http ://symfony.com/doc/current/book/forms.html#book-forms-data-class

于 2013-02-14T15:05:44.887 に答える
0

コントローラ内:

$form->getData()->getUser();
于 2012-11-28T00:55:56.173 に答える