Thursday, 12 February 2015

Getting The records using YII :: Find method

Getting a single Value using find method 

$criteria=new CDbCriteria;
$criteria->select='max(reg_no) as reg_no';
$oldreg=GrvRegister::model()->find($criteria);
$newreg=$oldreg['reg_no'] + 1;

In the above example criteria is the select condition

Getting a single record using Primary Key

$model=GrvRegister::model()->findByPk($id);

now the $model will have the record.

Another find method is

$urec = User::model()->find("username='$var'");
$desig=$urec->DISG_NAME;

$post=Post::model()->find();
$post=Post::model()->find('id=5');
$post=Post::model()->find('id=:id',array(':id'=>2));
$post=Post::model()->find(array('condition'=>'id=:id','params'=>array(':id'=>3)));
$post=Post::model()->find('id=6');
$posts=Post::model()->findAll();
$posts=Post::model()->findAll(new CDbCriteria(array('limit'=>3,'offset'=>1)));
$posts=Post::model()->findAll('id=6');
$post=Post::model()->findByPk(2);
$post=Post::model()->findByPk(array(3,2));
$post=Post::model()->findByPk(array());
$post=Post::model()->findByPk(null);
$post=Post::model()->findByPk(6);
$posts=Post::model()->findAllByPk(array(4,3,2),'id<4');
$posts=Post::model()->findAllByPk(2);
$posts=Post::model()->findAllByPk(array());
$post=Post::model()->findByAttributes(array('author_id'=>2),array('order'=>'id DESC'));
$posts=Post::model()->findAllByAttributes(array('author_id'=>2));
$post=Post::model()->findBySql('select * from posts where id=:id',array(':id'=>2));
$posts=Post::model()->findAllBySql('select * from posts where id>:id',array(':id'=>2));




No comments:

Post a Comment