0

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?

4

2 回答 2

0

你应该花点时间阅读文档

具体看一下可用的配置选项,比如unique

于 2013-06-26T09:56:47.060 回答
0

我用解决方法解决了这个问题:

我为 tags_tickets 表(TagTicket)创建了一个单独的模型,并删除了 Ticket 和 Tag 模型中的 HABTM 关联。

为此,我为 Ticket 和 Tag 模型添加了与 TagTicket 模型的 hasMany 关系,并且该模型与 Ticket 和 Tag 建立了 belongsTo 关系。

现在我可以根据需要保存和删除,并且可以在保存前检查重复项。

于 2013-06-27T07:34:17.990 回答