我有3张桌子
他们有这些关系:证书,用户,状态
certificates - user, one -- many
user-status, one -- one
我也有activeQuery:
$this->andWhere(['<', 'created', new Expression("DATE_SUB(NOW(), INTERVAL " . $days . " DAY)")])
->andWhere(['is_active' => 1])
->andWhere(['<', 'delayed_until', new Expression("NOW()")])
->andWhere(['<', 'resend_count', LeCertificate::RESEND_COUNT])
是否可以添加范围->andWhere
,我可以获得status.not_deleted = 0
当前证书?
答案 0 :(得分:1)
假设您已在LeCerificate
和User
类中定义了正确的关系:
LeCertificate::find()->alias('C')
->joinWith(['users', 'users.status S'], false, 'INNER JOIN')
->andWhere(['<', 'C.created', new Expression("DATE_SUB(NOW(), INTERVAL " . $days . " DAY)")])
->andWhere(['C.is_active' => 1])
->andWhere(['<', 'C.delayed_until', new Expression("NOW()")])
->andWhere(['<', 'C.resend_count', LeCertificate::RESEND_COUNT])
->andWhere(['S.not_deleted' => 0]);
关系应如下所示:
class LeCertificate extends ActiveRecord
{
...
public function getUsers()
{
return $this->hasMany(User::className(), ['certificate_id' => 'id']);
}
...
}
和
class User extends ActiveRecord
{
...
public function getStatus()
{
return $this->hasOne(Status::className(), ['id' => 'status_id']);
}
...
}