0

我在 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 |
-------------------------------------------------
4

2 回答 2

1

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

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

于 2019-07-24T12:51:26.660 回答
0

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

$users = TableRegistry::get('Users');
于 2019-04-12T10:24:45.907 回答