用户
ID | customerID | <others>
userTemplates
ID | userID | productCode
模板
ID | customerID | productCode | <others>
我试图找到一个很好的解决方案,根据 userTemplates 表中的 prodCode 获取分配给给定用户的所有模板。
产品代码需要包含一个外卡%,所以就像这个P.0100。%或%。0100。%
到目前为止我已经这样做了:
$usersTemplates = ArrayHelper::map(userTemplates::find()->where("userID = ".Yii::$app->user->identity->ID."")->all(),'ID','productCode');
$templates = new Query;
$templates->from('templates')
->where("customerID = ".Yii::$app->user->identity->customer->ID."")
->andWhere("disabled = 0")
->andWhere("approved = 1");
foreach($usersTemplates as $prodCode){
$templates->andWhere("productCode LIKE '%".$prodCode."%'");
}
但我最终得到了这个
SELECT * FROM `templates` WHERE ((((customerID = 1) AND (disabled = 0)) AND (approved = 1)) AND (productCode LIKE '%TESTWP%')) AND (productCode LIKE '%P.0100.0001%')
几乎就在那里但是这一点:
AND (productCode LIKE '%TESTWP%')) AND (productCode LIKE '%P.0100.0001%')
需要:
AND ((productCode LIKE '%TESTWP%')) OR (productCode LIKE '%P.0100.0001%'))
我确定可能有更好的方法来获取模板列表。
提前致谢
答案 0 :(得分:1)
当您有复杂查询时,一个简单的解决方案基于findBySql
$sql = "SELECT *
FROM `templates`
WHERE ((((customerID = 1)
AND (disabled = 0))
AND (approved = 1))
AND ((productCode LIKE '%TESTWP%'))
OR (productCode LIKE '%P.0100.0001%'))";
$model = Templates::findBySql($sql)->all();