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.

torsdag den 17. oktober 2013

Yii Magic, Use of Scope

Hi,

This is one the magical features of ActiveRecord (AR) in Yii Framework.

When you need to create a Scope or limitation, the only thing that needs to be done is to define and add the 'function scopes' in the model class and then you are able to define the Scope rules.

Example:

In this example the 'owner' scope is defined. If you are familiar with the CDBCriteria definitions, defining the Scope would be like a `walk in the park`.

public function scopes()
    {
        return array(
            'owner'=>array(
                'with'=>array('department'),               
                'condition'=>'t.department_id = department.id AND department.user_company_id=:user_company_id',
                'params'=>array(
                    ':user_company_id'=>Ccom::user()->userCompany->id,
                ),
            ),
        );
     }

/**
* Ccom::user()->userCompany->id 
* is my short hand for
* User::model()->findByPk(Yii::app()->user->id)->userCompany->id
*/

This rule is defined so the user can only access her or his own groups in the departments of his company.

't.' in this case is the Group table, and Group table is related to Department table by the foreign key 'department_id' and Department is related to the UserCompany table by the foreign key 'user_company_id'

To use this Scope you have many options.

Examples:
Use
$dataProvider=new CActiveDataProvider(Group::model()->owner());
Instead of
$dataProvider=new CActiveDataProvider('Group');

Group::model()->owner() return a type CDbCritera object, with every thing there is needed to make an approperiate Select from the database

Use
Department::model()->owner()->findAll();
Instead of
Department::model()->findAll();

Use
$model->owner()->search();
Instead of
$model->search();

I wish the Yii Core developers best of luck. They really do a great job.
Please leave a comment or link if you have related knowledge about this subject, thanks.

Read more here: http://www.yiiframework.com/doc/guide/1.1/en/database.ar#named-scopes

tirsdag den 8. oktober 2013

Yii Select2, How to initiated selected (saved) values


To achieve this

In this scenarie we have 5 choice from which 3 are already saved and we wish to represent this selection again in our form by YiiBooster

Pay attention to text colors.

This widget call:

$this->widget(
        'bootstrap.widgets.TbSelect2',
        array(
            'asDropDownList' => false,
            'name' => 'userRoles',
            'data'=>$userRoles,
            'options' => array( // selected options by default
               
            ),
            'options' => array(           
                'tags' => $userRoles, // Array
                'placeholder' => '',
                'width' => '220px',
                'tokenSeparators' => array(',', ' '),
                'initSelection'=>'js:function(element, callback){
                    var data = [];
                    '.$s2InitStr.'
                    callback(data);
                }',
            ),
        )
    );

Produces this JS code

jQuery('#userRoles').select2(
{
'tags':['Admin','Authenticated','Guest','representative','yderzonen'],
'placeholder':'',
'width':'220px',
'tokenSeparators':[',',' '],
'initSelection':function(element, callback){
var data = [];
data.push({id:'Admin' , text: 'Admin'});
data.push({id:'Authenticated' , text: 'Authenticated'});
data.push({id:'representative' , text: 'representative'});
callback(data);
}});
 
 
After this is done you have to produce this JS code:
 
jQuery(function($) {
 
 /**
  * The selectors must be changed to match your Select2 ID 
  */
 
  $('#s2id_userRoles').select2('val','Admin');
  $('#s2id_userRoles').select2('val','Authenticated');
  $('#s2id_userRoles').select2('val','representative');
 
}); 


More Documentation
 
http://ivaynberg.github.io/select2/ 
http://stackoverflow.com/questions/15174635/proper-usage-of-jquery-select2s-initselection-callback-with-remote-data
http://yiibooster.clevertech.biz/widgets/forms_inputs/view/select2.html#config
 
If you have a better idea please leave a link. Thanks