Beforedelete Check Condition

I am developing a SaaS application

i want to deleteByAttributes

before the deletion i want to check for the ownership

How I write beforeDelete()

these are my beforeSave and beforeFind







public function beforeSave()

    {

		$currentdb  = explode('=', Yii::app()->db->connectionString);

		$this->tenant = $currentdb[2];

		return parent::beforeSave();

	}







public function beforeFind()

	{

		$currentdb  = explode('=', Yii::app()->db->connectionString);

		

		$criteria = new CDbCriteria;

		$criteria->condition = "tenant=:tenant";

		$criteria->params = array(":tenant"=>$currentdb[2]);

		

		$this->dbCriteria->mergeWith($criteria);

		parent::beforeFind();

    }




Help me

please reply . .

Ownership of what? There is CActiveRecord::beforeDelete(). Just overwrite it in your model and this is runned before your deletion process. If it is not the owner, return false and delete is not performed.

ok thank u

i mean multiple owners are there for data. when i use the simple deletebyAttribute ,the others with the same condition satisfies will be removed . so i want to avoid the deletion of the datas of other owners .

there is a field in every table named "tenant"

so as you said





protected function beforeDelete()

	{

		if ($this->tenant == "Myownerid")

		{

			return true;

			

		}

                else

                {

		        

                        return false; // prevent actual DELETE query from being run


                }

	}

this will work right?

Yes.

Look at CActiveRecord::delete. If beforeDelete returns false then delete returns also false and deleteByPk is not executed.

http://www.yiiframework.com/doc/api/1.1/CActiveRecord#delete-detail

thanks mihkel…thats the way i founded too :) . so in our conclusion it will work.

its not working for deleteAllByAttributes(array(‘campaign’=>‘1’)); !!

deleteAllByAttributes function not going through the beforeDelete() check !

what to do !!

Overwrite the model deleteAllByAttributes like this:




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

	{

		if ($this->beforeDelete()) {

			return parent::deleteAllByAttributes($attributes, $condition, $params);

		}

		return false;

	}



Should do the trick :)

If you need this function everywhere, create your own activerecord, which extends CActiveRecord and extend your own models on activerecord.




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

{

                if ($this->beforeDelete()) {

                        return parent::deleteAllByAttributes($attributes, $condition, $params);

                }

                return false;

}



this will not work . it only check only once . if first attribute is getting true result it deletes all because , deleteAllByAttributes deleting in a single step .

i have only one option to use it everywhere is change the CActiveRecord , or add condition in the deleteAllByAttributes();

Ah right. But you do not have to change cactiverecord at all. Because there is nothing to change. If you have to check every record then iterate over objects and call delete everytime.




...

	$models = Model::model()->findAll();

	foreach ($models as $model) {

		//calls before delete

		$model->delete();

	}		

	...



Or yeah, write condition to delete is the easiest way.




...

	Model::model()->deleteByAttributes('$attributes','condition = :something', array(':something' => $someVar));	

	...



yeah :) this is what i used !! cheers !!