public function save(Post $post)
 {
     $sql = new \Zend\Db\Sql\Sql($this->adapter);
     $insert = $sql->insert()->values(array('title' => $post->getTitle(), 'slug' => $post->getSlug(), 'content' => $post->getContent(), 'category_id' => $post->getCategory()->getId(), 'created' => time()))->into('news');
     $statement = $sql->prepareStatementForSqlObject($insert);
     $statement->execute();
 }
示例#2
0
 /**
  * @return bool
  */
 public function create()
 {
     $id = $this->getIdentifier();
     $db = $this->createZendDb();
     $sql = new \Zend\Db\Sql\Sql($db);
     $query = $sql->insert()->into(self::getTable())->values($this->fields);
     try {
         $stmt = $sql->prepareStatementForSqlObject($query);
         $result = $stmt->execute();
         $inserted_id = $result->getGeneratedValue();
         $this->id = $inserted_id;
         if (isset(static::$id_method)) {
             $this->{static::$id_method}($inserted_id);
         }
         foreach (static::$relations as $relation) {
             if (isset($relation[2]) && $relation[2]) {
                 $model = new $relation[0]();
                 $model->{$relation}[1]($inserted_id);
                 $model->create();
             }
         }
     } catch (\Exception $e) {
         $this->exception = $e;
         return false;
     }
     return true;
 }