Hy guys, i need some advice for the following.
I have three tables: daytrip, daytrip_tag and tag. When saving (edit or add) a daytrip i need to save the new tags and the relation between the daytrip and tag.
What is the best approach…
i was thinking about an afterSave in the Daytrip model. But this will go wrong for tag that already excist for a daytrip… (Unique constraint violation)
protected function afterSave() {
foreach ($this->tags as $tag) {
$find = Tag::model()->find('tag=:tag', array(':tag' => $tag));
if (!$find) {
$row = new Tag;
$row->name = $tag;
$row->save();
//insert using $this->id_daytrip and $row->id_tag
} else { //tag already excist
//insert using $this->id_daytrip and $find->id_tag
//goes wrong for tags that already excist for daytrip
}
}
}