0

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!!!

3 Answers 3

4

The values returned from the database are already in an array.

You can use the entity field type to create the form you want.
This is how it works:

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

Replace the property attribute for a field in the target entity (ProductExtraspecsSpecs) that you want to be displayed in the form.

If something is still unclear to you just ask and I will try to supply additional information.

To have the current objects selected do this:
In the controller: $selected = $product->getExtraspecs();

In the form builder:

$form = $this->createFormBuilder($product)
    ...
    ->add('specs', 'entity', array(
        'data' => $selected,
        ...
    ))->getForm();
Sign up to request clarification or add additional context in comments.

5 Comments

Very good :) I'm still not getting it to work.. but I think is because of the relationship model.. I will try to solve that... thank you!!!!!
I don't get errors.. it just simply don't select the current.. maybe the relations are not set properly and therefore the getExtraspecs() is not selecting properly.. I've just made a print_r($selected) and it shows and enormous amount of data..
Are you positive that you have 'multiple' => true in your specs field options like noted in my post? Because this i required to select multiple.
Hello MatsRietdijk... i'm still working around this problem and found out that probably its the comparison between the "avaialbel" and the "seleted" that its not been made correctly... Can you give me some advice about the sturcture of each other? thank you :)
I just commented on your new question regarding that problem.
1

You can request objects directly in the formBuilder using the widget entity. The following example shows some of the basic attributes, where:

  • label is obvious
  • empty_value is obvious too
  • class is the fully qualified entity that you want to choose from
  • property is the class member that is displayed in the list (what the user sees)
  • data is the currently selected one, if you have a doctrine relationship (like OneToMany)
  • query_builder finally lets you specify the selection which elements should be displayed in the list (in your example case "where cat is 0")

All together you get this:

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

You can read more about it here:

Entity Field Type

3 Comments

What is the additional problem you got?
I've edited my question.. is about the current value.. Can you please check the question.. thank you very much :)
I only know how to do this for the form as it shows in my answer so i added it there. Note that i use a input field where you can select multiple objects because there is a Many to One relation.
0

You can use the Serializer Component to turn your Doctrine entities into an array.

1 Comment

The problem with the Serializer and using it on ORM entities is that it also serializes all relationships. E.g. a Menu has a few hundred Options mapped through ORM, passing a Menu entity through a serializer will also serialize all the Options, even when they're not needed, resulting in excessive memory usage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.