sorting to DASC does not work in Yii? - php

$model = new TableName('search');
$criteria = new CDbCriteria;
$criteria->order = 'id DESC';
$model = $model->findAll($criteria);
$model->unsetAttributes(); // clear any default values
if (isset($_GET['TableName']))
$model->attributes = $_GET['TableName'];
$this->render('TableNameView', array(
'model' => $model,
));
after use this code gets a white screen, no any action, and it should sort the why this is happening and how can I fix this way?

You override the original value of $model variable.
$model = $model->findAll($criteria);
After that $model is not an instance of TableName anymore! findAll returns a list of TableName's. So, you must iterate throught this list to get a single instance of TableName.
$models = $model->findAll($criteria);
foreach( $models as $model ) {
// do something with $model
}
Or, if you just need to get the last record, you can use find:
$criteria = new CDbCriteria;
$criteria->order = 'id DESC';
$model = $model->find($criteria);

Related

How to define model parameter value in Yii?

I have a user table in which i have multiple roles and i am showing different user on different action by using same model search function. By this
public actionAdmin(){
$model = new User('search')
$model->unsetAttributes();
$model->userRole = UmsConfing::ADMIN;
if(isset($_GET('User')))
$model->attributes = $_GET['User'];
$this->render('userlist',arary('model'=>$model));
}
I am using this function for different roles .This works well. but now i want to show admin and operation user in same list i tried
$model->userRole = UmsConfig::ADMIN || UmsConfig:: OPERATION
but i didn't work Please help.
Please you can try this below code:
public actionAdmin(){
$model = new User('search')
$model->unsetAttributes();
if(isset($_GET('User')))
$model->attributes = $_GET['User'];
$this->render('userlist',arary('model'=>$model));
}
Below codeadd in User Model Search Function:
model/User.php
public function search() {
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->addInCondition('userRole', array (UmsConfig::ADMIN,UmsConfig:: OPERATION));
$criteria->compare('status',$this->status);
$criteria->compare('is_deleted',$this->is_deleted);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'pagination' => array(
'pageSize' => yii::app()->params->pagesize,
),
'sort'=>array(
'defaultOrder'=>array(
'id'=>CSort::SORT_DESC
),
),
));
}

Yii - findAll with order by

How to findAll with specific column with order by desc ?
Code bellow worked and find all from the developer id
$id = Yii::app()->user->getState('id');
$models = Games::model()->findAll('developer_id='.$id);
Code bellow worked and ordered
$models = Games::model()->findAll(array('order'=>'status'));
When I mixed together then only worked for findAll developer_id='.$id doesn't order by
$id = Yii::app()->user->getState('id');
$models = Games::model()->findAll('developer_id='.$id,array('order'=>'status'));
Any suggestion to do that ? Thanks
In your model, add this function:
public function scopes() {
return array(
'bystatus' => array('order' => 'status DESC'),
);
}
Now you can do the query like this:
$models = Games::model()->bystatus()->findAll('developer_id='.$id);
=====
Bonus: You can also add this function in your model:
public function bydeveloper($devId) {
$this->getDbCriteria()->mergeWith(array(
'condition' => 'developer_id = '.$devId,
));
return $this;
}
Now you can do the query like this:
$models = Games::model()->bystatus()->bydeveloper($id)->findAll();
you can try this -
$id = Yii::app()->user->getState('id');
$model = Games::model()->findAll(array("condition" => "developer_id = '".$id."'","order" => "status"));
its should be work
You can try use criteria:
$id = Yii::app()->user->getState('id');
$criteria=new CDbCriteria;
$criteria->compare('developer_id',$id);
$criteria->order='status DESC';
$models = Games::model()->findAll($criteria);
It's not the best way but you can do:
$models = Games::model()->findAll('developer_id='.$id.' order by status DESC');
Model::find()->where([['attribute'=>'value']])->orderBy(['attribute'=>SORT_DESC])->all();

YII model ::: CGridView Search by specific attribute using criteria

I am new in yii. I want to search by attribute (field name) from my model and need to view in view page or another page by zii.widgets.grid.CGridView.
how can I create a search() function in model by findByAttribute()
so that we can show the results by attribute without any search
here is my model function but it is not working.. :: ERROR Undefined variable: pp_requisitionno
public function searchView()
{
$criteria=new CDbCriteria();
$criteria->select= 'pp_requisitionno';
$criteria->addSearchCondition('pp_requisitionno',$pp_requisitionno);
$criteria->compare('pp_requisitionno',$this->pp_requisitionno);
$criteria->condition = 'pp_requisitionno=:pp_requisitionno';
$criteria->params = array(':pp_requisitionno'=>Yii::app()->Request->Getpost('pp_requisitionno'));
$model = Requisitiondt::model()->find();
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
Any help please...
It's probably going to be more useful to define a general search function that can be reused for different searches. This can be done in the following way:
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search() {
$criteria = new CDbCriteria;
//Define all your searchable fields here
$criteria->compare('t.title', $this->title, true);
$criteria->compare('t.type', $this->type, true);
$criteria->compare('t.published', $this->published, true);
$criteria->compare('category.id', $this->category_id, true);
//Add any other criteria, like the default sort order etc.
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Then in your controller you can use the search like this;
pubic function actionSearch(){
$model = new Requisitiondt;
if (isset($_POST)){
$model->attributes = $_POST;
$dataProvider = $model->search();
$this->render('searchView', array('dataProvider' => $dataProvider));
}
}
The view 'searchView' then looks like this;
<?php
$this->widget('CGridView', array(
'dataProvider' => $dataProvider,
'columns' => array(
//Add in whatever columns you want the grid to show
)
));
?>
Obviously you'll need to replace your own model names and field names, but this is the general idea. This way will search for any attributes you include in the POST request,and is more reusable.
You need use Relations in your model, read here.
Then add 'filter' => $model, in your GridView Widget options array

Yii CDbCriteria compare 2 attributes to a single value

Hi I am new to yii and I am currently working on a project and I am having trouble with CDbCriteria.
My target query is:
title LIKE '$_GET['search']%' OR description LIKE '$_GET['search']%'
Is it possible to attain same result like this using CDbCriteria compare?
Controller action :
public function actionClassifieds(){
$model = new Classifieds('search');
$model->unsetAttributes();
if(isset($_GET['category'])){
if( $_GET['category'] == 0 ){
$model->classified_category_id = '';
}else{
$model->classified_category_id = $_GET['category'];
}
}
if(isset($_GET['search'])){
$model->title = $_GET['search'];
$model->description = $_GET['search'];
}
$this->render('classifieds',array('model'=>$model));
}
My model:
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('classified_category_id',$this->classified_category_id);
$criteria->compare('title',$this->title,true);
$criteria->compare('description',$this->description,true);
$criteria->compare('price',$this->price,true);
$criteria->compare('timestamp',$this->timestamp);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>10,
),
));
}
Try:
$criteria->condition = "title LIKE :t OR description LIKE :d";
$criteria->params = array(
':t' => trim( Yii::app()->request->getParam('search') ).'%',
':d' => trim( Yii::app()->request->getParam('search') ).'%');
Better using Yii::app()->request->getParam($var_name)
UPDATED
$criteria->compare('classified_category_id',$this->classified_category_id);
$criteria->compare('price',$this->price,true);
$criteria->compare('timestamp',$this->timestamp);
$criteria->addCondition("title LIKE :t OR description LIKE :d");
$criteria->params[':t'] = $this->title;
$criteria->params[':d'] = $this->description;

how to use both condition() and addcondition() for a same search model

I have a grid view with a condition "upload_date = today", but when I search for other dates it is not filtering the grid. Here is my model:
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$date=date('Y-m-d');
$criteria=new CDbCriteria(array('order'=>'upload_date ASC'));
$criteria->condition = "upload_date='$date' ";
if(isset($_GET['upload_date']))
{
$criteria->addCondition('upload_date = :upload_date','AND');
$criteria->params[':upload_date'] = $this->upload_date;
}
$criteria->compare('book_id',$this->book_id);
$criteria->compare('date_received',$this->date_received,true);
$criteria->compare('batch',$this->batch,true);
$criteria->compare('isbn_no',$this->isbn_no,true);
$criteria->compare('book_title',$this->book_title,true);
$criteria->compare('auther_name',$this->auther_name,true);
$criteria->compare('upload_date',$this->upload_date,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>50
),
));
}
I am trying to use both condition and addcondition in same search. What did I do wrong in the model above?
Try this:
public function search()
{
$criteria=new CDbCriteria(array('order'=>'upload_date ASC'));
if(isset($_GET['ModelName']['upload_date']))
{
$criteria->compare('upload_date',$this->upload_date,true);
// $criteria->addCondition('upload_date = :upload_date');
// $criteria->params[':upload_date'] = $this->upload_date;
}
else
{
$date=date('Y-m-d');
$criteria->condition = "upload_date='$date' ";
}
$criteria->compare('book_id',$this->book_id);
$criteria->compare('date_received',$this->date_received,true);
$criteria->compare('batch',$this->batch,true);
$criteria->compare('isbn_no',$this->isbn_no,true);
$criteria->compare('book_title',$this->book_title,true);
$criteria->compare('auther_name',$this->auther_name,true);
$criteria->compare('upload_date',$this->upload_date,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'pagination'=>array(
'pageSize'=>50
),
));
}
You don't need addCondition, specially because you are adding a condition to the same column.
Edit:
In your main initial condition you have already set upload_date = $date, now if you add condition to it, say $_GET['ModelName']['upload_date'] provided by the user in the filter, your actual db query will become something like select * from tbl_name where upload_date = 'xxx' and upload_date = 'yyy' , here xxx=$date you gave, and yyy=date provided by user. Now if xxx != yyy, how will the query return any value? It is not possible.
Hope you got this explanation.

Categories