简体   繁体   English

如何在CakePHP 3.2中设置条件?

[英]How to put condition in CakePHP 3.2?

How do I put conditions in a query when fetching from the database in CakePHP 3.2. 从CakePHP 3.2中的数据库中获取数据时,如何在查询中添加条件。 Here I have implemented a pagination and it is easy to pass WHERE conditions (See code below). 在这里,我实现了分页,并且很容易传递WHERE条件(请参见下面的代码)。

public $paginate = [
        'fields' => ['Users.id', 'Users.email', 'Users.name', 'Users.is_active'],
        'limit' => 1,
        'conditions'=>['Users.is_active' =>1],
        'order' => [
            'Users.id' => 'asc'
        ]
    ];

public function index() {
        $users = $this->paginate($this->Users);
        $this->set(compact('users'));
    }

In above code I have used pagination, so it's easy for me to pass conditions and other things like fields, order etc. 在上面的代码中,我使用了分页,因此我很容易传递条件和其他诸如字段,订单等内容。

If the pagination is not present, then what will be the code? 如果不存在分页,那么代码是什么? Here I only have to fetch the data from the database, then give conditions and other things. 在这里,我只需要从数据库中获取数据,然后给出条件和其他内容即可。 In CakePHP 2.6 the code would be like below. 在CakePHP 2.6中,代码如下所示。

  $this->EventCategory->find('all', array('conditions' => array('EventCategory.is_active' => 1), 'fields' => array('EventCategory.id', 'EventCategory.name')));

Is there any other way that I can do it like above (without pagination) in CakePHP 3.2? 在CakePHP 3.2中,还有其他方法可以像上面一样(没有分页)吗?

Any proposals are welcome. 欢迎任何建议。 Thank you in advance. 先感谢您。

Please read the docs http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html 请阅读文档http://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html

$query = $this->EventCategory->find('all')
    ->select(['EventCategory.id', 'EventCategory.name'])
    ->where(['EventCategory.is_active' => 1]);

use this code in controller 在控制器中使用此代码

$con=[];
$con['Users.role_id'] = 3;

  if (isset($this->request->query['keyword']) && !empty($this->request->query['keyword'])) {
            $keyword = trim($this->request->query['keyword']);
            $con['OR'] = ['Users.phone LIKE' => '%' . $keyword . '%', 'Users.name LIKE' => '%' . $keyword . '%', 'ContractorDetails.company_name LIKE' => '%' . $keyword . '%', 'ProjectTypes.name LIKE' => '%' . $keyword . '%'];
        }

 $this->paginate = [
            'sortWhitelist' => ['ContractorDetails.company_name', 'ProjectTypes.name', 'Users.name', 'Users.id', 'Users.phone', 'Users.deleted', 'Users.probationary'],
            'contain' => ['ContractorDetails.ProjectTypes'],
            'conditions' => $con,
            'order' => ['Users.id' => 'desc'],
            'limit' => (isset($this->request->query['limit'])) ? $this->request->query : 10,
        ];
        $users = $this->paginate($this->Users);

        $this->set(compact('users'));

user this in view.ctp 在view.ctp中使用此用户

use Cake\Core\Configure;
use Cake\I18n\Number;
    <div class="row">
                <div class="col-sm-6">
                    <div class="dataTables_info" id="editable_info" role="status" aria-live="polite"><?= $this->Paginator->counter(['format' => 'Showing {{start}} to {{end}} of {{count}} contractors']) ?></div>
                </div>
                <?php if ($this->Paginator->counter(['format' => '{{pages}}']) > 1) { ?>
                    <div class="col-sm-6">
                        <div class="dataTables_paginate paging_bootstrap" id="editable_paginate">
                            <ul class="pagination pull-right">
                                <?= $this->Paginator->prev('Previous') ?>
                                <?= $this->Paginator->numbers() ?>
                                <?= $this->Paginator->next('Next') ?>
                            </ul>
                        </div>
                    </div>
                <?php } ?>
            </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM