Question

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?

Was it helpful?

Solution 2

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..

OTHER TIPS

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

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

you'll still get errors.

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();

Hey I had the same problem.

I have added the PluginName.ModelName, then it is resolved. So adding a PluginName in front of the plugin model is very good practice,

Thanks, Vijay

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top