CakePHP 3.7中如何创建查询生成器失败

时间:2019-04-12 10:19:32

标签: cakephp-3.7

我正在CakePHP 3.7中设置查询生成器,以在UsersTable中的单行中获取值。我想获取行值“其中Name ='Budi'”

我试图通过在https://book.cakephp.org/3.0/en/orm/query-builder.html中浏览来解决此问题,但不起作用

<?php
//UsersController.php

namespace App\Controller;

use App\Controller\AppController;
use App\Model\Table\UsersTable;
use Cake\Event\event;
use Cake\I18n\Time;
use Cake\ORM\TableRegistry;

class UsersController extends AppController
{
  public function index
  {
    $query = $users->find()
        ->select(['ID', 'Nama'])
        ->where(['Nama =' => 'Budi']);


        foreach ($query as $users) {
            debug($users->Nama);
        }

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

  //code from cake bake all
}
?>

此处显示的消息错误

Notice (8): Undefined variable: users [APP/Controller\UsersController.php, line 20]

Notice (1024): Undefined property: ErrorController::$Auth in C:\xampp\htdocs\klinikucing\src\Controller\AppController.php on line 57 [CORE\src\Controller\Controller.php, line 388]

Warning (512): Unable to emit headers. Headers sent in file=C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php line=853 [CORE\src\Http\ResponseEmitter.php, line 48]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 148]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 181]

Warning (2): Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\klinikucing\vendor\cakephp\cakephp\src\Error\Debugger.php:853) [CORE\src\Http\ResponseEmitter.php, line 181]

An Internal Server Error Occurred

我希望输出是

-------------------------------------------------
| Nama    |   Role   | Dibuat      |  Diubah    |
------------------------------------------------- 
| Budi    |  Visitor | 10/04/2019  | 12/04/2019 |
-------------------------------------------------

2 个答案:

答案 0 :(得分:0)

在索引函数的第一行,我认为您必须定义$users变量

$users = TableRegistry::get('Users');

答案 1 :(得分:0)

您还需要参考控制器的型号

使用 $query = $this->Users->find() ->select(['ID', 'Nama']) ->where(['Nama =' => 'Budi']);