Im having problems in the CGridView when Im trying to search a value that its related to another table (model).
My Relations are:
public function relations()
{
return array(
'iduserFrom' => array(self::BELONGS_TO, 'CrugeUser', 'iduser_from'),
'iduserTo' => array(self::BELONGS_TO, 'CrugeUser', 'iduser_to'),
);
}
And my search():
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('idinvitation',$this->idinvitation);
$criteria->compare('iduser_from',$this->iduser_from);
$criteria->compare('iduser_to',$this->iduser_to);
$criteria->compare('state',$this->state);
$criteria->compare('create_time',$this->create_time,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
and in case is necessary,
My CGridView:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'invitation-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'idinvitation',
array(
'name' => 'iduser_from',
'value' => 'CrugeStoredUser::Model()->FindByPk($data->iduser_from)->username',
),
array(
'name' => 'iduser_to',
'value' => 'CrugeStoredUser::Model()->FindByPk($data->iduser_to)->username',
),
array(
'name' => 'state',
'value'=>'Invitation::getEstadoInvitacion($data->state)',
'filter'=>CHtml::dropDownList(
'Invitation[state]',
$model->state,
CHtml::listData(Invitation::model()->getEstadoLista(), 'id', 'valor'),
array(
'empty' => 'Todos',
)
),
'cssClassExpression'=> 'Invitation::getEstadoColor($data->state)',
),
'create_time',
array(
'class'=>'CButtonColumn',
),
),)); ?>
So what Im trying is searching an user by his "username" located in model "CrugeStoredUser", but when I try this I get nothing
Some Help?
I've just spent few days over the same problem.
My solution is
Model:
public $iduser_from_name;
public $iduser_to_name;
public function rules()
{
return array(
...
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('...., iduser_from_name,iduser_to_name ', 'safe', 'on'=>'search'),
);
}
public function attributeLabels()
{
return array(
...
'iduser_from_name ' => 'Username From',
'iduser_to_name ' => 'Username To',
);
}
public function search()
{
...
if($this->iduser_from_name)
{
$criteria->together = true;
$criteria->with = array('iduserFrom');
$criteria->compare('iduserFrom.username',$this->iduser_from_name,true);
}
if($this->iduser_to_name)
{
$criteria->together = true;
$criteria->with = array('iduserTo');
$criteria->compare('iduserTo.username',$this->iduser_to_name,true);
}
...
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
View:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'invitation-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'name'=>'iduser_from_name',
'value'=>'$data->iduserFrom->username',
),
array(
'name'=>'iduser_to_name',
'value'=>'$data->iduserTo->username',
),
.....
));
Please make sure following points:
--->Make new "property" (required)
--->mark as "safe" in 'on'=>'search' scenario in rules method (required)
--->set name in attributeLabels method (optional)
--->add condition with "with" and "together" in search function (required)
--->set "name" in "columns" as the "property" name(required
I've tested it with MANY_MANY realtion but I'm qite sure it works with other types.
Related
I am using Cgridview to display results for "User" model with relation from "UserFlag" model.
"User" model -> tbl_user (id, name, password, flag)
"Flag" model -> tbl_userFlag (id, flag)
The id means the same from both models. However, the flags means differently (I can't modify the database so have to stick with it) and I need to display them on the same gridview.
The problem I encounter is that
the gridview can show both flags correctly but it fails and shows error when I try to sort and filter the flag from "User" model. (However sorting and filtering work fine for the flag from "UserFlag" model.)
How can I solve it?
The error log:
CDbCommand failed to prepare the SQL statement: SQLSTATE[HY000]: General error: 1 ambiguous column name: flag.
"User" model:
class User extends CActiveRecord
{
public function relations()
{
return array(
'FLAG' => array(self::HAS_ONE, 'UserFlag','id'),
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('name',$this->username,true);
$criteria->compare('password',$this->password,true);
$criteria->compare('flag',$this->flag,true);
$criteria->with = array(
'FLAG' => array(
'select' => 'FLAG.flag',
'together' => true,
)
);
$criteria->compare('FLAG.flag',$this->flagFromB,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=> array(
'attributes'=>array(
'flagFromB' => array(
'asc' => 'FLAG.flag',
'desc' => 'FLAG.flag DESC',
),
'*',
),
),
));
}
"UserFlag" model:
Link to table tbl_userFlag (id, flag)
"User" view:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'user-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'id',
'username',
'password',
'email',
'flag',
array(
'name' => 'flagFromB',
'type' => 'raw',
'value' => '$data->FLAG->flag',
),
array(
'class'=>'CButtonColumn',
),
),
));
You can change your modal relationship.
User.php modal file.
<?php
class User extends CActiveRecord {
public function relations() {
return array(
'FLAG' => array(self::BELONGS_TO, 'UserFlag', 'id'),
);
}
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id);
$criteria->compare('name', $this->username, true);
$criteria->compare('password', $this->password, true);
$criteria->compare('flag', $this->flag, true);
$criteria->with = array(
'FLAG' => array(
'select' => 'FLAG.flag',
'together' => true,
)
);
$criteria->compare('FLAG.flag', $this->flagFromB, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => array(
'attributes' => array(
'flagFromB' => array(
'asc' => 'FLAG.flag',
'desc' => 'FLAG.flag DESC',
),
'*',
),
),
));
}
}
?>
I hope it will help.
this is my view file
.........
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'header' => 'Order ID',
'name' => 'order_id',
'type' => 'raw',
'value' => 'Order::getorderid($data->order_id)',
),
),
));
this is my file controller.php file for search function
public function actionSearch()
{
$model = new Order('search');
$model->unsetAttributes(); // clear any default values
if(isset($_GET['Order'])) {
$model->attributes = $_GET['Order'];
//echo "pre"; print_r($_GET); exit;
}
$this->render('search',array(
'model'=>$model,
));
}
this is the model file.php
public function search()
{
$criteria=new CDbCriteria;
$criteria->select='t.*';
$criteria->compare('t.order_id',$this->order_id,true);
$criteria->compare('t.payment_firstname',$this->payment_firstname,true);
$criteria->compare('t.telephone',$this->telephone,true);
$criteria->compare('t.email',$this->email,true);
$criteria->compare('t.payment_address_1',$this->payment_address_1,true);
$criteria->compare('t.tracking_id',$this->tracking_id,true);
$criteria->join = 'left join order_product op on op.order_id = t.order_id where t.order_type IN (3) group by t.order_id';
$criteria->together = true;
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=>'order_id DESC',
),
'pagination'=>array(
'pageSize' => 40,
),
));
}
I don't see any errors in console.. $_GET is returning the exact the field, but after searching for order_id it is displaying all the orders..
where have this gone wrong ?
Check these code this is what i am using (DB:Postgresql)
Controller
public function actionEstimate()
{
$model=new SalesEstimate();
$model->unsetAttributes();
if(isset($_GET['SalesEstimate']))
{
$model->setAttributes($_GET['SalesEstimate']);
}
$this->render('estimate', array('model'=>$model));
}
Model
public function search()
{
$command =Yii::app()->db->createCommand("SELECT COUNT(*) FROM sp_sales_estimate_search(:enquiry_id,:ref_no,:estimate_date,:property_id,:amount,:status)");
$command->bindParam(":enquiry_id",$this->enquiry_id,PDO::PARAM_STR);
$command->bindParam(":ref_no",$this->ref_no,PDO::PARAM_STR);
$command->bindParam(":estimate_date",$this->estimate_date,PDO::PARAM_STR);
$command->bindParam(":property_id",$this->property_id,PDO::PARAM_STR);
$command->bindParam(":amount",$this->amount,PDO::PARAM_STR);
$command->bindParam(":status",$this->status_search,PDO::PARAM_STR);
$count=$command->queryScalar();
$sql="SELECT * FROM sp_sales_estimate_search(:enquiry_id,:ref_no,:estimate_date,:property_id,:amount,:status)";
$dataProvider=new CSqlDataProvider($sql, array(
'params' => array(':enquiry_id' => $this->enquiry_id,':ref_no' => $this->ref_no,':estimate_date' => $this->estimate_date,':property_id' => $this->property_id,':amount' => $this->amount,':status' => $this->status_search),
'totalItemCount'=>$count,
'db'=>Yii::app()->db,
'sort'=>array(
'attributes'=>array(
'ref_no','estimate_date','property_id','amount','status_search'
),
),
'pagination'=>array(
'pageSize'=>10,
),
));
return $dataProvider;
}
View
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'estimate-grid',
'itemsCssClass'=>'table table-bordered table-condensed table-hover table-striped dataTable',
'filter'=>$model,
'dataProvider'=>$model->search(),
'enablePagination' => true,
'enableHistory'=>true,
'pagerCssClass'=>'dataTables_paginate paging_bootstrap table-pagination',
'pager' => array('header'=>'','htmlOptions'=>array('class'=>'pagination')),
'columns' => array(
array(name=>'ref_no','value'=>'$data["ref_no"]','header'=>'Ref No','htmlOptions'=>array('style'=>'text-align:right;width:5%')),
array(name=>'estimate_date','value'=>'Yii::app()->dateFormatter->format("dd/MM/yyyy",$data["estimate_date"])','header'=>'Date'),
),
'htmlOptions'=>array('class'=>'grid-view table-responsive'),
))
?>
Did you set order_id as safe in model.?
How to make CGridView columns sortable (On clicking column title) using CSqldataprovider.
In controller
$sql = "select id ,name, address
from User
where city = 'ABC' ";
$rawData = Yii::app()->db->createCommand($sql);
return $allMovies = new CSqlDataProvider($rawData, array(
'keyField' => 'id',
'sort'=>array(
'attributes'=>array(
'id', 'name', 'address',
),
),
'pagination' => array(
'pageSize' => 10,
),
));
In view
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'all_movies',
'dataProvider' => $allMoviesStats,
'columns' => array(
'id',
'name',
'address',
'city'
)
)
);?>
It is giving error
"error":{"code":99,"text":"Property \"CGridView.sort\" is not defined.
You need to create object for CDbCriteria and Sort class and try to use as below and change the code for your requirement.
public function search()
{
$criteria=new CDbCriteria;
$criteria->condition="active=1";
if($this->name=="Enter Country Name" || $this->name=='') {
$this->name='';
} else {
$this->name=$this->name;
}
$criteria->compare('name',$this->name,true);
$sort = new CSort;
$sort->defaultOrder = 'id DESC';
$sort->attributes = array(
'name' => array(
'asc' =>'name',
'desc' =>'name DESC',
),
...
... // attributes to sort
);
return new CActiveDataProvider('Country', array( //Country is nothing but you model class name
'criteria' =>$criteria,
'sort' => $sort,
'pagination'=>array('pageSize'=> 10),
));
}
I have a admin.php created by gii, inside it there's a table column 'lang_id' have relation to primary key 'id' of table 'lang'.
What should I put in the columns array ? I think i should use "Lang.name" but it didn't work.
protect/view/mainmenu/admin.php
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mainmenu-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'menu_id',
'Lang.name', // I want this column display the name of Language, instead of lang_id
'name',
'remark',
array(
'class'=>'CButtonColumn',
'template'=>'{update}'
),
),
)); ?>
protect/model/Mainmenu.php
public function relations(){
return array(
'lang'=>array(self::HAS_ONE, 'Lang', 'lang_id')
);
}
public function search(){
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id,true);
$criteria->compare('menu_id',$this->menu_id);
$criteria->compare('lang_id',$this->lang_id);
$criteria->compare('name',$this->name,true);
$criteria->compare('sorting',$this->sorting);
$criteria->compare('remark',$this->remark,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
the relation name is lang. Whereas in the columns array you have used Lang (with l in uppercase).
So you will have to do this
'lang.name'
in the columns array
and second thing in the search()
You need to add one more line
$criteria->with = array(
'lang'
);
maybe this can work
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mainmenu-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'menu_id',
array(
'name'=>'Lang',
'value'=>'$data->lang->name',
),
'name',
'remark',
array(
'class'=>'CButtonColumn',
'template'=>'{update}'
),
),
)); ?>
Hi everyone,
I am new to yii and I am performing crud operation using gii for model called city and I have relation with state model a city->state_id = state.state_id. Crud everything is working fine, but in that view part I want to filter based on state_name
In relations method,I have code this code
'state' => array(self::BELONGS_TO, 'MasState', 'State_Id'),
here is the code for search
public function search() {
// #todo Please modify the following code to remove attributes that should not be searched.
$criteria = new CDbCriteria;
$MasState = new MasState;
$criteria->together = true;
$criteria->compare('state.State_Name',$MasState->State_Name,true);
$criteria->with= array('state');
$criteria->compare('City_Id', $this->City_Id);
$criteria->compare('State_Id', $this->State_Id);
$criteria->compare('City_Name', $this->City_Name, true);
$criteria->compare('Del_Flag', $this->Del_Flag);
$criteria->compare('Ts_Modified', $this->Ts_Modified, true);
$criteria->compare('Ts_Created', $this->Ts_Created, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
and admin.php page code
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mas-city-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'header' => 'SL NO#',
'class' => 'CounterColumn'
),
array(
'name' => 'state.State_Name',
'header' => 'State Name',
'filter' => CHtml::activeTextField($model,'State_Name'),
'value' => '$data->state->State_Name',
),
'City_Name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>
I am getting error "Property "MasCity.State_Name" is not defined" I know city model does not have state_name but how to make it work. Plese help me
Thanks in advance
First in your search method you should do:
public function search() {
$criteria = new CDbCriteria;
$criteria->with= array('state');
$criteria->together = true;
//Get the relation value from the request
$criteria->compare('state.State_Name',Yii::app()->request->getParam('state_state_name'),true);
$criteria->compare('City_Id', $this->City_Id);
$criteria->compare('State_Id', $this->State_Id);
$criteria->compare('City_Name', $this->City_Name, true);
$criteria->compare('Del_Flag', $this->Del_Flag);
$criteria->compare('Ts_Modified', $this->Ts_Modified, true);
$criteria->compare('Ts_Created', $this->Ts_Created, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
And in your view:
<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'mas-city-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
array(
'header' => 'SL NO#',
'class' => 'CounterColumn'
),
array(
'name' => 'state.State_Name',
'header' => 'State Name',
'filter' => CHtml::textField('state_state_name', Yii::app()->request->getParam('state_state_name')),
'value' => '$data->state->State_Name',
),
'City_Name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>