Yii CActiveRecord 逻辑删除封装

<?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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值