简体   繁体   English

CakePHP发布查询后调试的奥秘

[英]CakePHP Post Query Debug Mystery

I need some help understanding how to debug in cake and where an error may be coming from. 我需要一些帮助,以了解如何在cake中进行调试以及错误可能来自何处。

I love how well cake is organized. 我喜欢蛋糕的组织方式。 Until now, it's been relatively easy for me to pinpoint what function to go to when there was an error. 到现在为止,我一直很容易找出出现错误时要去的功能。
I have a model called “projects” (of course it has a controller and views as well). 我有一个称为“项目”的模型(当然它也有一个控制器和视图)。

My problem is when clicking the submit button in a view called “add”. 我的问题是在名为“添加”的视图中单击提交按钮。 I know that the post back calls the function “add” in the “projects_controller.php” but I cannot find any code in that function that is causing the problem. 我知道回发在“ projects_controller.php”中调用函数“ add”,但是我找不到该函数中导致问题的任何代码。 (I think the problem may be in the super class “AppController”.) (我认为问题可能出在“ AppController”超类中。)

In my "add" function, after the data gets submitted from the form, I added this 在我的“添加”功能中,从表单提交数据后,我添加了此内容

$log = $this->Project->getDataSource()->getLog(false, false);  
debug($log);

to show the SQL statements. 显示SQL语句。 Here is the problem: 这是问题所在:

[49] => Array
    (
        [query] => loadModel
        [error] => 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 'loadModel' at line 1
        [affected] => 
        [numRows] => 
        [took] => 0
    )

Obviously not a valid MySQL query! 显然不是有效的MySQL查询! But I'm not sure why it's doing that. 但是我不确定为什么要这么做。

In my App Controller I did add some code which included: $this->loadModel('User'); 在我的App Controller中,我确实添加了一些代码,其中包括: $this->loadModel('User'); . But I'm not sure, if that was it, or why it would come up as a query of itself. 但是我不确定,是否是它,或者它为什么会作为对自身的查询而出现。 Here are the changes (in Git) I made to the AppController (which involved adding that line): 这是我对AppController所做的更改(在Git中)(涉及添加该行):

function _canAccessProjectAssets($projectId = null) {
        if (isset($projectId)) {

                if ($this->_isProjectPrivate($projectId) == false) {
                        return true;
                } elseif ($this->_isLoggedIn() == false) {
                        return false;
                } elseif ($this->_isInGroup('Admins') == true) {
                        return true;
                } else {
                        $this->loadModel('Project');

                        $project = $this->Project->find('first', array(
                                'conditions' => array(
                                        'Project.id' => $projectId
                                ),
                                'fields' => array(
                                        'applicant_id',
                                        'id',
                                        'private'
                                ),
                                'contain' => array(
                                        'Department' => array(
                                                'fields' => array(
                                                        'id',
                                                )
                                        ),
                                        'ProjectType' => array(
                                                'fields'  => array(
                                                        'id',
                                                        'department_id',
                                                        'name'
                                                )
                                        )
                                )
                        ));

                        if ($this->Session->read('Auth.User.id') == $project['Project']['applicant_id']) {
                                return true;
                        } else {
                                $departments = Set::extract('/Department/id', $project);
                                if (!in_array($project['ProjectType']['department_id'], $departments)) { array_push($departments, $project['ProjectType']['department_id']); }
                                $id = $this->Session->read('Auth.User.id');

+                       $id = $this->Session->read('Auth.User.id');
+                       $this->loadModel('User');
+                       $userLoggedIn = $this->User->find('first', array(
+                               'recursive' => 1,
+                               'conditions' => array(
+                                       'User.id' => $id
+                               ),
+                               'fields' => array(
+                                       'id'
+                               ),
+                               'contain' => array(
+                                       'Department' => array(
+                                               'fields' => array(
+                                                       'id',
+                                                       'name'
+                                               )
+                                       ),
+                               )
+                       ));
+
+                       $sameDepartment = false;
+                       foreach($departments as $dept)
+                       {
+                           foreach($userLoggedIn['Department'] as $departmentLoggedIn)
+                           {
+                               if($dept == $departmentLoggedIn['id'])
+                               {
+                                       $sameDepartment = true;
+                               }
+                           }
+                       }
+
+                       return $sameDepartment;

and

        function _getUserJsonLog() {
+           $id = $this->Session->read('Auth.User.id');
+           $this->loadModel('User');
+           $user = $this->User->find('first', array(
+                                'recursive' => 1,
+                                'conditions' => array(
+                                        'User.id' => $id
+                                ),
+                                'fields' => array(
+                                        'id'
+                                ),
+                                'contain' => array(
+                                        'Department' => array(
+                                                'fields' => array(
+                                                        'id',
+                                                        'name'
+                                                )
+                                        ),
+                                )
+                        ));
+
+           $departmentList = "";
+           $deptCount = 0;
+           foreach($user['Department'] as $department)
+           {
+               if($deptCount > 0)
+               {
+                   $departmentList = $departmentList . ", ";
+               }
+               $departmentList = $departmentList . $department['id'];
+               $deptCount++;
+           }
+

I've even commented out the loadModel lines; 我什至已经注释掉了loadModel行; and the error remains (so maybe the above additions weren't the problem). 并且错误仍然存​​在(因此,上述问题可能不是问题所在)。 Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

Sounds like you are invoking loadModel() on a model object somewhere, this is exactly what would happen in that case, as the models magic __call handler will execute all calls to non-existing methods as SQL queries. 听起来就像您在某个地方的模型对象上调用loadModel() ,这正是在这种情况下会发生的情况,因为模型魔术__call处理程序将执行对不存在方法的所有调用,作为SQL查询。

https://github.com/cakephp/cakephp/blob/2.6.1/lib/Cake/Model/Model.php#L827 https://github.com/cakephp/cakephp/blob/2.6.1/lib/Cake/Model/Model.php#L827

Enable debug mode (set it to 2 ) and check the stacktrace that will now appear when the error is triggered, this should reveal where exactly loadModel() is being invoked wrongly. 启用调试模式 (将其设置为2 ),并检查在触发错误时现在将显示的loadModel() ,这应该可以显示在哪里错误地调用了loadModel()

usually, if this error happens, you don't have the model instance, but an app model instance you work on. 通常,如果发生此错误,则您没有模型实例,但是您正在使用的应用程序模型实例。 the app model instance doesnt have the add() method and directly queries the db with add(). 应用程序模型实例没有add()方法,而是直接使用add()查询数据库。

so make sure your model is properly included. 因此,请确保正确包含您的模型。 since you didnt show us the code how you call the method (and how you make the model available to the controller) I cannot offer any concrete advice, though. 由于您没有向我们展示代码,因此您如何调用方法(以及如何使模型对控制器可用),但是我无法提供任何具体建议。

if you manually include it: 如果您手动包含它:

$this->ModelName = ClassRegistry::init('ModelName');

May be your problem like this post , Error: SQLSTATE[42000]: Syntax error or access violation with cakePHP 可能是您这样的问题, 错误:SQLSTATE [42000]:语法错误或cakePHP的访问冲突

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM