在 CakePHP 3 中生成 OR SQL 语句
Generate an OR SQL statement in CakePHP 3
我正在将一些发现从 cakephp2 转换为 cakephp3。
我需要搜索存储在不同列中的导师 table 的名字和姓氏。根据文档,我需要在 table 之间进行 LEFT 连接。我在 2 个字段中使用了 or 条件,但如果两个参数都有值,它的工作方式类似于 'and' 条件。
我的问题是
q1) I cant get the data with just the first name only , and the surname is null.
q2) I need to pass both first name and surname to get just those data with that name.
Not sure how to do this in cakephp3.
eg $a5='fred'; //just want all first names like fred
$a6=null; //sometimes this will be filled
$a3='2015-05-30';
$a4='2016-06-01';
$query3 = $this->Lessons->find()
->contain(['Tutors'])
->select(['lessons.id','lessons.lesson_date','tutors.id','tutors.first_name','tutors.last_name' ])
->where(['Lessons.lesson_date >' => $a3,'Lessons.lesson_date <' => $a4,
'OR' => [['tutors.first_name like' => '%'.$a5.'%'], ['tutors.last_name like' => '%'.$a6.'%']],
]);
foreach ( $query3 as $row) {
debug($row->toArray());
}
关于这一点我不明白文档。
http://book.cakephp.org/3.0/en/orm/query-builder.html#advanced-conditions
UPDATE- 试过这个,这也只给出了所有带有 'at' 或 'to' 的数据,但它应该是任何名称同时带有 'at' 和 'to' 的他们。
$query3 = $this->Lessons->find()
->contain(['Tutors','Subjects', 'TutoringTypes','Terms','Students'])
->select(['lessons.id','lessons.lesson_date','tutors.id','tutors.first_name','tutors.last_name',
'subjects.id','subjects.name','terms.id','terms.title'])
->where(['Lessons.lesson_date >' => $a3,'Lessons.lesson_date <' => $a4])
->orWhere(function ($exp) {
return $exp->and_([
'tutors.first_name like' => '%an%',
'tutors.last_name like' => '%to%',
]);
});
注意生成的SQL。您要么在 DebugKit 中看到它,要么通过调用 $query->sql()
来调试它。您构建的查询有误:
您生成 OR ((... AND ...))
是因为您使用的是 and_
。你可能想要 ... OR ...
.
->orWhere(function ($exp) {
return $exp->and_([
'tutors.first_name like' => '%an%',
'tutors.last_name like' => '%to%',
]);
});
你可能想要 OR
。但是将数组直接传递给 orWhere()
也可以。
我正在将一些发现从 cakephp2 转换为 cakephp3。 我需要搜索存储在不同列中的导师 table 的名字和姓氏。根据文档,我需要在 table 之间进行 LEFT 连接。我在 2 个字段中使用了 or 条件,但如果两个参数都有值,它的工作方式类似于 'and' 条件。 我的问题是
q1) I cant get the data with just the first name only , and the surname is null.
q2) I need to pass both first name and surname to get just those data with that name.
Not sure how to do this in cakephp3.
eg $a5='fred'; //just want all first names like fred
$a6=null; //sometimes this will be filled
$a3='2015-05-30';
$a4='2016-06-01';
$query3 = $this->Lessons->find()
->contain(['Tutors'])
->select(['lessons.id','lessons.lesson_date','tutors.id','tutors.first_name','tutors.last_name' ])
->where(['Lessons.lesson_date >' => $a3,'Lessons.lesson_date <' => $a4,
'OR' => [['tutors.first_name like' => '%'.$a5.'%'], ['tutors.last_name like' => '%'.$a6.'%']],
]);
foreach ( $query3 as $row) {
debug($row->toArray());
}
关于这一点我不明白文档。 http://book.cakephp.org/3.0/en/orm/query-builder.html#advanced-conditions
UPDATE- 试过这个,这也只给出了所有带有 'at' 或 'to' 的数据,但它应该是任何名称同时带有 'at' 和 'to' 的他们。
$query3 = $this->Lessons->find()
->contain(['Tutors','Subjects', 'TutoringTypes','Terms','Students'])
->select(['lessons.id','lessons.lesson_date','tutors.id','tutors.first_name','tutors.last_name',
'subjects.id','subjects.name','terms.id','terms.title'])
->where(['Lessons.lesson_date >' => $a3,'Lessons.lesson_date <' => $a4])
->orWhere(function ($exp) {
return $exp->and_([
'tutors.first_name like' => '%an%',
'tutors.last_name like' => '%to%',
]);
});
注意生成的SQL。您要么在 DebugKit 中看到它,要么通过调用 $query->sql()
来调试它。您构建的查询有误:
您生成 OR ((... AND ...))
是因为您使用的是 and_
。你可能想要 ... OR ...
.
->orWhere(function ($exp) {
return $exp->and_([
'tutors.first_name like' => '%an%',
'tutors.last_name like' => '%to%',
]);
});
你可能想要 OR
。但是将数组直接传递给 orWhere()
也可以。