How To Access A Related Tables Relations?

I have three mysql tables, collector, company, and reproduction. The reproduction table is related to the collector table through the foreign key of collectorID. The collector table is related to the company table through the foreign key companyID.

I need to access the company name for the collector from inside the Reproduction model. I can easily access the collector’s name from



foreach ($repro as record) echo $record->collector->name

How do I now carry the collector table’s relation over so I can access the company’s name from the company table?

Hi Joev you need can specify a through association take a look at this

Thanks. I’m not sure how to access the record. My model relations function now looks like

return array(

	'collector' => array(self::BELONGS_TO, 'Collector', 'collectorID'),

	'art' => array(self::BELONGS_TO, 'Art', 'artID'),

	'reproductionStatus' => array(self::BELONGS_TO, 'ReproductionStatus', 'reproductionStatusID'),



and in my view file I have


foreach ($repro as $record)

	echo $record->collector->name.': '.$record->company->name.'<br />'; 

but I get a Trying to get property of non-object error.

What am I doing wrong?

You probably have the order wrong try this



// this should be companies since you have has_many

'companies'=>[self::HAS_MANY, 'Company', ['companyID'=>'collectorID'], 'through'=>'collector']


The order was right but the relation was wrong. Each collector can only have one company (brain freeze moment) so I changed my relation to



and it worked.

Thanks for all your help.