Reputation: 8609
I need some help understanding how to debug in cake and where an error may be coming from.
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. (I think the problem may be in the super class “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. 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! But I’m not sure why it’s doing that.
In my App Controller I did add some code which included: $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):
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; and the error remains (so maybe the above additions weren't the problem). Any help would be appreciated. Thanks!
Upvotes: 0
Views: 176
Reputation: 60483
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.
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.
Upvotes: 1
Reputation: 2594
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().
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
Upvotes: 0