CGridView with linked models and in condition

I have two models, loading data from different tables of the same database. I’ll call one A and the other B. I’ve linked the two models using the relations() methods, such that A belongs to B and B has many A.

I’m now trying to display data from both models in the same table using CGridView with a CActiveDataProvider. My CActiveDataProvider has the following criteria:

$criteria->select = '*';

$criteria->with = array('B');

I’m currently attempting to display just the ID from model A and the Name from model B. My view contains the following:

$this->widget('zii.widgets.grid.CGridView', array(

	'dataProvider' => $dataProvider,

	'enableSorting' => false,

	'columns' => array(





This works fine and displays two columns, one with the IDs from model A, the other with the Names from model B. However, I need to display only those rows whose ID is in a given list. To do this I added a simple InCondition to my criteria, making the full criteria as below:

$criteria->select = '*';

$criteria->with = array('B');

$criteria->addInCondition('t.ID', $allowedIDs);

When I do this, I get all of the IDs filtered and listed correctly, but the Names are all blank. Putting var_dump($data->B ) as the value of a column shows as null in every row. However, I get no errors so that must still be referencing something (if I change B to C then I get the error "A.C is not defined"). It is as if adding that InCondition has caused all of the data from B to become null.

Is this a bug or expected behaviour? Either way, does anyone know of a workaround/am I doing something wrong here?

Nevermind, just found the solution. :rolleyes: It turns out that my relationships weren’t quite right. I gave the column name of model A in A’s relationship method, and the column name of model B in B’s relationship method, whereas what I needed was for both to have the column name of model B.

When I ran it without the InCondition, it looked like it was working because I had data for both, but as it happens the Names were not the correct ones. I’ve sorted the relationships out and now I get correct data in both columns, even with the InCondition being used.