Difference Between Defaultscope() And Beforefind()

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





public function defaultScope()

{

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

		

return array(

'condition'=> "tenant=:tenant",

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

}







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();

  }






getting same result. which is better?

Please reply …

HI

Please reply won’t increase the number or quality of answer. Being polite may.

There none better, for your simple use, there is no deep difference, use the one you think is the more natural to you.

this is not the answer that i expected!!

anyway thank u.

I think that the answer is in this:


$criteria = new CDbCriteria;

defaultScope is cleaner and is meant to be used when you want to add something common to all model queries. You have to write less code, you don’t have to create new objects or whatsoever. Just condition and params.

Thanks Mihkel :)

depending on what you would expect as a final result. In your specific implementation there is no difference. But if you refer to the docs: http://www.yiiframework.com/doc/api/1.1/CActiveRecord#defaultScope-detail you will see that defaultScope for example is only valid for SELECT statements. Also one of the major ideas behind defaultScope is to achiev DB table inheritance ;) But even in your case I would still use defaultScope as it merges into $criteria automatically. Anyway frameworks are there to think instead of you from time to time :lol:

ok bettor.

So in my case the defaultscope is better . right?

Thank you bettor .

Yep! B)

Ok Thank you .