views:

242

answers:

2

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 false too. Unfortunately, this will delete any other instances 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????

A: 

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.

PawelMysior
The id you speak of is either the tag_id or the point_id. tag_id is from the tag if it already exists and point_id is the id of the item i'm trying to tag.The problem in my mind is that the points_tags entry is getting over written if an entry is already in there from another item.For example:point 'a' has a tag -- test if I ALSO want tag point 'b' with tag 'test', it will delete the current entry in points_tags for point 'a' with tag 'test' and insert an entry for point 'b' with tag 'test'. Therefore removing any previous associations made with 'test'.Does that clarify it for you?
zmonteca
A: 

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.

Travis Leleu
I'm actually doing that. The issue is that my save is first selecting all my tag_id's from my join table and then subsequently deleting all those. Alternatively, it should be selecting all from my join table based on my tag_id and point_id.
zmonteca