【问题标题】:How to create query builder was failed in CakePHP 3.7如何在 CakePHP 3.7 中创建查询构建器失败
【发布时间】:2019-07-24 20:51:26
【问题描述】:

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

【问题讨论】:

    标签: cakephp-3.7


    【解决方案1】:

    你还需要引用控制器的型号

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

    【讨论】:

    • 也仅供参考,如果您还不知道,= 登录密钥是可选的,['Nama' =&gt; 'Budi'] 也可以。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-09
      • 2015-01-09
      • 2015-08-16
      • 1970-01-01
      相关资源
      最近更新 更多