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.
Related
I am trying to access attributes of the model in the view and it is throwing me the error mentioned in the title. This is my CompanyMask model:
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'company' => array(self::BELONGS_TO, 'company', 'company_id'),
);
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
// $criteria->select = "t.id, t.company_id, t.mask_id, t.mask_text, c.name as company_name";
// $criteria->join = 'LEFT JOIN company c on c.id=t.company_id';
$criteria->with = array('company');
$criteria->compare('id', $this->id);
$criteria->compare('company_id', $this->company_id);
$criteria->compare('mask_id', $this->mask_id);
$criteria->compare('mask_text', $this->mask_text, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
Now In the view I am trying to access the name of the company like this:
$gridView = $this->widget('zii.widgets.grid.CGridView', array(
'id' => 'deals-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'ajaxUpdate' => 'deals-grid',
'itemsCssClass' => 'table table-striped table-responsive table-bordered no-margin',
'pagerCssClass' => 'pagination pagination-sm no-margin pull-right',
'pager' => array(
'maxButtonCount' => '7',
),
'columns' => array(
array(
'header' => 'Company Name',
'type' => 'raw',
'htmlOptions' => array('style' => 'width:15%',),
'value' => '$data->company->name',
),
),
));
What am I doing wrong here? Any help?
It's about two things:
You are using $data as object, but it's an array: $data['company']->name
You are using single quotes, so the value is the literal value $data->company->name instead of the real value. Remove the single quotes around $data['company']->name
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.
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 two models. They are DisnotificationUpdate and DisNotification.
DisnotificationUpdate relations are as below.
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'user' => array(self::BELONGS_TO, 'Login', 'userid'),
'notifi' => array(self::BELONGS_TO, 'Disnotification', 'notifi_id'),
);
}
It's attributes are as following.
public function attributeLabels()
{
return array(
'id' => 'ID',
'notifi_id' => 'Notifi',
'view' => 'View',
'userid' => 'Userid',
);
}
Disnotification model has following attributes.
public function attributeLabels()
{
return array(
'id' => 'ID',
'notification' => 'Notification',
'owner' => 'Owner',
'workspace_id' => 'Workspace',
'postID' => 'Post',
'pDate' => 'P Date',
);
}
I want to select values from DisnotificationUpdate and order by values using 'pDate' in DisNotification.
I tried as following.
$dataProvider=DisnotificationUpdate::model()->findAll(array(
'condition' => 'userid=:userid',
'order' => 'notifi.pDate ASC',
'limit'=>10,
'params' => array(':userid' => $myid)
));
But it is giving an error saying, "Unknown column 'notifi.pDate' in 'order clause'". What I am doing wrong? Thanks.
You need to eager load related model
$dataProvider=DisnotificationUpdate::model()->findAll(array(
'with' => array('notifi'),
'condition' => 'userid=:userid',
'order' => 'notifi.pDate ASC',
'limit'=>10,
'params' => array(':userid' => $myid)
));
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',
),
),
)); ?>