Обновить запись через модель в zend framework

У меня есть модель и вам нужно будет обновить запись. каждый раз значение $ count ($ count = $ post-> save ()) равно NULL . как можно узнать, сохранена ли эта запись или нет. если сохранено, я хочу отобразить следующее сообщение «Сообщение обновлено», а если не другое сообщение «Сообщение не может обновиться».

Это всегда происходит в другом порту. как я могу знать, что модель обновлена ​​правильно или нет?

$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_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('Ymd 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; } } не 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('Ymd 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; } } 

Класс приложения_Model_DbTable_Post

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

Дайте мне знать, если что-то неверно. Я новичок в zend и сделал thsi, обращаясь к сайту zend. http://framework.zend.com/manual/1.12/en/learning.quickstart.create-model.html

вы можете расширить свой сценарий таким образом. 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('Ymd 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; } } не 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('Ymd 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'); }