Findallbyattributes With Related Model

I’m trying to do a findAllByAtrributes using a related model column as one of the criteria, but I keep getting a CDbException stating the column cannot be found.

Here’s my model Relationship:




public function relations() {

  return array(

    'MetaData' => array(self::BELONGS_TO, 'ProjectMeta', 'wbse_or_io'),

  );

}



And here’s my attempted query:




$listing = ProjectIndex::model()->with('MetaData')

  ->findAllByAttributes(array(

    'report_date'=>$reportDate,

    'MetaData.cost_centre'=>$costCentre

  )

);



From what I’ve read through Google/StackOverflow/these forums, I should be able to reference the cost_centre column in the MetaData relationship. But I keep getting the following error:


Table "tbl_project_index" does not have a column named "MetaData.cost_centre"

How do I reference the related table column?

I believe findAllByAttributes() only matches columns in the table representing the main active record model. This may work better:




$criteria = new CDbCriteria;

$criteria->with = array('MetaData');

$criteria->addColumnCondition(array(

    't.report_date'=>$reportDate,

    'MetaData.cost_centre'=>$costCentre,

));

$listing = ProjectIndex::model()->findAll($criteria);



Spectacular! That did the trick perfectly! Thanks very much. :D