I try to add a Tag system to my Website.
Users can create Tickets and add Tags to them.
To do this I have a HABTM relation between the Models Ticket and Tag.
My problem is now:
When I save a new relation I can have multiple Times the same Ticket id in the tags_tickets table, but as soon as I try to use the tag_id more than once it deletes the old entries for this id.
class Tag extends AppModel {
public $name = 'Tag';
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
'Ticket' => array(
'className' => 'Ticket',
'joinTable' => 'tags_tickets',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'ticket_id',
'unique' => 'keepExisting',
)
);
}
class Ticket extends AppModel {
public $name = 'Ticket';
public $actsAs = array('Containable');
public $hasAndBelongsToMany = array(
'Tag' => array(
'className' => 'Tag',
'joinTable' => 'tags_tickets',
'foreignKey' => 'ticket_id',
'associationForeignKey' => 'tag_id',
'unique' => 'keepExisting',
)
);
}
In the function of the controller I do a saveAll.
Just an example of what i mean:
ticket.id = 1 and tag_id = 1 works just fine.
I can now add for example ticket.id = 1 and tag_id = 2.
But when I add ticket.id = 2 and tag_id = 1 it deletes the entry with ticket.id = 1 and tag_id = 1.
I can't get it to work properly. I don't want to change unique to false because I don't want to have multiple times the same relation in the database. Is there a way to solve this?