Ejemplo n.º 1
0
 public function createTopic($data)
 {
     $textData = isset($data['text']) ? $data['text'] : array();
     $tagData = isset($data['tags']) ? $data['tags'] : array();
     //        $categoryData = isset($data['categories']) ? $data['categories'] : array();
     if ($textData) {
         unset($data['text']);
         $text = new TopicTexts();
         $text->assign($textData);
         $this->text = $text;
     }
     $tags = array();
     if ($tagData) {
         unset($data['tags']);
         $tagArray = is_array($tagData) ? $tagData : explode(',', $tagData);
         foreach ($tagArray as $tagName) {
             $tag = Tags::findFirst(array("conditions" => "tagName = :tagName:", "bind" => array('tagName' => $tagName)));
             if (!$tag) {
                 $tag = new Tags();
                 $tag->tagName = $tagName;
             }
             $tags[] = $tag;
         }
         if ($tags) {
             $this->tags = $tags;
         }
     }
     $this->assign($data);
     $this->createdAt = $this->updatedAt = time();
     if (!$this->save()) {
         throw new Exception\RuntimeException('Create topic failed');
     }
     return $this;
 }
Ejemplo n.º 2
0
 public function updatePost($data)
 {
     $textData = isset($data['text']) ? $data['text'] : array();
     $tagData = isset($data['tags']) ? $data['tags'] : array();
     $categoryData = isset($data['categories']) ? $data['categories'] : array();
     $connectionData = isset($data['connectids']) ? $data['connectids'] : array();
     if ($textData) {
         unset($data['text']);
         $text = new Text();
         $text->assign($textData);
         $this->text = $text;
     }
     $tags = array();
     //remove old relations
     if ($this->tagsPosts) {
         $this->tagsPosts->delete();
     }
     if ($tagData) {
         unset($data['tags']);
         $tagArray = is_array($tagData) ? $tagData : explode(',', $tagData);
         foreach ($tagArray as $tagName) {
             $tag = Entities\Tags::findFirst(array("conditions" => "tagName = :tagName:", "bind" => array('tagName' => $tagName)));
             if (!$tag) {
                 $tag = new Entities\Tags();
                 $tag->tagName = $tagName;
             }
             $tags[] = $tag;
         }
         if ($tags) {
             $this->tags = $tags;
         }
     }
     //remove old relations
     $categories = array();
     if ($this->categoriesPosts) {
         $this->categoriesPosts->delete();
     }
     if ($categoryData) {
         unset($data['categories']);
         foreach ($categoryData as $categoryId) {
             $category = Category::findFirst($categoryId);
             if ($category) {
                 $categories[] = $category;
             }
         }
         $this->categories = $categories;
     }
     $connections = array();
     //remove old relations
     if ($this->postConnects) {
         $this->postConnects->delete();
     }
     if ($connectionData) {
         unset($data['connectids']);
         foreach ($connectionData as $connectionId) {
             $connection = new Entities\Connections();
             $connection->sourceId = $this->id;
             $connection->targetId = $connectionId;
             $connection->createdAt = time();
             $connections[] = $connection;
         }
         $this->postConnects = $connections;
     }
     $this->assign($data);
     if (!$this->save()) {
         throw new Exception\RuntimeException('Update post failed');
     }
     return $this;
 }