how to build query like `select … where id % 2 = 1` using Yii 2 ?
问题 Surely, I can Yii::$app->db->createCommand($sql)->query() , but what if I wanna use a ActiveRecord::find()->where($conditions) to do this job ? 回答1: Here is one of the options with using yii\db\Expression : use yii\db\Expression; ... $models = Customer::find() ->select(['id', 'name', ...]) ->where(new Expression('id % 2 = 1')]) ->all(); It's definetely better than raw sql and ['%2=', 'id', 1] because it follows the order and more readable in my opinion. ['%2=', 'id', 1] also is not suitable