簡體   English   中英

如何在CakePHP 3.2中設置條件?

[英]How to put condition in CakePHP 3.2?

從CakePHP 3.2中的數據庫中獲取數據時,如何在查詢中添加條件。 在這里,我實現了分頁,並且很容易傳遞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'));
    }

在上面的代碼中,我使用了分頁,因此我很容易傳遞條件和其他諸如字段,訂單等內容。

如果不存在分頁,那么代碼是什么? 在這里,我只需要從數據庫中獲取數據,然后給出條件和其他內容即可。 在CakePHP 2.6中,代碼如下所示。

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

在CakePHP 3.2中,還有其他方法可以像上面一樣(沒有分頁)嗎?

歡迎任何建議。 先感謝您。

請閱讀文檔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]);

在控制器中使用此代碼

$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'));

在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