Note that there are some explanatory texts on larger screens.

plurals
  1. PODoctrine2 update many-to-many relations
    primarykey
    data
    text
    <p>I hava relations Many-to-Many with Product entity and Feature entity Product entity:</p> <pre><code>/** * @ORM\ManyToMany(targetEntity="Feature") * @ORM\JoinTable(name="Product_Feature", * joinColumns={@ORM\JoinColumn(name="Product_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="Feature_id", referencedColumnName="id")} * ) */ private $features; </code></pre> <p>Feature entity:</p> <pre><code>/** * @ORM\ManyToMany(targetEntity="Product", mappedBy="features") * @ORM\OrderBy({"position" = "ASC"}) */ private $products; </code></pre> <p>ProductRepository.php:</p> <pre><code>public function updateFeatures($id, $featuresIds) { return $this-&gt;getEntityManager()-&gt;createQueryBuilder() -&gt;update('TestCatalogBundle:Product', 'p') -&gt;set('p.features', ':features') -&gt;where('p.id = :id') -&gt;setParameter('features', $featuresIds) -&gt;setParameter('id', $id) -&gt;getQuery() -&gt;getResult(); } </code></pre> <p>But when I call updateFeatures I get error:</p> <blockquote> <p>features = :features&#039;: Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected</p> </blockquote> <p>How can I update Product_Feature table? Also I can't delete all features from Product_Feature by product's id.<br> I changed my controller in next way:</p> <pre><code> $em = $this-&gt;getDoctrine()-&gt;getEntityManager(); $features = $em-&gt;getRepository('TestCatalogBundle:Feature')-&gt;findBy(array('id' =&gt; $featureIds)); $product = $em-&gt;getRepository('TestCatalogBundle:Product')-&gt;find($id); $product-&gt;getFeatures()-&gt;clear(); foreach ($features as $feature) { $product-&gt;addFeature($feature); } $em-&gt;persist($product); $em-&gt;flush(); </code></pre> <p>But if I use native sql I need 2 queries for deleting features and insert new features. But here I need 2 select queries. Maybe I made this task wrong?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload