【问题标题】:Symfony2 Entity to arraySymfony2 实体到数组
【发布时间】:2012-11-09 14:54:24
【问题描述】:

我正在尝试将我的平面 php 项目迁移到 Symfony2,但它变得非常困难。 例如,我有一个 Products 规范表,它有几个规范,并且可以通过 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');

但是“extraspecs”来自 oneToMany 的关系,其中每个产品都有几个 extraspecs...

这是 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

我该怎么做?

谢谢!!!

【问题讨论】:

    标签: arrays forms symfony entity


    【解决方案1】:

    从数据库返回的值已经在一个数组中。

    您可以使用entity field type 创建您想要的表单。
    它是这样工作的:

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

    替换目标实体 (ProductExtraspecsSpecs) 中要在表单中显示的字段的 property 属性。

    如果您仍有不清楚的地方,请尽管询问,我会尽力提供更多信息。

    要选择当前对象,请执行以下操作:
    在控制器中:$selected = $product->getExtraspecs();

    在表单构建器中:

    $form = $this->createFormBuilder($product)
        ...
        ->add('specs', 'entity', array(
            'data' => $selected,
            ...
        ))->getForm();
    

    【讨论】:

    • 非常好 :) 我仍然无法正常工作.. 但我认为是因为关系模型.. 我会尝试解决这个问题...谢谢!!!!!!
    • 我没有得到错误..它只是不选择当前..可能关系设置不正确,因此 getExtraspecs() 没有正确选择..我刚刚做了一个 print_r($selected) ,它显示了大量的数据..
    • 您确定您的规格字段选项中有'multiple' => true,就像我的帖子中提到的那样?因为这个我需要选择多个。
    • Hello MatsRietdijk...我仍在解决这个问题,发现可能是“avaialbel”和“seleted”之间的比较,它没有正确制作...你能给我一些关于彼此结构的建议?谢谢你:)
    • 我刚刚评论了您关于该问题的新问题。
    【解决方案2】:

    您可以使用小部件entity 直接在formBuilder 中请求对象。以下示例显示了一些基本属性,其中:

    • label 很明显
    • empty_value也很明显
    • class 是您要选择的完全限定实体
    • property 是列表中显示的类成员(用户看到的)
    • data 是当前选中的,如果你有教义关系(如OneToMany
    • query_builder 最后让您指定选择哪些元素应显示在列表中(在您的示例中为“cat 为 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');
            }
    ));
    

    您可以在此处阅读更多信息:

    Entity Field Type

    【讨论】:

    • 您遇到的其他问题是什么?
    • 我已经编辑了我的问题..是关于当前值..你能检查一下这个问题吗..非常感谢:)
    • 我只知道如何为表单执行此操作,因为它在我的答案中显示,所以我在那里添加了它。请注意,我使用一个输入字段,您可以在其中选择多个对象,因为存在多对一关系。
    【解决方案3】:

    您可以使用Serializer Component 将您的 Doctrine 实体转换为数组。

    【讨论】:

    • Serializer 以及在 ORM 实体上使用它的问题在于它还会序列化所有关系。例如。一个菜单有几百个通过 ORM 映射的选项,通过序列化程序传递一个菜单实体也会序列化所有选项,即使它们不需要,导致内存使用过多
    猜你喜欢
    • 2018-02-20
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 2013-12-24
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    相关资源
    最近更新 更多