List Of Query Commands for getting Data from database in Yii
Get Single Record.
1. ModelName::model()->findByPk($primaryKey);
2. ModelName::model()->findByAttributes(array(‘columnName1?=>$value1, ‘columnName2?=>$value2));
3. ModelName::model()->find(array(‘condition’=>’columnName1=’.$value1. ‘ and columnName2=’.$value2));
4. Yii::app()->user->db->createCommand() ->select(‘*’) ->from(‘tableName’) ->where(‘columnName1=’.$value1.’ and columnName2=’.$value2) ->queryRow();
Get Last Single Record.
1. ModelName::model()->findByAttributes(array(‘columnName1?=>$value1, ‘columnName2?=>$value2, array(‘order’=>’Auto_Increment_ID desc’)));
2. ModelName::model()->find(array(‘condition’=>’columnName1=’.$value1.’ and columnName2=’.$value2, ’order’=>’auto_Increment_ID desc’));
3. Yii::app()->user->db->createCommand() ->select(‘*’) ->from(‘tableName’) ->where(‘columnName1=’.$value1.’ and columnName2=’.$value2.’ order by Auto_Increment_ID’) ->queryRow();
Get Multiple Records.
1. ModelName::model()->findAll(array(‘condition’=>’columnName1=’.$value1.’ and columnName2=’.$value2));
2. ModelName::model()->findAll(‘columnName1=’.$value1.’ and columnName2=’.$value2);
3. ModelName::model()->findAllByAttibutes(array(‘columnName1?=>$value1,’columnName2?=>$value2));
4. ModelName::model()->findAllByAttibutes(array(‘columnName1?=>$value1,’columnName2?=>$value2), $condition= ‘columnName3 <> :aliasName3 AND columnName4 >= :aliasName4 OR columnName5 = :aliasName5?, $params =array( ‘:aliasName3? => $value3, ‘:aliasName4? => $value4, ‘:aliasName5? => $value5, ));
5. Yii::app()->user->db->createCommand() ->select(‘*’) ->from(‘tableName’) ->where(‘columnName1=’.$value1.’ and columnName2=’.$value2) ->queryAll();