예제 #1
0
 /**
  * extracts tags from the provided text and connects them to this question. If a tag is deleted it
  * will be deleted from the relation
  *
  * @return void
  * @author The Young Shepherd
  **/
 public function save(Doctrine_Connection $conn = null)
 {
     $currentTags = array();
     foreach ($this->Tags as $tag) {
         $currentTags[] = $tag->name;
     }
     $newTags = array_keys(Toolkit::extractTags($this->Best->question . ' ' . $this->Best->answer));
     $deleteTags = array_diff($currentTags, $newTags);
     foreach ($deleteTags as $name) {
         foreach ($this->Tags as $index => $tag) {
             if ($tag->name === $name) {
                 unset($this->Tags[$index]);
                 break;
                 // go to next name to delete
             }
         }
     }
     $addTags = array_diff($newTags, $currentTags);
     foreach ($addTags as $name) {
         if (!($tag = Doctrine::getTable('Tag')->findOneByName($name))) {
             $tag = new Tag();
             $tag->name = $name;
         }
         $this->Tags[] = $tag;
     }
     return parent::save($conn);
 }