2013-12-09 23 views
1

我有一个模型,需要更新记录。每次$ count ($ count = $ post-> save())正在为NULL。怎么可能知道这个记录是否保存。如果保存,我想显示以下消息'发布更新',如果不是'发布无法更新'的其他消息。通过Zend框架中的模型更新记录

这总是会去其他端口。我如何知道模型是否正确更新?

  $post = new Application_Model_Post($form->getValues()); 
      $post->setId($id); 
      $count = $post->save(); 
      //var_dump($count); exit; 
      if ($count > 0) { 
       $this->_helper->flashMessenger->addMessage('Post updated'); 
      } else { 
       $this->_helper->flashMessenger->addMessage('Post cannot update'); 
      } 

Application_Model_Post代码是如下,

class Application_Model_Post 
{ 
    /** 
    * @var int 
    */ 
    protected $_id; 

    /** 
    * @var string 
    */ 
    protected $_title; 

    /** 
    * @var string 
    */ 
    protected $_body; 

    /** 
    * @var string 
    */ 
    protected $_created; 

    /** 
    * @var string 
    */ 
    protected $_updated; 

    /** 
    * @var Application_Model_PostMapper 
    */ 
    protected $_mapper; 

    /** 
    * Class Constructor. 
    * 
    * @param array $options 
    * @return void 
    */ 
    public function __construct(array $options = null) 
    { 
     if (is_array($options)) { 
      $this->setOptions($options); 
     } 
    } 

    public function setOptions(array $options) 
    { 
     $methods = get_class_methods($this); 
     foreach ($options as $key=> $value) { 
      $method = 'set'.ucfirst($key); 
      if (in_array($method, $methods)) { 
       $this->$method($value); 
      } 
     } 

     return $this; 
    } 

    public function setId($id) 
    { 
     $this->_id = $id; 
     return $this; 
    } 

    public function getId() 
    { 
     return $this->_id; 
    } 

    public function setTitle($title) 
    { 
     $this->_title = (string) $title; 
     return $this; 
    } 

    public function getTitle() 
    { 
     return $this->_title; 
    } 

    public function setBody($body) 
    { 
     $this->_body = $body; 
     return $this; 
    } 

    public function getBody() 
    { 
     return $this->_body; 
    } 

    public function setCreated($ts) 
    { 
     $this->_created = $ts; 
     return $this; 
    } 

    public function getCreated() 
    { 
     return $this->_created; 
    } 

    /** 
    * Set data mapper. 
    * 
    * @param mixed $mapper 
    * @return Application_Model_Post 
    */ 
    public function setMapper($mapper) 
    { 
     $this->_mapper = $mapper; 
     return $this; 
    } 

    /** 
    * Get data mapper. 
    * 
    * Lazy loads Application_Model_PostMapper instance if no mapper 
    * registered. 
    * 
    * @return Application_Model_PostMapper 
    */ 
    public function getMapper() 
    { 
     if (null === $this->_mapper) { 
      $this->setMapper(new Application_Model_PostMapper()); 
     } 
     return $this->_mapper; 
    } 

    /** 
    * Save the current post. 
    * 
    * @return void 
    */ 
    public function save() 
    { 
     $this->getMapper()->save($this); 
    } 

    public function getPost($id) 
    { 
     return $this->getMapper()->getPost($id); 
    } 

    /** 
    * Update the current post. 
    * 
    * @return void 
    */ 
    public function update($data, $where) 
    { 
     $this->getMapper()->update($data, $where); 
    } 

    /** 
    * Find a post. 
    * 
    * Resets entry state if matching id found. 
    * 
    * @param int $id 
    * @return Application_Model_Post 
    */ 
    public function find($id) 
    { 
     $this->getMapper()->find($id, $this); 
     return $this; 
    } 

    /** 
    * Fetch all posts. 
    * 
    * @return array 
    */ 
    public function fetchAll() 
    { 
     return $this->getMapper()->fetchAll(); 
    } 
} 

getMapper指类Application_Model_PostMapper。

class Application_Model_DbTable_Post extends Zend_Db_Table_Abstract 
{ 
    protected $_name = 'posts'; 
} 

Application_Model_DbTable_Post

class Application_Model_PostMapper 
{ 
public function save(Application_Model_Post $post) 
{ 
    $data = array(
     'title'=>$post->getTitle(), 
     'body'=>$post->getBody(), 
     'created'=>$post->getCreated() 
    ); 

    if (null === ($id = $post->getId())) { 
     unset($data['id']); 
     $data['created'] = date('Y-m-d H:i:s'); 
     $post->setId($this->getDbTable()->insert($data)); 
    } else { 
     $this->getDbTable()->update($data, array('id = ?'=>$id)); 
    } 
} 

public function getDbTable() 
{ 
    if (null === $this->_dbTable) { 
     $this->setDbTable('Application_Model_DbTable_Post'); 
    } 

    return $this->_dbTable; 
} 
} 

类让我知道,如果有什么不正确。我是zend的新手,并在提到zend网站时做了这个。 http://framework.zend.com/manual/1.12/en/learning.quickstart.create-model.html

+1

什么Application_Model_Post的代码? – Snowwolf

+1

这真的取决于你如何实现模型的'save()'方法。 save()方法返回什么? – burntblark

+0

@Snowwolf请检查更新后的问题。我包含了Application_Model_Post的代码。如果我缺少任何最佳实践,请告诉我。 – dev1234

回答

2

你可以像这样扩展你的脚本。任何插入或更新期间,zend dbtable都会触发Zend_Db_Exception错误。

class Application_Model_PostMapper 
{ 
    public function save(Application_Model_Post $post) 
    { 
     $data = array(
      'title'=>$post->getTitle(), 
      'body'=>$post->getBody(), 
      'created'=>$post->getCreated() 

     ); 

     try { 

      if (null === ($id = $post->getId())) { 
       unset($data['id']); 
       $data['created'] = date('Y-m-d H:i:s'); 
       $post->setId($this->getDbTable()->insert($data)); 
      } else { 
       $this->getDbTable()->update($data, array('id = ?'=>$id)); 
      } 
     } catch (Zend_Db_Exception $e) { 
      // error thrown by dbtable class 
      return $e->getMessage(); 
     } 

     // no error 
     return true; 
    } 
} 

现在你可以检查这样

$post = new Application_Model_Post($form->getValues()); 
$post->setId($id); 

$isSaved = $post->save(); 

if ($isSaved === true) { 
    $this->_helper->flashMessenger->addMessage('Post updated'); 
} else { 
    // error 
    // $isSaved holds the error message 
    $this->_helper->flashMessenger->addMessage('Post cannot update'); 
} 
+0

tnx。这是检查保存与否的最佳方法吗? – dev1234

+1

这取决于你想要解决的任务。如果您只想检查更新或插入查询是否已成功发送并执行 - 是的。 – ins0

+0

是否可以更新上述答案,以便在保存/更新失败时显示错误? (它可能因为不为空,唯一约束,无效数据而失败) – dev1234