Blake Miller Tue Oct 02 10:42:57 -0400 2012

Subject: Problem with association based on a condition

I have the following query, which pulls back locations, but ONLY those where the location is in the state I pass in:

return Company::find("all", array('joins' => array('INNER JOIN eniloncompanylocations loc ON (loc.eniloncompanyid = eniloncompanies.id)'),
'include' => array('locations'),
'order' => 'eniloncompanies.name ASC',
'conditions' => array("loc.stateid = ?", $stateid)));

The problem is, when I access the locations collection from one of the companies, as so:
$company->locations

...it disregards the stateid condition, and returns all those locations related to the company. How can I write the query so that, when the locations collection is accessed, it's only those in the state specified in the query?