Hi, I have a criteria which involves a master table and two related tables, so I defined my relationships using self::BELONGS_TO. I define the $criteria->with() with the proper relations, and launch the findAll. The SQL looks fine and if I run it with, say phpMyAdmin, it returns every record (10,000+ records in this case), but findAll returns only the first one!!. But if I delete the $criteria->with, it returns every record, but it fires up 10,000+ queries to find the related data. I really don’t understand what could be wrong:
master (Log) model relations:
public function relations()
{
return array(
'users'=>array(self::BELONGS_TO, 'Users', 'uid'),
'profile'=>array(self::BELONGS_TO, 'Profile', 'uid'),
);
}
$criteria = new CdbCriteria();
$criteria->select = 'info';
$criteria->condition = 't.uid=:uid';
$criteria->params = array(':uid'=>$model->uid);
$criteria->addBetweenCondition('date', $model->start_date, $model->final_date);
$criteria->order = 'date ASC';
$criteria->with = array(
'profile'=>array(
'select'=>'name',
),
'users'=>array(
'select'=>'email,login',
),
);
$records = Log::model()->findAll($criteria); // this returns ONE record, but the SQL created by
// Yii returns all 10,000 records if run directly in mysql.
$criteria = new CdbCriteria();
$criteria->select = 'info';
$criteria->condition = 't.uid=:uid';
$criteria->params = array(':uid'=>$model->uid);
$criteria->addBetweenCondition('date', $model->start_date, $model->final_date);
$criteria->order = 'date ASC';
$records = Log::model()->findAll($criteria); // this returns all 10,000 records but executes
// 10,000 queries when I loop through $records to get the information.
Any help is appreciated.