HAS_MANY relation in CGridView

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?

Please show us your getManagersByItemId() function.




public function getManagersByItemId()

        {

                $res = array();

                foreach ($this->managers as $manager) {

                        $res[] = $manager->getLink();

                }

                return implode("<br>", $res);

        }



This function get formated links for view.

And function getLink() in User model:




public function getLink()

	{

		return CHtml::link($this->login, array('user/view', 'id' => $this->id),

			array(

				'target' => '_blank',

				'title' => $this->first_name . ' ' . $this->last_name,

				'rel' => 'tooltip',

			)

		);

	}



I think your problem comes from your models declaration:

if a User can have many items an an Item can have many Users, then the relations are MANY_MANY

Have a look here to see how to deal with that. (operation table, will then not be associated to an Active Record Class)