编辑:我需要帮助的是删除 topic_posts 表中主题和帖子之间的所有关系,以便清理关系并删除旧的关系。然后其余代码应该可以正常工作,因为所有问题都将得到解决,因为我们在添加它们之前删除了关系。
在我的 CakePHP 应用程序中,我有 Posts 和 Topics(主题是唯一的并且有一个 id),它们通过处理帖子和主题之间关系的 Topic_Posts 相互链接。
但是,如果用户编辑具有关系的帖子并保存它,而不是仅仅修改关系,它将在 Topic_posts 表中复制它们,并且如果用户从帖子中删除主题,也不会删除它们!
处理这个问题的最佳方法是什么?我听说过关于删除该帖子的所有关系然后重新添加它们的讨论是处理所述场景的最干净和最好的方法,但是我该怎么做呢?
这是处理主题保存的代码(它确实检查主题是否重复)但不检查它们是否是重复的关系,也不会删除关系。
public function savePostTopics($postId, $topics)
{
// Explode the topics by comma, so we have an array to run through
$topics = explode(',', $topics);
// Array for collecting all the data
$collection = array();
foreach($topics as $topic)
{
// Trim it so remove unwanted white spaces in the beginning and the end.
$topic = trim($topic);
// Make it all lowercase for consistency of tag names
$topic = strtolower($topic);
// Check if we already have a topic like this
$controlFind = $this->find(
'first',
array(
'conditions' => array(
'title' => $topic
),
'recursive' => -1
)
);
// No record found
if(!$controlFind)
{
$this->create();
if(
!$this->save(
array(
'title' => $topic
)
)
)
{
// If only one saving fails we stop the whole loop and method.
return false;
}
else
{
$temp = array(
'TopicPost' => array(
'topic_id' => $this->id,
'post_id' => $postId
)
);
}
}
else
{
$temp = array(
'TopicPost' => array(
'topic_id' => $controlFind['Topic']['id'],
'post_id' => $postId
)
);
}
$collection[] = $temp;
}
return $this->TopicPost->saveMany($collection, array('validate' => false));
}
以下是关联:
Post.php
class Post extends AppModel
{
public $name = 'Post';
public $belongsTo = 'User';
public $hasMany = array('Answer');
// Has many topics that belong to topic post join table... jazz
public $hasAndBelongsToMany = array(
'Topic' => array('with' => 'TopicPost')
);
}
Topic.php
class Topic extends AppModel
{
public $hasMany = array(
'TopicPost'
);
}
TopicPost.php
class TopicPost extends AppModel {
public $belongsTo = array(
'Topic', 'Post'
);
}
编辑:我已完成以下操作以使两列彼此唯一:
`id` int(11) unsigned NOT NULL auto_increment,
`topic_id` int(11) NOT NULL,
`post_id` int(11) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `unique_row` (`topic_id`,`post_id`)
但是当我进行更新时,我得到一个 SQL 错误,所以基本上 Cake 没有正确处理这个问题......我该如何解决这个问题,因为它只是通过防止数据重复来部分解决问题!另外,当我希望从 topic_posts 中删除关系但不知道该怎么做时,我该如何处理?
Database Error
Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2-107' for key 2
SQL Query: INSERT INTO `db52704_favorr`.`topic_posts` (`topic_id`, `post_id`) VALUES (2, 107)