【问题标题】:how to make a dynamic query using cakephp 3?如何使用 cakephp 3 进行动态查询?
【发布时间】:2016-09-13 10:22:21
【问题描述】:

我有一个过滤器可以从中搜索数据库。但是,如果任何过滤器字段为空,则应忽略。

$name = $this->request->data['name'];
$status = $this->request->data['status'];
$code_transaction = $this->request->data['code_transaction'];

$orders = $this->Orders->find('all')->where(['name'=> $name, 'status'=> $status, 'id'=> $code_transaction]);

当你后面有一个空白字段时,不要查询任何数据库。 当您第一次填写字段并忽略那些空白的字段时,我需要帮助来创建查询以在数据库中执行查询。谁能帮助我?我接受任何创建它的想法。

【问题讨论】:

  • 删除一些 if's 并构建你的数组

标签: php jquery mysql cakephp-3.0


【解决方案1】:

你可以这样做:

$query = ['name'=> $name, 'status'=> $status, 'id'=> $code_transaction];
$newdataquery =[];
foreach($query as $key => $data) {
 if (strlen($data) > 0 )
     $newdataquery[$key] = $data;
}
$orders = $this->Orders->find('all')->where($newdataquery);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-15
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-28
    相关资源
    最近更新 更多