CakePHP - HABTM - 将多个标签添加到多个点
我正在尝试用多个标签“标记”多个“点”。我已成功标记我的单点。不幸的是,当我尝试使用标签(例如在另一点上使用“test2”)作为标签时,如果我将“unique”设置为 false 或者将“unique”设置为 true,则会出现重复输入错误,它将删除“test2”所有其他点的标签并创建一个新标签。
这是我的帖子数据:
Array
(
[Tag] => Array
(
[id] => 4b7af6d7-787c-4f10-aa49-2502c0a80001
[name] => Test2
)
[Point] => Array
(
[id] => 4b47c66f-a130-4d12-8ccd-60824051e4b0
)
)
在我的标签模型中,我有这个:
public $hasAndBelongsToMany = array(
'Point' => array(
'className' => 'Point',
'joinTable' => 'points_tags',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'point_id',
'unique' => false)
);
我也尝试过将“unique”设置为 true。不幸的是,这将删除连接表(“points_tags”)中“Test2”的任何其他实例。
我已经尝试使用 save() 和 saveAll() 进行此操作。两者都给我这个错误:
警告(512):SQL错误:1062:键“MAN_ADD”的重复条目“4b7af6d7-787c-4f10-aa49-2502c0a80001-4b47c66f-a130-4d12-8ccd-608”[CORE/cake/ libs/model/datasources/dbo_source.php,第 527 行] 查询: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')
想法???
建议????
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 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????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
id
从哪里来?我猜测它是表的主键,根据我从您的帖子中了解到的内容(请写得更清楚,帮助我们帮助您),问题不在于点或标签,而在于 point_tags 表中的 id。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.当您使用 save 方法时,您是否在循环内执行它?请记住,最佳实践是在循环中保存时调用 model::create()。
我经常发现,当我遇到 HABTM 保存行为问题时,这是因为我没有调用 model::create。
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.