Yii2写自定义条件

时间:2015-12-07 07:05:35

标签: yii2

我有类似的东西

SELECT * FROM `Project` WHERE `personincharge` REGEXP "(^|,)2(,|$)"

我该如何编写自定义

class Meal < ActiveRecord::Base
  has_many :meal_ingredients
  has_many :ingredients, through: :meal_ingredients
  accepts_nested_attributes_for :ingredients, :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :meal_ingredients
end

class Ingredient < ActiveRecord::Base
  has_many :meal_ingredients
  has_many :meals, through: :meal_ingredients
end

class MealIngredient < ActiveRecord::Base
  belongs_to :meal
  belongs_to :ingredient
end

2 个答案:

答案 0 :(得分:0)

你可以通过findBySql这样轻松地做到这一点

$sql = 'SELECT * FROM `Project` WHERE `personincharge` REGEXP "(^|,)2(,|$)";';


$model = Project::findBySql($sql)->all();  

答案 1 :(得分:0)

您可以使用Yii2 createcommand方法执行任何普通的SQL查询,如下所示:

use yii\db\Query;
$connection = \Yii::$app->db;

$model = $connection->createCommand('SSELECT * FROM `Project` WHERE `personincharge` REGEXP "(^|,)2(,|$)"');
$projects = $model->queryAll();