public function actionList()
{
$userx = $_GET[user];
if($_GET[user]!=""){
$pages=new CPagination(user::model()->searchByUserId($userx)->count());
$pages->pageSize=1;
$pages->applyLimit();
$models=user::model()->findAll();
}
else{
$criteria = new CDbCriteria;
$pages=new CPagination(user::model()->count($criteria));
$pages->pageSize=1;
$pages->applyLimit($criteria);
$models=user::model()->findAll($criteria);
}
$this->render('list',array(
'models'=>$models,
'pages'=>$pages,
));
}
code user model as below
<code>
public function searchByUserID($userx){
$criteria = new CDbCriteria;
$criteria->condition = "user_name LIKE :username";
$criteria->params=array(":username"=>"%$userx%");
$cari = user::model()->findAll($criteria);
return $cari;
}
</code>
how to add function in model, ex: searchByUserId to return data base on $userx parameter ?
if able, how to count the row from that function return?
well, how to add more search criteria in model as function ?
could you give me example code ?
This is my function, but its not works.
public function searchByUserID($userx){
$criteria = new CDbCriteria;
$criteria->condition = "user_name LIKE :username";
$criteria->params=array(":username"=>"%$userx%");
$cari = user::model()->findAll($criteria);
return $cari;
after I got that function, hoe to return the count or the rows returned ?
The returned result ($cari) can be treated as an array, so use foreach to process the result, count the number of array elements (php function), etcetera.
public function scopes(){
return array(
'fromUser' => array('condition'=> "user_name LIKE :username")
);
}
public function fromUser($username){
$this->getDbCriteria()->mergeWith(array('params' => array('username'=>$username)));
return $this;
}