I am sort of a newbie with CakePHP. I have two tables: contacts and tags, and an HABTM table contacts_tags. User should be able to add tags as people can do in delicious.com. they get a list of tags as they type. They can also add new tags just by typing them. I want to check the tags table to see which tags are already present and which are not. The tags which are new should be added to the table and the insert ids are appended to an array. Then I want to use these tag ids to update the contacts_tags table. That's it. These are the two functions I have created:
/**
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));
}
}
}
Neither of $this->ContactTag->hasAny, $this->ContactTag->create, $this->ContactTag->save is working... Am I missing something?