phalcon中save方法的用法和update方法

我们来看看一个save方法用法。

phalcon中的save方法,他是一个集 添加 和 修改 与一身的一个方法。

$this->someModel->save($data);

在上面的这个方法中他会自动识别,在$data 数组中有没有 主键,

if(主键存在){
    执行修改;
}else{
    执行添加
}

但是我们会发现,将$data 中没有的字段,他会清空。

下面由marserphalconCMS中我们可以看到,做了这样的处理

/**
     * 封装phalcon model的update方法,实现仅更新数据变更字段,而非所有字段更新
     * @param array|null $data
     * @param null $whiteList
     * @return bool
     */
    public function iupdate(array $data=null, $whiteList=null){
        if(count($data) > 0){
            $attributes = $this -> getModelsMetaData() -> getAttributes($this);
            $this -> skipAttributesOnUpdate(array_diff($attributes, array_keys($data)));
        }
        return parent::update($data, $whiteList);
    }

其中getModelsMetaData

 /**
     * Returns the models meta-data service related to the entity instance
     *
     * @return \Phalcon\Mvc\Model\MetaDataInterface 
     */
    public function getModelsMetaData() {}

zephir源码

    /**
     * Returns the models meta-data service related to the entity instance
     */
    public function getModelsMetaData() -> <MetaDataInterface>
    {
        var metaData, dependencyInjector;

        let metaData = this->_modelsMetaData;
        if typeof metaData != "object" {

            let dependencyInjector = <DiInterface> this->_dependencyInjector;

            /**
             * Obtain the models-metadata service from the DI
             */
            let metaData = <MetaDataInterface> dependencyInjector->getShared("modelsMetadata");
            if typeof metaData != "object" {
                throw new Exception("The injected service 'modelsMetadata' is not valid");
            }

            /**
             * Update the models-metadata property
             */
            let this->_modelsMetaData = metaData;
        }
        return metaData;
    }

和getAttributes

 /**
     * Returns table attributes names (fields)
     *
     * @param \Phalcon\Mvc\ModelInterface $model 
     * @return array 
     */
    public function getAttributes(\Phalcon\Mvc\ModelInterface $model);

还有skipAttributesOnUpdate

/**
     * Sets a list of attributes that must be skipped from the
     * generated UPDATE statement
     * <code>
     * <?php
     * class Robots extends \Phalcon\Mvc\Model
     * {
     * public function initialize()
     * {
     * $this->skipAttributesOnUpdate(array('modified_in'));
     * }
     * }
     * </code>
     *
     * @param array $attributes 
     */
    protected function skipAttributesOnUpdate(array $attributes) {}

zephir源码

    /**
     * Sets a list of attributes that must be skipped from the
     * generated UPDATE statement
     *
     *<code>
     * <?php
     *
     * class Robots extends \Phalcon\Mvc\Model
     * {
     *     public function initialize()
     *     {
     *         $this->skipAttributesOnUpdate(
     *             [
     *                 "modified_in",
     *             ]
     *         );
     *     }
     * }
     *</code>
     */
    protected function skipAttributesOnUpdate(array! attributes) -> void
    {
        var keysAttributes, attribute;

        let keysAttributes = [];
        for attribute in attributes {
            let keysAttributes[attribute] = null;
        }

        this->getModelsMetaData()->setAutomaticUpdateAttributes(this, keysAttributes);
    }

都是phalcon自带的

用下面方式进行保存

$result = $this -> iupdate($data);

这样就将数据被清空的问题解决了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值