Hi all!
I have three models: Item, Operation, User.
In DB:
table Item
id
table Operation
id
item_id
manager_id
I have error when trying search manager by login:
“Column not found: 1054 Unknown column ‘managers.login’ in ‘where clause’. The SQL statement executed was: SELECT COUNT(*) FROM item
item
WHERE managers.login=:ycp0. Bound with :ycp0=‘alexe’”
I want to bring in the CGridView all users of item
Item may has one or more operations, and operation may has one or more users.
models/Item.php
<?php
class Item extends CActiveRecord
{
public static function model($className = __CLASS__)
{
return parent::model($className);
}
public function tableName()
{
return 'item';
}
public function rules()
{
return array(
array('internal_id', 'required'),
array('status', 'numerical', 'integerOnly' => true),
array('task_id, currency', 'length', 'max' => 20),
array('internal_id, title', 'length', 'max' => 1024),
array('price', 'length', 'max' => 14),
array('id, date_create, task_id, status, internal_id, title, price, currency, url, note, source', 'safe', 'on' => 'search'),
);
}
public function relations()
{
return array(
'operations' => array(self::HAS_MANY, 'Operation', 'item_id'),
'currency' => array(self::BELONGS_TO, 'Currency', 'currency_id'),
'managers' => array(self::HAS_MANY, 'User', array('manager_id' => 'id'),
'select' => 'id, login, first_name, last_name', 'through' => 'operations'
),
);
}
public function search()
{
$criteria = new CDbCriteria();
$criteria->compare('id', $this->id, true);;
$criteria->compare('title', $this->title, true);
$criteria->compare('price', $this->price, true);
$criteria->compare('currency', $this->currency, true);
$criteria->compare('url', $this->url, true);
$criteria->compare('note', $this->note, true);
$criteria->compare('source', $this->source, true);
$criteria->compare('managers', $this->managers, true);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
'sort' => array(
'defaultOrder' => 'id DESC',
),
));
}
public function getManagersByItemId()
{
$res = array();
foreach ($this->managers as $manager) {
$res[] = $manager->getLink();
}
return implode("<br>", $res);
}
}
models/Operation.php
public function relations()
{
return array(
'manager' => array(self::BELONGS_TO, 'User', 'manager_id'),
);
}
views/items/all.php
<?php $this->widget('ext.bootstrap.widgets.BootGridView', array(
'id' => 'item-grid',
'dataProvider' => $model->search(),
'filter' => $model,
'columns' => array(
'id',
'title',
array(
'name' => 'managers',
'header' => 'Managers',
'type' => 'raw',
'value' => '$data->getManagersByItemId()',
),
),
)); ?>
But it’s not working and I want have filter and sorting by manager login in the table.
Where are my mistake?