0

フラットな php プロジェクトを Symfony2 に移行しようとしていますが、非常に難しくなっています。たとえば、いくつかの仕様を持ち、その Extraspecs DB テーブルの「cat」属性によって区別できる製品仕様のテーブルがあります。したがって、そのテーブルのエンティティを作成し、「cat」= 0 の仕様だけの配列を作成したいと考えています...

コードはこれだと思います..そうですか?

$typeavailable = $this->getDoctrine()
        ->getRepository('LabsCatalogBundle:ProductExtraspecsSpecs')
        ->findBy(array('cat' => '0'));

これを配列に入れて、このようなフォームで動作させるにはどうすればよいですか?:

form = $this ->createFormBuilder($product)
->add('specs', 'choice', array('choices' => $typeavailableArray), 'multiple' => true)  

前もって感謝します :)

#

皆さん、ありがとうございました..

しかし今、私は別の問題に遭遇しました..実際、私は既存のオブジェクトからフォームを構築しています:

$form = $this ->createFormBuilder($product)
                ->add('name', 'text')
                ->add('genspec', 'choice', array('choices' => array('0' => 'None', '1' => 'General', '2' => 'Specific')))
                ->add('isReg', 'choice', array('choices' => array('0' => 'Material', '1' => 'Reagent', '2' => 'Antibody', '3' => 'Growth Factors', '4' => 'Rodents', '5' => 'Lagomorphs')))

その場合、私の現在の値は「extraspecs」という名前なので、次のように追加しました。

->add('extraspecs', 'entity', array(
                        'label'          => 'desc',
                        'empty_value'    => ' --- ',
                        'class'          => 'LabsCatalogBundle:ProductExtraspecsSpecs',
                        'property'       => 'specsid',
                        'query_builder'  => function(EntityRepository $er) {
                            return $er ->createQueryBuilder('e');

しかし、「エクストラスペック」は、すべての製品がいくつかのエクストラスペックを持つ oneToMany の関係から来ています...

ORM は次のとおりです。

Labs\CatalogBundle\Entity\Product:
    type: entity
    table: orders__regmat
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 100
        catnumber:
            type: string
            scale: 100
        brand:
            type: integer
            scale: 10
        company:
            type: integer
            scale: 10
        size:
            type: decimal
            scale: 10
        units:
            type: integer
            scale: 10
        price:
            type: decimal
            scale: 10
        reqcert:
            type: integer
            scale: 1
        isReg:
            type: integer
            scale: 1
        genspec:
            type: integer
            scale: 1

    oneToMany:
        extraspecs:
            targetEntity: ProductExtraspecs
            mappedBy: product

Labs\CatalogBundle\Entity\ProductExtraspecs:
    type: entity
    table: orders__regmat__extraspecs

    fields:
        extraspecid:
            id: true
            type: integer
            unsigned: false
            nullable: false
            generator:
                strategy: IDENTITY
        regmatid:
            type: integer
            scale: 11
        spec:
            type: integer
            scale: 11
        attrib:
            type: string
            length: 20
        value:
            type: string
            length: 200

    lifecycleCallbacks: {  }


    manyToOne:
      product:
        targetEntity: Product
        inversedBy: extraspecs
        joinColumn:
            name: regmatid
            referencedColumnName: id

どうすればいいですか?

ありがとうございました!!!

4

3 に答える 3

4

データベースから返された値は、すでに配列になっています。

エンティティ フィールド タイプを使用して、必要なフォームを作成できます。
これがどのように機能するかです:

$form = $this->createFormBuilder($product)
    ->add('specs', 'entity', array(
        'class' => 'LabsCatalogBundle:ProductExtraspecsSpecs',
        'choices' => $typeavailable,
        'property' => 'specsid',
        'multiple' => true,
    ))->getForm();

propertyフォームに表示するターゲット エンティティ (ProductExtraspecsSpecs) のフィールドの属性を置き換えます。

まだ不明な点がある場合は、質問してください。追加情報を提供しようとします。

現在のオブジェクトを選択するには、次のようにします:
コントローラーで:$selected = $product->getExtraspecs();

フォームビルダーで:

$form = $this->createFormBuilder($product)
    ...
    ->add('specs', 'entity', array(
        'data' => $selected,
        ...
    ))->getForm();
于 2012-11-09T15:58:28.127 に答える
1

formBuilderウィジェットを使用してオブジェクトを直接リクエストできますentity。次の例は、いくつかの基本的な属性を示しています。

  • label明らかです
  • empty_valueも明らかです
  • class選択する完全修飾エンティティーです。
  • propertyリストに表示されるクラスメンバーです (ユーザーに表示されるもの)
  • data教義関係 ( OneToManyなど)がある場合は、現在選択されているものです。
  • query_builder最後に、リストに表示する要素の選択を指定できます(例の場合、「cat is 0」)

まとめると、次のようになります。

$builder ->add('entity', 'entity', array(
            'label'          => 'Choose from this list',
            'empty_value'    => 'This is a blank value on top of the list',
            'class'          => 'VendorNameBundle:Entity',
            'property'       => 'name',
            'data'           => $currentObject->getEntity(),
            'query_builder'  => function(EntityRepository $er) {
                return $er ->createQueryBuilder('e')
                           ->groupBy('e.id')
                           ->orderBy('e.name', 'ASC');
        }
));

詳細については、こちらをご覧ください。

エンティティ フィールド タイプ

于 2012-11-09T15:57:59.427 に答える
0

シリアライザ コンポーネントを使用して Doctrine エンティティを配列に変換できます。

于 2012-11-09T15:56:58.380 に答える