【问题标题】:build a cakephp query with nested ANDs, ORs, and related model conditions使用嵌套 AND、OR 和相关模型条件构建 cakephp 查询
【发布时间】:2016-02-22 03:09:02
【问题描述】:

我正在尝试根据一些嵌套条件以及相关模型的值来检索数据。

例如,下面的伪代码将在Models 表中找到行,其中Models.type 是选定值之一('mk1''mk2'、或'mk3')和Models.style 是其中之一选定的值('new',或'old'),并且相关模型的字段OtherModelsOtherModels.statusactivepending

$query = $this->Models->find()
  ->contain(['OtherModels'])
  ->where(['Models.type' => ['mk1', 'mk2', 'm3'])
  ->andWhere(['Models.style' => ['old', 'new'])
  ->andWhere(['OtherModels.status' => 'active', 'pending'])
  ;
$data = $query->all();

此代码实际上不起作用,但此处仅用于说明目的。我将如何编写工作代码?

【问题讨论】:

  • 'Models.type IN' => ['mk1', 'mk2', 'm3'] 等...

标签: database cakephp orm cakephp-3.0


【解决方案1】:

您没有使用 IN 关键字。

$query = $this->Models->find()
  ->contain(['OtherModels'])
  ->where(['Models.type IN' => ['mk1', 'mk2', 'm3'])
  ->andWhere(['Models.style' => ['old', 'new'])
  ->andWhere(['OtherModels.status' => 'active', 'pending'])
  ;
$data = $query->all();

更多详情http://book.cakephp.org/3.0/en/orm/query-builder.html#automatically-creating-in-clauses

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-03
    • 1970-01-01
    • 2016-12-29
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多