牛骨文教育服务平台(让学习变的简单)
博文笔记

Yii CActiveRecord 逻辑删除封装

创建时间:2014-03-06 投稿人: 浏览次数:190

<?php
class BaseModel extends CActiveRecord
{
    /**
     * Set id,cretate_time,update_time value before save this model
     */
    protected function beforeSave()
    {
        if($this->isNewRecord)
        {
            if($this->hasAttribute("id"))
            {
                $this->id = StringUtil::uuid();
            }

            if($this->hasAttribute("creator") && empty($this->creator))
            {
                $this->creator = Yii::app()->user->id;
            }

            if($this->hasAttribute("create_time"))
            {
                $this->create_time = new CDbExpression("NOW()");
            }
        }

        if($this->hasAttribute("updated_time"))
        {
            $this->updated_time = new CDbExpression("NOW()");
        }

        return parent::beforeSave();
    }


    /**
     * check the value is true or false
     * @param $value The need checked value
     */
    public function checkTruthy($value)
    {
        $truthy = (bool)$value;
        if("string" == gettype($value))
        {
            if ("true" != $value && "1" != $value)
            {
                $truthy = false;
            }
        }
        return $truthy;
    }

    /**
     * logic delete, set is delete cloum true if has
     * @see CActiveRecord::deleteByPK()
     */
    public function deleteByPK($pk,$condition="",$params=array())
    {
        Yii::trace(get_class($this).".deleteByPk()","system.model.CActiveRecord");

        if($this->hasAttribute("is_delete"))
        {
            return $this->updateByPk($pk, array("is_delete" => Constant::DELETED), $condition, $params);
        }
        else
        {
            return parent::deleteByPK($pk, $condition, $params);
        }
    }

    /**
     * logic delete, set is delete cloum true if has
     * @see CActiveRecord::deleteAll()
     */
    public function deleteAll($condition="",$params=array())
    {
        Yii::trace(get_class($this).".deleteAll()","system.model.BaseModel");

        if($this->hasAttribute("is_delete"))
        {
            return $this->updateAll(array("is_delete" => Constant::DELETED), $condition, $params);
        }
        else
        {
            return parent::deleteAll($condition, $params);
        }
    }

    /**
     * logic delete, set is delete cloum true if has
     * @see CActiveRecord::deleteAllByAttributes()
     */
    public function deleteAllByAttributes($attributes,$condition="",$params=array())
    {
        Yii::trace(get_class($this).".deleteAllByAttributes()","system.model.BaseModel");

        if($this->hasAttribute("is_delete"))
        {
            $builder=$this->getCommandBuilder();
            $criteria=$builder->createCriteria($condition,$params);

            if(is_array($attributes))
            {
                $columnCriteria = new CDbCriteria();

                foreach ($attributes as $key => $value)
                {
                    if($this->hasAttribute($key))
                    {
                        $columnCriteria->compare($key, $value);
                    }
                }

                $criteria->mergeWith($columnCriteria);
            }

            return $this->updateAll(array("is_delete" => Constant::DELETED), $criteria);
        }
        else
        {
            return parent::deleteAllByAttributes($attributes, $condition, $params);
        }
    }

    /**
     * filter deleted
     * @see CActiveRecord::query()
     */
    protected function query($criteria,$all=false)
    {
        if($this->hasAttribute("is_delete"))
        {
            $criteria->compare($this->getTableAlias() . ".is_delete", Constant::NOT_DELETED);
        }

        return parent::query($criteria,$all);
    }

    /**
     * filter deleted
     * @see CActiveRecord::count()
     */
    public function count($condition="",$params=array())
    {
        Yii::trace(get_class($this).".count()","app.model.BaseModel");
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createCriteria($condition,$params);

        if($this->hasAttribute("is_delete"))
        {
            $criteria->compare($this->getTableAlias() . ".is_delete", Constant::NOT_DELETED);
        }

        $this->applyScopes($criteria);

        if(empty($criteria->with))
            return $builder->createCountCommand($this->getTableSchema(),$criteria)->queryScalar();
        else
        {
            $finder=new CActiveFinder($this,$criteria->with);
            return $finder->count($criteria);
        }
    }

    /**
     * filter deleted
     * @see CActiveRecord::countByAttributes()
     */
    public function countByAttributes($attributes,$condition="",$params=array())
    {
        Yii::trace(get_class($this).".countByAttributes()","app.model.BaseModel");
        $prefix=$this->getTableAlias(true).".";
        $builder=$this->getCommandBuilder();
        $criteria=$builder->createColumnCriteria($this->getTableSchema(),$attributes,$condition,$params,$prefix);

        if($this->hasAttribute("is_delete"))
        {
            $criteria->compare($this->getTableAlias() . ".is_delete", Constant::NOT_DELETED);
        }

        $this->applyScopes($criteria);

        if(empty($criteria->with))
            return $builder->createCountCommand($this->getTableSchema(),$criteria)->queryScalar();
        else
        {
            $finder=new CActiveFinder($this,$criteria->with);
            return $finder->count($criteria);
        }
    }

    /**
     * remove attributes from record model
     */
    public function uselessFields()
    {
        return array("is_delete");
    }

    /**
     * romeve useless fileds when CJSON::encode.
     */
    public function getIterator()
    {
        $attributes=$this->getAttributes();
        $uselessFields = $this->uselessFields();
        foreach($uselessFields as $field)
        {
            if(isset($attributes[$field]))
            {
                unset($attributes[$field]);
            }
        }
        return new CMapIterator($attributes);
    }
}


封装方法时需要先看一下Yii中的有关源码,有的只需要要在源码的基础上添加上自己的条件即可,如count等,只是添加了is_delete条件的判断

转载自:http://blog.csdn.net/f0225/article/details/19079155

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。