Yii Framework Forum: Difference Between Defaultscope() And Beforefind() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Difference Between Defaultscope() And Beforefind() difference between defaultScope() and beforeFind() Rate Topic: -----

#1 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 884
  • Joined: 20-April 11
  • Location:India

Posted 16 April 2013 - 07:51 AM

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?
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#2 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 884
  • Joined: 20-April 11
  • Location:India

Posted 17 April 2013 - 01:02 AM

Please reply ...
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#3 User is offline   seb7 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 03-April 12

Posted 17 April 2013 - 03:09 AM

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.
0

#4 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 884
  • Joined: 20-April 11
  • Location:India

Posted 17 April 2013 - 03:57 AM

View Postseb7, on 17 April 2013 - 03:09 AM, said:

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.
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#5 User is offline   Mihkel. 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 20
  • Joined: 12-November 12

Posted 17 April 2013 - 04:04 AM

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.
1

#6 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 884
  • Joined: 20-April 11
  • Location:India

Posted 17 April 2013 - 04:07 AM

View PostMihkel., on 17 April 2013 - 04:04 AM, said:

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 :)
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

#7 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 19 April 2013 - 03:55 AM

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.yiiframew...ultScope-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:
0

#8 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 884
  • Joined: 20-April 11
  • Location:India

Posted 19 April 2013 - 05:27 AM

View Postbettor, on 19 April 2013 - 03:55 AM, said:

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.yiiframew...ultScope-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 .
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
1

#9 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 19 April 2013 - 05:52 AM

View PostRajith R, on 19 April 2013 - 05:27 AM, said:

ok bettor.

So in my case the defaultscope is better . right?

Thank you bettor .

Yep! B)
1

#10 User is offline   Rajith R 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 884
  • Joined: 20-April 11
  • Location:India

Posted 19 April 2013 - 06:24 AM

View Postbettor, on 19 April 2013 - 05:52 AM, said:

Yep! B)



Ok Thank you .
Rajith Ramachandran,
Wiwo inc.
| Mobile: 919995504508
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users