Monday, October 28, 2013

FindAll & find In Yii

find() With Condition
     $model = User::model()->find('userid=1 AND status="A"');
(OR)
     $model = User::model()->find('userid=:userId And status=:Status',
                                   array(':userId'=>1,':status'=>'A'));

find() Width Criteria
 $criteria = new CDbCriteria;
     $criteria->condition='userid=1 AND status="A"'; 
     $model = User::model()->find($criteria);
(OR)
     $criteria=new CDbCriteria;     
     $criteria->condition='userid=:userId AND status=:Status';
     $criteria->params=array(':userId'=>10,':Status'=>'A');
     $model=User::model()->find($criteria);
find() Max Id
     $criteria=new CDbCriteria;
     $criteria->select='max(userid) as id';
     $model = User->model()->find($criteria);
Yii findAll()
$model=User::model()->findAll();

findAll() With Select
$model=User::model()->findAll(array(
                                'select'=>'userid, username'
                             ));  
findAll() With Conditon
$model=User::model()->findAll(
                              array(
                              'select'=>'userid, username',
                              'condition'=>'status="A"'
                             ));    
findAll() With Conditon, Group
     $model=User::model()->findAll(
                              array(
                              'condition'=>'status="A"',
                              'group'=>'type'
                             ));  
findAll() With Conditon, Order
 $model=User::model()->findAll(
                              array(
                              'select'=>'userid,username',
                              'condition'=>'status="A"',
                              'order'=>'username'
                             ));

 $events = Events::model()->findAll(array(
    "order" => "id DESC",
));
findAll() With Conditon, Order
     $model=User::model()->findAll(
                              array(
                              'select'=>'userid,username',
                              'condition'=>'status="A"',
                              'order'=>'username'
                             )); 
findAll() With limit
   $model=User::model()->findAll(
                              array(
                              'condition'=>'status="A"',
                              'limit'=>'5'
                             ));  
findAll with distinct results
StudentEnrollmentInfo::model()->findAll(array(
    'distinct'=>true,
)), 'session', 'session');
 $Criteria = new CDbCriteria();
                        $Criteria->limit = 8;
                        $Criteria->offset = 1;
                        $Criteria->order = "id DESC";
                        $emplaoyee = Employees::model()->findAll($Criteria);
findAll() "with()" function
      $model=User::model()->with('login')->findAll(
                              array(
                               'select'=>'t.userid,t.username,login.time',
                               'condition'=>'login.satus="L"'
                              ));
findAllByPk Array value
$usermodel=User::model()->findAllByPk($useridarray);
$usermodel=User::model()->findAllByPk(array(2,3,10)
Yii findAll() Join
 $categorymodel=Category::model()->with(array(
            'user'=>array(
            'select'=>'categoryname',
            'joinType'=>'INNER JOIN',
            'condition'=>'user.categoryname="activeuser"',
        ),
        ))->findAll();

2 comments: