YII: My Cheat Sheet

1. Query with condition/s
Example

	//This returns data that match with predefined conditions
	$arrResults = YourModelName::model()->findAllByAttributes(array('name'=>$name));
	//To access the results you can loop it
	$rVal = array();
	foreach ($arrResults as $v){
		$rVal[] = $v->{$attr}; //$attr can be id, name etc.
	}

	//Returns one data
	$arrResults = YourModelName::model()->findByAttributes(array('name'=>$name));
	echo $arrResults->name;

//with custom fields and conditions
$arrResults = ModelName::model()->findAll(
array(
‘select’ => ‘id,title,created’,
‘condition’ => ‘id > 436 || id = 2’
)
);
2. Extract Data from Model

$users = User::model()->findAll();
$usersArr = CHtml::listData( $users, 'id' , 'name');
print_r( $usersArr );

Array {
    2 => 'someone',
    20 => 'kitty',
    102 => 'Marian',
    // ...
}

Tags:

Comments are closed.