3

I couldn't find any similar questions so here goes:

I have a Many-To-Many relationship between my (order)Flow Entity and my Product Entity, but it doesn't select an <option> in the <select>.

All the Entities has been setup by Symfony itself so the @ORM\ManyToMany etc. should be ok.

My controller has the following line:

$form = $this->createForm(new \FDM\BestilBundle\Type\FlowsType(), $flow)->add('submit', 'submit');

The $flow is populated correctly from the db.

The FlowsType file has amoung many fields the following code:

->add('product', 'collection', [
  'type' => new ProductDropdownType(),
  'allow_add' => TRUE,
  'allow_delete' => TRUE,
  'prototype' => TRUE,
  'by_reference' => FALSE,
  ]
)

All fields in the FlowsType are filled out correctly, except the one below.

The ProductDropdownType has the following code:

$builder->add('name', 'entity', [
  'class' => 'FDMBestilBundle:Flows',
  'property' => 'name',
  'by_reference' => false
]);

The number of rows is correct - if I have three rows in my many-to-many sql table it shows three rows. They just aren't selected. If I change the ProductDropdownType to:

$builder->add('name', 'text');

The data is showing just fine - so the db and everyting is working. Except when I want a collection of <select>...

The relations are the following in the Entities:

In the Flow Entity:

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="FDM\BestilBundle\Entity\Product", inversedBy="flow")
 * @ORM\JoinTable(name="productsinflows",
 *   joinColumns={
 *     @ORM\JoinColumn(name="flow", referencedColumnName="flowId")
 *   },
 *   inverseJoinColumns={
 *     @ORM\JoinColumn(name="product", referencedColumnName="productId")
 *   }
 * )
 */
private $product;

In the Product Entity:

/**
 * @var \Doctrine\Common\Collections\Collection
 *
 * @ORM\ManyToMany(targetEntity="FDM\BestilBundle\Entity\Flows", mappedBy="product")
 */
private $flow;

Can someone please help me - I'm stuck!

4
  • You mean your select is populated with 3 options which values are empty? i dont get "They just aren't selected" Commented May 21, 2015 at 8:24
  • I mean that the select tag is correctly filled with "products" data. But the select tag isn't selected. None of the option tags has the "selected" attribute. One of my flow has three product in the Many-To-Many table. The page shows correctly three select. All three select has all the products from the products table. But none of the three has the selected attribute. Commented May 21, 2015 at 8:40
  • Can you update your answer with relations. Commented May 21, 2015 at 9:04
  • looks correct to me, i am not sure about collection if it takes account of relations, i know for sure that if the entity was inside your main type without collection it should be selected but since its a collection and another formtype it may not be supported Commented May 21, 2015 at 10:36

1 Answer 1

2

You can use query builder in your form, this is an exemple : In the query builder, create the SQL query with DQL

->add('espece', 'entity', array(
    'class'    => 'ADMapecheBundle:Espece',
    'property' => 'nom',
    'multiple' => false,
    'query_builder' => function(EspeceRepository $er) use ($user) {
                        return $er->createQueryBuilder('p')
                            ->where("p.user = :user")
                            ->orderBy('p.nom', 'ASC')
                            ->setParameter('user', $user);

I hope to help you, friendly

Sign up to request clarification or add additional context in comments.

3 Comments

Hi Capt. what does your EspeceRepository file look like? And where do you get the $user object/variable from?
i believe this was just a piece of his code isn't adapted to your code, but this is not what you are looking for, you already have your data, query_builder is used if you need special query for the entities, but he want all i guess
The user object is a where close for my query and my EspeceRepository is call to set Type of the query result, it is empty.

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.