Symfony2 Entity to array

Posted by Adriano Pedro on Stack Overflow See other posts from Stack Overflow or by Adriano Pedro
Published on 2012-11-09T14:54:24Z Indexed on 2012/11/09 17:00 UTC
Read the original article Hit count: 283

Filed under:
|
|
|

I'm trying to migrate my flat php project to Symfony2, but its coming to be very hard. For instance, I have a table of Products specification that have several specifications and are distinguishables by its "cat" attribute in that Extraspecs DB table. Therefore I've created a Entity for that table and want to make an array of just the specifications with "cat" = 0...

I supose the code is this one.. right?

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

Now how can i put this in an array to work with a form like this?:

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

Thank you in advance :)

#

Thank you all..

But now I've came across with another problem.. In fact i'm building a form from an existing object:

$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')))

So.. in that case my current value is named "extraspecs", so i've added this like:

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

But "extraspecs" come from a relationship of oneToMany where every product has several extraspecs...

Here is the 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

HOw should I do this?

Thank you!!!

© Stack Overflow or respective owner

Related posts about arrays

Related posts about forms