0

I'm learning how to use Symfony and I have a logical issue.

Here is what I want to do : I have two entities: post and category. Between them, I have a ManyToMany relationship.

Here, everything works as I expected. I have two forms and a relational table between the entities named: post_category

I'd like to give a range for each post which is in a category. So I thought that I have to insert the third column in my relational table.

But here I'm lost. How can I create this? Should I do it manually and create a custom query in a repository?

I never used a custom query yet, so if someone can give some clues to do it...

Thanks a lot!

Here are my (simplified) entities:

class Category
{

    private $id;

    private $title;

    private $description;

    private $relPosts;

}

class Post
{
    private $id;

    private $title;

    private $content;

    private $slug;

    private $relCategories;

    private $range;

    private $createdAt;
}

My conditional table "post_contenu":

post_id | contenu_id

  • Paster you entities please. I dont really understand you. If you have created 2 entites with their fields, What is you real problem for creating another field in the 3rd table? – AythaNzt Mar 08 '19 at 08:24
  • As you noticed, Symfony (or to be more precise Doctrine) takes care for any intermediate tables in case of **ManyToMany** relations. If you want to extend such an intermediate table with further columns, you have to create an entity class for it and break down the **ManyToMany** relation into two **ManyToOne** relations (from the newly created class). [Here](https://stackoverflow.com/a/54904148/3848833) I've answered a similar question, but the answer should apply in you case too. – cezar Mar 08 '19 at 08:45
  • Thanks I'm going to study your answer. – Clémentine Mar 08 '19 at 08:48
  • 1
    As Cezar said, you need to use `ManyToOne <=> OneToMany` associations between 3 participating classes. Check [Doctrine Association Mapping](https://www.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-bidirectional) documentation for more details. – Preciel Mar 08 '19 at 11:55

0 Answers0