开发者

CakePHP - HABTM - adding multiple tags to multiple points

I am trying to 'tag' multiple 'points' with multiple tags. I'm tagging my single points successfully. Unfortunately, when i try and use a tag, such as 'test2' on another point as a tag it is either giving me a duplicate entry error if i have my 'unique' set to false or if 'unique' is set to true, it will del my tag for all other points for 'test2' and create a single new one.

Here is what i have for my post data:

Array
(
    [Tag] => Array
        (
            [id] => 4b7af6d7-787c-4f10-aa49-2502c0a80001
            [name] => Test2
        )

    [Point] => Array
        (
            [id] => 4b47c66f-a130-4d12-8ccd-60824051e4b0
        )

)

In my tag model i have this:

public $hasAndBelongsToMany = array(

'Point' => array(
        'className' => 'Point',
        'joinTable' => 'points_tags',
        'foreignKey' => 'tag_id',
        'associationForeignKey' => 'point_id',
        'unique' => false)
);

I have tried this with 'unique' set as true, too. Unfortunately, this will delete any other instan开发者_开发技巧ces of 'Test2' in the join table ('points_tags').

I have tried this using both save() and saveAll(). Both are giving me this error:

Warning (512): SQL Error: 1062: Duplicate entry '4b7af6d7-787c-4f10-aa49-2502c0a80001-4b47c66f-a130-4d12-8ccd-608' for key 'MAN_ADD' [CORE/cake/libs/model/datasources/dbo_source.php, line 527] Query: INSERT INTO points_tags (tag_id,point_id,id) VALUES ('4b7af6d7-787c-4f10-aa49-2502c0a80001','4b47c66f-a130-4d12-8ccd-60824051e4b0','4b7b39f3-46f8-4744-ac53-3973c0a80001')

Thoughts????

Suggestions????


Where does the id come from? I'm guessing its a primary key of the table, and from what I understand from your post (please write more clearly, help us help you) the problem isn't with points or tags, but with the id in the points_tags table.


When you use the save method, are you doing it inside of a loop? Remember, best practice is to call model::create() whenever you're saving in a loop.

I frequently find that when I have issues with the HABTM saving behavior, it's because I didn't call model::create.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜