如何在CakePHP中手动更新jointable
我对CakePHP有点陌生。 我有两个表:contacts和tags,以及一个HABTM表contacts\u tags。用户应该能够像人们在delicious.com中所做的那样添加标签。他们在键入时会得到一个标签列表。他们还可以通过键入新标记来添加新标记。我想查看标签表,看看哪些标签已经存在,哪些没有。新的标记应添加到表中,插入ID将附加到数组中。然后我想使用这些标记ID来更新contacts\u tags表。就这样。以下是我创建的两个函数:如何在CakePHP中手动更新jointable,php,database,cakephp,Php,Database,Cakephp,我对CakePHP有点陌生。 我有两个表:contacts和tags,以及一个HABTM表contacts\u tags。用户应该能够像人们在delicious.com中所做的那样添加标签。他们在键入时会得到一个标签列表。他们还可以通过键入新标记来添加新标记。我想查看标签表,看看哪些标签已经存在,哪些没有。新的标记应添加到表中,插入ID将附加到数组中。然后我想使用这些标记ID来更新contacts\u tags表。就这样。以下是我创建的两个函数: /** this function check
/**
this function checks for existing tags and creates new tags
@params string (of comma separated tags)
@return array (of IDs)
**/
function saveAndCreateTags($tags){
$tags = explode(",", $tags); //create array of tags
$idArray = array();
foreach($tags as $tag) {
$count = $this->find('count', array('conditions' => array('name' => $tag)));
if($count === 0) {
$this->create();
if($this->save($tag)) {
$idArray[] = $this->getInsertID();
}
}
else {
$idArray[] = $this->getID();
}
}
return $idArray;
}
/**
this function updates the relational table
@params array (this array is returned by saveAndCreateTags function
@params int (id of the contact)
**/
function updateContactTagsTable($idArray, $contactId){
foreach($idArray as $tagId) {
$count = $this->ContactTag->hasAny(array('tag_id' => $tagId, 'contact_id' => $contactId));
if($count === 0) {
();
$this->ContactTag->save(array('contact_id' => $contactId, 'tag_id' => $tagId));
}
}
}
$this->ContactTag->hasAny,$this->ContactTag->create,$this->ContactTag->save都不起作用。。。我遗漏了什么吗?请查看teknoid在上发表的这篇文章。对不起,我无法确切地了解问题所在。如果您试图保存相关的模型数据,您可能希望研究使用model::saveAll方法,而不是model::save。谢谢!这正是我想要的…: