0

I have an entity called Item which has a ManyToMany connection with a JoinTable to an entity called Tags

This is working well. The problem is, that I need the tags in the exact order as they are chosen, not sorted by tag id, as I get them now when I call getTags() on the Item class. Is there a way to do that?

Pankaj
  • 37
  • 7
  • You need a third entity joining tags and items, with a `sort_order` property to achieve what you want - I think that's the easiest solution (or you could store the sort_order in the tag itself but then your tag cannot be reused with another item, which I think is not what you want) – tchap Feb 14 '19 at 09:12
  • @tchap I have already join table in same way, but I am not sure in getTags function how to get this order – Pankaj Feb 14 '19 at 09:20
  • I don't think my answer was clear : you need a third **Entity** with its own table; for instance, an entity named **ItemTag** that has a tag, an item, and a sort_order. then you do `$item->getItemTags()` to get the ItemTag with their tag and order – tchap Feb 14 '19 at 09:22
  • @tchap he already has this – Cid Feb 14 '19 at 09:24
  • What you want is not a manytomany-relation but a third entity. in a manytomany-relation you cant have extra fields. See this: https://stackoverflow.com/questions/3542243/doctrine2-best-way-to-handle-many-to-many-with-extra-columns-in-reference-table – Benjamin Kozlowski Feb 14 '19 at 13:14

1 Answers1

1

Use the join table to define that order.

Item_id | Tags_id | Order
--------+---------+------
 1      | 1       | 2
 1      | 2       | 1    <-- order for tags of item 1
 1      | 3       | 3
 -------+---------+------
 2      | 42      | 1
 2      | 1       | 2    <-- order for tags of item 2
 -------+---------+------
 3      | 3       | 1    <-- order for tags of item 3

... And the entities equivalent :

You are going from

//MToM : Many To Many
//OToM : One To Many
//MToO : Many To One

[Item] -MToM-> [Tags]
[Tags] -MToM-> [Item]

To

[Item] -OToM-> [ItemTags] -MToO-> [Tags]
[Tags] -OToM-> [ItemTags] -MToO-> [Item]

And, in your entities classes :

class Item
{
    // some properties ...

    /**
     * @OneToMany(targetEntity="ItemTags", mappedBy="Item")
     */
    private $ItemTags; //array

    //more properties, and get/set
}

class Tags
{
    // some properties ...

    /**
     * @OneToMany(targetEntity="ItemTags", mappedBy="Tags")
     */
    private $ItemTags; //array

    //more properties, and get/set
}

class ItemTags
{
    /**
     * @ManyToOne(targetEntity="Item", inversedBy="ItemTags")
     * @JoinColumn(name="Item_id", referencedColumnName="id")
     *                   ^-----^-------------------------^^---- Make sure to use the same than in your table !
     */
    private $Item; // single Item

    /**
     * @ManyToOne(targetEntity="Tags", inversedBy="ItemTags")
     * @JoinColumn(name="Tags_id", referencedColumnName="id")
     *                   ^-----^-------------------------^^---- Make sure to use the same than in your table !
     */
    private $Tags; // single Tags

    private $Order;

    // get/set and stuff
}
Cid
  • 14,968
  • 4
  • 30
  • 45