通过 Zend 框架中的模型更新记录 [英] Update a record through model in zend framework

查看:22
本文介绍了通过 Zend 框架中的模型更新记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型,需要更新记录.每次 $count ($count = $post->save()) 都是 NULL.怎么可能知道这个记录是否保存.如果保存,我想显示以下消息发布更新",如果不是,则显示其他消息发布无法更新".

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'.

这总是去 else 端口.我怎么知道模型更新是否正确?

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 代码如下,

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 指的是 Application_Model_PostMapper 类.

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

Application_Model_DbTable_Post 的类

Class of Application_Model_DbTable_Post

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

如果有任何不正确的地方,请告诉我.我是 Zend 的新手,并在参考 Zend 网站时完成了这项工作.http://framework.zend.com/manual/1.12/en/learning.quickstart.create-model.html

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

推荐答案

你可以像这样扩展你的脚本.zend dbtable 在任何插入或更新期间的任何错误时触发 Zend_Db_Exception.

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

现在你可以这样检查

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

这篇关于通过 Zend 框架中的模型更新记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆