Update a record through model in zend framework

2019-06-07 19:25发布

问题:

I am having a model and would need to update the record. every time $count ($count = $post->save()) is being NULL. how is it possible to know whether this record saved or not. if saved, i want to display the following message 'Post updated' and if not the other message 'Post cannot update'.

This is always going to the else port. how can i know model updated correctly or not?

            $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 code is as below,

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 refers to the class 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('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;
}
}

Class of Application_Model_DbTable_Post

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

Let me know if anything is incorrect. i am a newbie to zend and did thsi while referring the zend site. http://framework.zend.com/manual/1.12/en/learning.quickstart.create-model.html

回答1:

you can extend your script like this. zend dbtable triggers the Zend_Db_Exception on any error during any insert or update.

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;
    }
}

now you can check like this

$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');
}