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

咱們來看看一個save方法用法。php

phalcon中的save方法,他是一個集 添加 和 修改 與一身的一個方法。git

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

在上面的這個方法中他會自動識別,在$data 數組中有沒有 主鍵,github

if(主鍵存在){
    執行修改;
}else{
    執行添加
}

可是咱們會發現,將$data 中沒有的字段,他會清空。數組

下面由marserphalconCMS中咱們能夠看到,作了這樣的處理app

/**
     * 封裝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);
    }

其中getModelsMetaDatathis

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

zephir源碼code

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

和getAttributesip

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

還有skipAttributesOnUpdateget

/**
     * 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);

這樣就將數據被清空的問題解決了。

相關文章
相關標籤/搜索