我是一個CakePHP的新手。 我有兩個表格:聯繫人和標籤,以及一個HABTM表contacts_tags。用戶應該可以添加標籤,就像人們可以在delicious.com中一樣。他們在輸入標籤時會得到一個列表。他們還可以通過輸入新標籤來添加新標籤。我想檢查標籤表,看看哪些標籤已經存在,哪些不是。應該將新的標籤添加到表中,並將插入標識附加到數組中。然後,我想使用這些標記ID來更新contacts_tags表。而已。這是兩個功能我已經創建:如何在CakePHP中手動更新連接表
/**
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));
}
}
}
既不的$這個 - > ContactTag-> hasAny,$這個 - > ContactTag->創建,$這個 - > ContactTag->保存工作......上午我錯過了什麼?
對不起,不能完全得到的問題就是一個手柄,準確。如果您嘗試保存相關模型數據,則可能需要使用Model :: saveAll()方法,而不是Model :: save()。 – 2009-10-20 17:49:30