fredag den 18. oktober 2013

Yii Scopes Magic in Relations :)

In an earlier post I explained how much the life gets easier when unitlizing the Scopes.
Here is something more.

When accessing the Relation's data or the relative's data you can take advantage of the Scopes in this way:


$currentpost->comments( array( 'scopes'=>array( 'recent' ) ) );
In the above code, you fetch the 'recent' 'Comments' which are gives to a given 'Post'


$visit->course->courseQuizs( array( 'scopes'=>'post' ) )
In this example you fetch the 'post-Course' 'Quiz'.
 
/* Defining Scopes for the Model */
public function scopes()
{
 return array(
  'pre'=>array(
   'condition'=>'pre=1',
  ),
  'post'=>array(
   'condition'=>'pre=0',
  ),
 );
} 
 
 
As you see the Scopes can be type String or Mixed.
If Mixed then all the Scopes will be merged into a single Scope.

Ingen kommentarer:

Send en kommentar