views:

125

answers:

2

I have a function in my Comic Model as such:

<?php

class Comic extends AppModel
{
    var $name = "Comic";
    // Methods for retriving information.
    function testFunc(){
        $mr = $this->find('all');
        return $mr;
    }
}
?>

And I am calling it in my controller as such:

<?php
class ComicController extends AppController
{
   var $name = "Comic";
   var $uses = array('Comic');
   function index()
   {
  }
  function view($q)
  {
    $this->set('array',$this->Comic->testFunc());
  }
}
?>

When I try to load up the page; I get the following error:

Warning (512): SQL 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 'testFunc' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 525] Query: testFunc

And the SQL dump looks like this:

(default) 2 queries took 1 ms
Nr Query Error Affected Num. rows Took (ms)
1 DESCRIBE comics 10 10 1
2 testFunc 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 'testFunc' at line 1 0

So it looks like, instead of running the testFunc() function, it is trying to run a query of "testFunc" and failing...

A: 

What happens if you do a var_dump( $this->Comic->testFunc() )? That looks like it should work. Btw, can you really set $array like that? I thought array was a reserved word in PHP. Regardless, you may want to rename that to something more descriptive (the type is array, so you're not really adding any metadata that isn't already there with that name).

Additionally, what shows up in the CakePHP error log? Anything in the Apache error log?

Travis Leleu
var_dump($this->Comic->testFunc()); results in bool(false)and still has the same error and sql list from cake debug mode:it's trying to run the query "testFunc" and erroring.also, the set array was just a test name I changes it to results, and nothing changed.Also, my host's apache doesn't keep an error_log
Rixius
Nope, PHP is perfectly happy to let you put whatever you like in a variable named `$array`.
Daniel Wright
Can you execute a regular query from your controller? I.e. can you retrieve data from your comic table?
Travis Leleu
A: 

OK, I was finally able to reproduce the same error. I suspected the error was Cake trying to "automagically" create model functionality, which it does when it can't find a model matching the controller. So, what I did was create the controller (app/controllers/comic_controller.php) but not a model. I used the original poster's exact controller code, and when I tried to invoke the Comic::view action, I got the same error.

I can only conclude that, similar to the other departures from Cake's naming conventions, the original poster named the model file in a funny way (e.g. app/models/comics.php) such that Cake was unable to find it, and attempted to compensate with magic. And failed.

So, to underscore my first comment to the original question, I strongly recommend following the conventions! At the very least, start by following them, then break them one at a time, so that when something breaks, you know why.

For a "Comics" module, here are the basic components in the conventional naming scheme:

  • Controller:
    • class name: ComicsController extends AppController
    • filename: app/controller/comics_controller.php
  • Model:
    • class name: Comic extends AppModel
    • filename: app/controller/comic.php
  • Views directory: app/views/comics/
  • Database table: comics
Daniel Wright
I had accidentally(by habit) named all of my models first-letter capitalized.I am surprised how particular cakes Magic really is... Will have to be more careful.
Rixius