Usage of CDbCriteria in Yii Framework

| December 23, 2013 | 0 Comments
<?php
 
$c = new CDbCriteria();
Hoge::model()->findAll($c);
// SELECT * FROM `hoge` `t`
 
 
$c = new CDbCriteria();
$c->addSearchCondition('t.fuga', null); // null, false, ''などは無視されるようです
 Hoge::model()->findAll($c);
// SELECT * FROM `hoge` `t`
 
 
$c = new CDbCriteria();
$c->addSearchCondition('t.fuga', 'a');
Hoge::model()->findAll($c);
// SELECT * FROM `hoge` `t` WHERE t.fuga LIKE :ycp0. Bound with :ycp0='%a%'
 
 
$c = new CDbCriteria();
$c->addSearchCondition('t.fuga', 'a%', false);
Hoge::model()->findAll($c);
// SELECT * FROM `hoge` `t` WHERE t.fuga LIKE :ycp0. Bound with :ycp0='a%'
 
 
$c = new CDbCriteria();
$c->addSearchCondition('t.fuga', 'a');
$c->addSearchCondition('t.piyo', 'd');
Hoge::model()->findAll($c);
// SELECT * FROM `hoge` `t` WHERE (t.fuga LIKE :ycp0) AND (t.piyo LIKE :ycp1). Bound with :ycp0='%a%', :ycp1='%d%'
 
 
$c = new CDbCriteria();
$c->addSearchCondition('t.fuga', 'a', true, 'OR');
$c->addSearchCondition('t.piyo', 'd', true, 'OR');
Hoge::model()->findAll($c);
// SELECT * FROM `hoge` `t` WHERE (t.fuga LIKE :ycp0) OR (t.piyo LIKE :ycp1). Bound with :ycp0='%a%', :ycp1='%d%'
 
 
$c = new CDbCriteria();
$c->compare('t.fuga', 'a', true, 'OR');
$c->compare('t.piyo', 'b', true, 'OR');
Hoge::model()->findAll($c);
// SELECT * FROM `hoge` `t` WHERE (t.fuga LIKE :ycp0) OR (t.piyo LIKE :ycp1). Bound with :ycp0='%a%', :ycp1='%b%'
 
 
Hoge::model()->findAll('t.fuga LIKE :fuga', array(':fuga' => '%a%'));
// SELECT * FROM `hoge` `t` WHERE t.fuga LIKE :fuga. Bound with :fuga='%a%'
 
 
Hoge::model()->findAll(array(
    'condition' => 't.fuga LIKE :fuga',
    'params' => array(':fuga' => '%a%'),
));
// SELECT * FROM `hoge` `t` WHERE t.fuga LIKE :fuga. Bound with :fuga='%a%'
 
 
$c = new CDbCriteria();
$c->condition = 't.fuga LIKE :fuga';
$c->params = array(':fuga' => '%a%');
Hoge::model()->findAll($c);
// SELECT * FROM `hoge` `t` WHERE t.fuga LIKE :fuga. Bound with :fuga='%a%'
$Criteria = new CDbCriteria();
$Criteria->condition = "price > 30";
$Products = Product::model()->findAll($Criteria);
/An example using the constructor to populate the properties.
$Criteria = new CDbCriteria(array('condition' => 'price > 30'));
$Products = Product::model()->findAll($Criteria);
$Criteria = new CDbCriteria();
$Criteria->condition = "price > 30";
$Criteria->limit = 1;
$Products = Product::model()->findAll($Criteria);
$Criteria = new CDbCriteria();
$Criteria->condition = "price > 30";
$Criteria->limit = 1;
$Criteria->offset = 1;
$Products = Product::model()->findAll($Criteria);
$Criteria = new CDbCriteria();
$Criteria->condition = "price > 30";
$Criteria->limit = 1;
$Criteria->offset = 1;
$Criteria->order = "name ASC";
$Products = Product::model()->findAll($Criteria);
$Criteria = new CDbCriteria();
$Criteria->condition = "price > 30";
$Criteria->limit = 1;
$Criteria->offset = 1;
$Criteria->order = "name ASC";
$Criteria->select = "id, name";
$Products = Product::model()->findAll($Criteria);
$criteria = new CDbCriteria;
$criteria->with = array('foreign_table1',
                        'foreign_table2', 
                        'foreign_table2.foreign_table3');
$criteria->together = true; // ADDED THIS
$criteria->select = array('id');
$criteria->condition = "foreign_table1.col1=:col_val AND 
                        foreign_table3.col3=:col_val2";
$criteria->params = array(':col_val' => some_val, ':col_val2' => other_val);
$criteria->order = 'foreign_table3.col5 DESC';
$criteria->limit = 10;
$criteria = new CDbCriteria;                    
                      $criteria->condition = 'flag="APP_PUBLISHED" or flag="APP_UPDATED"';
                      $criteria->order = "id desc";
                      $criteria->limit = 5;
                      $userActivities = UserActivity::model()->findAll($criteria);

Related Posts:

Tags: ,

Category: Uncategorized

Leave a Reply