I’m trying to select records with condition in relation.
ApiOnline relations:
return array(
'user' => array(self::BELONGS_TO, 'User', 'user_id'),
);
User to Car relation:
'car' => [self::BELONGS_TO, 'Car', 'car_id'],
Trying following return array with only first record:
$online_cr = new CDbCriteria();
$online_cr->compare('t.date', ">=$t_date_from");
$online_cr->compare('t.date', "<=$t_date_to");
$online_cr->compare('car.owner_id', $this->carowner->id);
$api_online = ApiOnline::model()->with(['user' => ['with' => ['car']]])->findAll($online_cr);
Select by SQL directly works, returning all records:
$api_online = ApiOnline::model()->findAllBySql('SELECT t.* FROM api_online t '
. 'LEFT JOIN user ON user.user_id = t.user_id '
. 'LEFT JOIN car ON car.car_id = user.car_id '
. 'WHERE t.date BETWEEN :date_from AND :date_to AND car.owner_id=:owner_id;', [
'date_from' => $t_date_from,
'date_to' => $t_date_to,
'owner_id' => $this->carowner->id,
]);
It’s a Yii bug or my mistake?