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.