CakePHP 2 - 为什么我在模型函数中收到 sql 错误?

发布于 2025-01-07 11:14:40 字数 2134 浏览 0 评论 0原文

我试图通过我的控制器和模型进行最简单的调用,但它不起作用。

我得到的错误是:

错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您 SQL 语法有错误;检查对应的手册 您的 MySQL 服务器版本,以便在附近使用正确的语法 第 1 行的“ppLongerFunction2”

控制器代码是:

<?php

class UsersController extends AppController{
public $uses = array('User');

function test(){
    $blah = $this->User->find('all');
    $u = $this->User->ppLongerFunction2();
    $this->set(compact('blah', 'u'));
}
}

模型是:

<?php
class User extends AppModel{
public var $name = 'User';
var $displayField = 'username';

var $useTable = 'users';

public function ppLongerFunction2(){
    $something = $this->find('all');
    return $something;
}
}

我哪里搞砸了?

堆栈跟踪:

#0 C:\wamp\www\lib\Cake\Model\Datasource\DboSource.php(436): PDOStatement->execute(Array)
#1 C:\wamp\www\lib\Cake\Model\Datasource\DboSource.php(403): DboSource->_execute('ppLongerFunctio...', Array)
#2 C:\wamp\www\lib\Cake\Model\Datasource\DboSource.php(647): DboSource->execute('ppLongerFunctio...', Array, Array)
#3 C:\wamp\www\lib\Cake\Model\Datasource\DboSource.php(589): DboSource->fetchAll('ppLongerFunctio...', Array, Array)
#4 C:\wamp\www\lib\Cake\Model\Model.php(720): DboSource->query('ppLongerFunctio...', Array, Object(AppModel))
#5 C:\wamp\www\app\Controller\UsersController.php(11): Model->__call('ppLongerFunctio...', Array)
#6 C:\wamp\www\app\Controller\UsersController.php(11): AppModel->ppLongerFunction2()
#7 [internal function]: UsersController->test()
#8 C:\wamp\www\lib\Cake\Controller\Controller.php(473): ReflectionMethod->invokeArgs(Object(UsersController), Array)
#9 C:\wamp\www\lib\Cake\Routing\Dispatcher.php(104): Controller->invokeAction(Object(CakeRequest))
#10 C:\wamp\www\lib\Cake\Routing\Dispatcher.php(86): Dispatcher->_invoke(Object(UsersController), Object(CakeRequest), Object(CakeResponse))
#11 C:\wamp\www\app\webroot\index.php(96): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#12 {main}

全部修复。这是一个文件名。感谢您的帮助:|愚蠢的

I am trying to do the simplest call through my controller and model, but it doesn't work.

The error I get is:

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You
have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near
'ppLongerFunction2' at line 1

The controller code is:

<?php

class UsersController extends AppController{
public $uses = array('User');

function test(){
    $blah = $this->User->find('all');
    $u = $this->User->ppLongerFunction2();
    $this->set(compact('blah', 'u'));
}
}

And the Model is:

<?php
class User extends AppModel{
public var $name = 'User';
var $displayField = 'username';

var $useTable = 'users';

public function ppLongerFunction2(){
    $something = $this->find('all');
    return $something;
}
}

Where am I messing up?

Stack Trace:

#0 C:\wamp\www\lib\Cake\Model\Datasource\DboSource.php(436): PDOStatement->execute(Array)
#1 C:\wamp\www\lib\Cake\Model\Datasource\DboSource.php(403): DboSource->_execute('ppLongerFunctio...', Array)
#2 C:\wamp\www\lib\Cake\Model\Datasource\DboSource.php(647): DboSource->execute('ppLongerFunctio...', Array, Array)
#3 C:\wamp\www\lib\Cake\Model\Datasource\DboSource.php(589): DboSource->fetchAll('ppLongerFunctio...', Array, Array)
#4 C:\wamp\www\lib\Cake\Model\Model.php(720): DboSource->query('ppLongerFunctio...', Array, Object(AppModel))
#5 C:\wamp\www\app\Controller\UsersController.php(11): Model->__call('ppLongerFunctio...', Array)
#6 C:\wamp\www\app\Controller\UsersController.php(11): AppModel->ppLongerFunction2()
#7 [internal function]: UsersController->test()
#8 C:\wamp\www\lib\Cake\Controller\Controller.php(473): ReflectionMethod->invokeArgs(Object(UsersController), Array)
#9 C:\wamp\www\lib\Cake\Routing\Dispatcher.php(104): Controller->invokeAction(Object(CakeRequest))
#10 C:\wamp\www\lib\Cake\Routing\Dispatcher.php(86): Dispatcher->_invoke(Object(UsersController), Object(CakeRequest), Object(CakeResponse))
#11 C:\wamp\www\app\webroot\index.php(96): Dispatcher->dispatch(Object(CakeRequest), Object(CakeResponse))
#12 {main}

All fixed. It was a file-name. Thanks for the help :| stupid

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野鹿林 2025-01-14 11:14:40

似乎发生的情况是,您的控制器尝试调用模型的 ppLongerFunction2() 但它未在模型中定义,因此它作为 SQL 函数传递。

我相信发生的情况是您在某个地方错过了以下约定,并且找不到模型文件(User.php)(可能是user.php) )。在这种情况下,cake 将在内部从数据库表构建模型,并且显然不会包含该函数。您可以通过在模型中输入一些肯定会导致错误的虚假内容来检查是否是这种情况,例如缺少 ; 并查看是否出现错误。如果没有,则您的文件未被使用。

What seems to be happening is that your controller tries to call the model's ppLongerFunction2() but it isn't defined in the model and therefor it is passed as an SQL function.

What I believe happens is that you've someplace missed following conventions and the file for the Model (User.php) can't be found (perhaps it is user.php). In this case cake will build the model from the DB table internally and it will obviously won't include the function. You can check if this is the case by typing something bogus in your model that would definitely cause an error, like a missing ; and see if the error comes up. If it doesn't your file isn't used.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文