Cactiverecord Limitations

the beforeDelete() method is not invoking for

deleteAllByAttributes();




public function deleteAllByAttributes($attributes,$condition='',$params=array())

{

    Yii::trace(get_class($this).'.deleteAllByAttributes()','system.db.ar.CActiveRecord');

    $builder=$this->getCommandBuilder();

    $table=$this->getTableSchema();

    $criteria=$builder->createColumnCriteria($table,$attributes,$condition,$params);

    $command=$builder->createDeleteCommand($table,$criteria);

    return $command->execute();

}



deleteAll();




public function deleteAll($condition='',$params=array())

{

    Yii::trace(get_class($this).'.deleteAll()','system.db.ar.CActiveRecord');

    $builder=$this->getCommandBuilder();

    $criteria=$builder->createCriteria($condition,$params);

    $command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);

    return $command->execute();

}




deleteByPk();





public function deleteByPk($pk,$condition='',$params=array())

{

    Yii::trace(get_class($this).'.deleteByPk()','system.db.ar.CActiveRecord');

    $builder=$this->getCommandBuilder();

    $criteria=$builder->createPkCriteria($this->getTableSchema(),$pk,$condition,$params);

    $command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);

    return $command->execute();

}




its only invoking for delete();




public function delete()

{

    if(!$this->getIsNewRecord())

    {

        Yii::trace(get_class($this).'.delete()','system.db.ar.CActiveRecord');

        if($this->beforeDelete())

        {

            $result=$this->deleteByPk($this->getPrimaryKey())>0;

            $this->afterDelete();

            return $result;

        }

        else

            return false;

    }

    else

        throw new CDbException(Yii::t('yii','The active record cannot be deleted because it is new.'));

}



i need it for all delete methods.

suggestions appreciated . please

AfterDelete is for ActiveRecord, not commands.

what?

your code uses


$command=$builder->createDeleteCommand($this->getTableSchema(),$criteria);

That’s the CDbCommandBuilder - http://www.yiiframework.com/doc/api/1.1/CDbCommandBuilder

AfterDelete is a method of CActiveRecord - http://www.yiiframework.com/doc/api/1.1/CActiveRecord#afterDelete-detail


You can instantiate each object (CActiveRecord) then ->delete() it .

i know that man…

pls dont miss lead . the code i pasted is from the CActiveRecord.php file . ok… not my code.

i am saying that the deleteAllByAttributes(); deleteAll(); deleteByPk(); are not going through the beforeDelete().

very simple.

I need beforeDelete() for all delete methods.

Sorry, but how were we supposed to know that it’s yii code ?

Override yii deleteAll() with your own in your model :




public function deleteAll($condition='',$params=array())

{

    // find all records using the $condition ans $param


    // loop on this records, one by one to trigger ->delete()


    // return true if all deletion were successfull

}



simple.

Do the same for all functions

ok thats the proper way.

I am saying that its the limitation that i faced 1st time with yii.

i am saying this because of the beforeFind() will work for all the findxxxxx() methods !!

Hi Rajith,

It’s in fact a limitation, but I think it’s a reasonable design decision.

It’s true that “deleteAll” could be implemented so that “beforeDelete” would be called for every deleted record. But I don’t want it.

When I want to use “deleteAll”, what I really want is a thin wrapper over the plain sql of “DELETE FROM … WHERE …”. I don’t want Yii to read all the target records into AR objects before deleting them. It would significantly slow down the process.

Yeah… i also found that it would slow down the process ,because of read to AR.

Thanks softark :) :)

hi softark,

what is the difference between defaultScope() and beforeFind() ?

this is my post

http://www.yiiframework.com/forum/index.php/topic/42388-difference-between-defaultscope-and-beforefind/page__p__201397#entry201397