findAll using relations and not SQL statement

It will not be easy, but I will try:

I have 3 models:

  • Company (Foreign Key: ownerId)

  • Owner

  • Cooperation

and relations:

Company:

  - relCompanyOwner => array(self::BELONGS_TO, 'Owner', 'ownerId', 'alias'=>'relCompanyOwner')

Owner:

  - relOwnerCompany => array(self::HAS_ONE, 'Company', 'ownerId', 'alias'=>'relOwnerCompany')

  - relOwnersCooperations => array(self::MANY_MANY, 'Cooperation', 'owner_cooperation(ownerId, cooperationId)', 'alias'=>'relOwnersCooperations')

Cooperation:

  - relCooperationsOwners => array(self::MANY_MANY,  'Owner', 'owner_cooperation(cooperationId, ownerId)', 'alias'=>'relCooperationsOwners')

How we see the owner belongs to some cooperations , not the company, but the company belongs to some owner.

Now let's say I have Company with id=15. The company belongs to some cooperations (using 'relCompanyOwner->relOwnersCooperations' relation). I want to show all companies that belongs to the same cooperations.

I make it like this:

$sql = "ownerId IN (SELECT DISTINCT owenrId FROM owner_cooperation WHERE cooperationId IN (SELECT coopearationId FROM owner_cooperation WHERE ownerId = '15'))";

$companies = Company::model()->findAll($sql);

Is there other way to do it, using my relations and not SQL statement.

You may want to check AR's with() method:

http://www.yiiframew…ord#with-detail

Quote

You may want to check AR's with() method:

http://www.yiiframew…ord#with-detail

I think it is what I need, but:

  • it works when I use only one relation,

  • it works when I use BELONGS_TO relation

  • it does not work with MANY_MANY relations - maybe I make sth wrong.

  • I do not know how should I use it with cascade relations.

Could anybody show me how should look the array for with() method like.