views:

50

answers:

3

I have two tables, Pages and Posts that are in HABTM and are joined using pages_posts join table.

My Page model along with the HABTM definition contains a function..

class Page extends AppModel
{
   var $name = "Page";
   ......
   ......
   function callthis()
   {
     return $this->find('all');;
   }
}

From my Posts controller, I'm trying to call that function..

class PostsController extends AppController
{
    ....
    ....
    function index()
    {
      $returned = $this->Post->Page->callthis();
    }
}

This results in

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 'threadedpages' at line 1 [CORE/cake/libs/model/datasources/dbo_source.php, line 681]

Further Debug output:

Code

        $out = null;
        if ($error) {
            trigger_error('<span style="color:Red;text-align:left"><b>' . __('SQL Error:', true) . "</b> {$this->error}</span>", E_USER_WARNING);

Context

$sql    =   "threadedpages"
$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 'threadedpages' at line 1"
$out    =   null

Calls

DboSource::showQuery() - CORE/cake/libs/model/datasources/dbo_source.php, line 681
DboSource::execute() - CORE/cake/libs/model/datasources/dbo_source.php, line 266
DboSource::fetchAll() - CORE/cake/libs/model/datasources/dbo_source.php, line 410
DboSource::query() - CORE/cake/libs/model/datasources/dbo_source.php, line 364
Model::call__() - CORE/cake/libs/model/model.php, line 502
Overloadable::__call() - CORE/cake/libs/overloadable_php5.php, line 50
AppModel::threadedpages() - [internal], line ??
PostsController::admin_index() - CORE/plugins/cakey/controllers/posts_controller.php, line 11
Dispatcher::_invoke() - CORE/cake/dispatcher.php, line 204
Dispatcher::dispatch() - CORE/cake/dispatcher.php, line 171
[main] - APP/webroot/index.php, line 83

Calling a associated model function should be possible in CakePHP right? If we checkout http://book.cakephp.org/view/1044/hasAndBelongsToMany-HABTM, We can see that

$this->Recipe->Tag->find('all', array('conditions'=>array('Tag.name'=>'Dessert')));

When find() function of Tag model can be called from associated Recipes controller, Why can't I call callthis() function of Page model from associated Posts controller?

+1  A: 

The problem is most likely in your relationships. I betch if you do

$this->Post->Page->find('all');

you'll still get errors.

Angel S. Moreno
@Moreno, thanks for your time.. You moved in the right direction, but you didn't give a definitive solution though. Marking as useful.
Ashok
I was waiting for you to post your relationships like @stefan asked. If I can't see it I can not provide a definitive solution. Did you get it to work?
Angel S. Moreno
A: 

Make sure the Page model is loaded in the PostsController (should be near the top of the controller file, before any functions):

var $uses = array('Post', 'Page');

Then you should be able to just call

$this->Page->callthis();
handsofaten
Loading an entire model into the current controller just for calling one function would be an overkill.. It would probably be fine, but I'm coding in Fat Model - Skinny Controller approach, so my model involves lots of code unnecessary for the PostsController. Thanks for your time :)
Ashok
Ah, I didn't realize you were doing this through a plugin. If you are calling a function from the model, it's going to have to load the model one way or another.
handsofaten
A: 

As Stefan and Moreno pointed, there was indeed a problem with my relationships.

According to http://book.cakephp.org/view/1114/Plugin-Models, If we need to reference a model within your plugin, we need to include the plugin name with the model name, separated with a dot.

I referenced the models directly without using Plugin.Model as className. Thanks everyone for your time..

Ashok
This is the MOST ANNOYING thing on Cake: Automodels without ANY notice. If cake doesn't find a model definition it just creates an auto-model with default values but IT DOES NOT SAY ANYTHING about it. So while you are wondering why your settings aren't working Cake just plainly ignores the file because you have some spelling-error or in your case the missing plugin-prefix. I'm sorry for the outburst but this one stole days of work in several projects. I wish there was a strict mode for disabling automodels. I'm happy you found your solution though. :-)
stefan
Agreed. Cake's Auto models are some times absolutely frustrating and time wasting. There definitely should be more information output from Cake.
Ashok