Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony Entities in a collection

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!

like image 546
Lars Hansen Avatar asked Jan 29 '26 16:01

Lars Hansen


1 Answers

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

like image 115
captain_rillette Avatar answered Feb 01 '26 05:02

captain_rillette