Symfony / API Platform – Many To Many relationship only working in one way

i’m working on a project where i need to associate "Articles" and "Addresses" entities to an "FAQ" entity. So i wrote those two relations in my Faq entity

/**
 * @ORMManyToMany(targetEntity=Article::class, inversedBy="faqs")
 * @Groups({"api_backend"})
 * @MaxDepth(1)
 */
private $article;

/**
 * @ORMManyToMany(targetEntity=Adresse::class, inversedBy="faqs")
 * @Groups({"api_backend"})
 * @MaxDepth(1)
 */
private $address;

and add these properties in my Adresse.php

/**
 * @ORMManyToMany(targetEntity=Faq::class, mappedBy="address")
 * @Groups({"api_backend"})
 * @MaxDepth(1)
 */
private $faqs;

and Article.php

/**
 * @ORMManyToMany(targetEntity=Faq::class, mappedBy="article")
 * @Groups({"api_backend"})
 * @MaxDepth(1)
 */
private $faqs;

When i try to add an article to one of my faqs it works great

Postman Request

[2020-10-27 03:31:50] doctrine.DEBUG: "START TRANSACTION" [] []
[2020-10-27 03:31:50] doctrine.DEBUG: UPDATE faq SET enabled = ? WHERE id = ? [true,3] []
[2020-10-27 03:31:50] doctrine.DEBUG: INSERT INTO faq_article (faq_id, article_id) VALUES (?, ?) [3,352] []
[2020-10-27 03:31:50] doctrine.DEBUG: "COMMIT" [] []

But when i try to add an adress, it’s just getting ignored, without any errors

Postman Request

[2020-10-27 03:41:18] doctrine.DEBUG: "START TRANSACTION" [] []
[2020-10-27 03:41:18] doctrine.DEBUG: UPDATE faq SET enabled = ? WHERE id = ? [true,3] []
[2020-10-27 03:41:18] doctrine.DEBUG: "COMMIT" [] []

nothing related to my relationship shows up in the log, and the relation is not updated in the database.

Funny thing, it works great in the other direction, but i can’t use that in my application.

Postman Request

[2020-10-27 03:45:07] doctrine.DEBUG: "START TRANSACTION" [] []
[2020-10-27 03:45:08] doctrine.DEBUG: INSERT INTO faq_adresse (faq_id, adresse_id) VALUES (?, ?) [3,16] []
[2020-10-27 03:45:08] doctrine.DEBUG: "COMMIT" [] []

And if i manually add the relationship in the database, i’m able to see it when i get my Faq entity

enter image description here

Any idea anyone ? i’m really getting stuck here

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *