1

I want to have separate row for each related object in CGridView.

ModelB has modelAId, so it is HAS_MANY relationship.

The following query returns what I am trying to get:

select * from modelA a
join modelB b on b.modelAId = a.id

Here I am getting separate rows for each corresponding modelB row, records from modelA may be duplicated.

However, The following provider is not returning expected records, how can I make use of this in CActiveDataProvider

$provider = new CActiveDataProvider ("ModelA", array ("criteria" => array (
            "with" => array("ModelB")
            )
        ));

If I add join "join" => " join modelB b on b.modelAId = t.id", and remove with() it is giving correct records, but when I include with() it only gives modelA records.

What is the correct way of getting data from ModelA INCLUDING separate rows for ModelB relation?

Jamol
  • 3,768
  • 8
  • 45
  • 68
  • How are your relations defined in the models? It's most likely not correct. You can also use a join in your criteria. – chris--- Aug 18 '14 at 09:59

1 Answers1

0

You can use cDbCriteria to that in ultimate easy fashion

$criteria=new cDbCriteria();
$criteria->with=array('ToModelB'); //Here ToModelB is defined relation in ModelA to ModelB
$dataProvider=new CActiveDataProvider('ModelA',array('criteria'=>$criteria));

But before that make sure you have proper relations defined for these two models.

anwerj
  • 2,386
  • 2
  • 17
  • 36
  • I am also using Criteria, but this way I can access all modelB objects like this: $provider->data->modelB. and in CGridView I cannot have separate row for each modelB objects. This is what I want to achieve: in CGridView, it should be possible to have modelB relation records, For example, for each modelA object I may have several modelB objects, so they all should be in separate rows – Jamol Aug 18 '14 at 10:28