Sam
Sam

Reputation: 462

CakePHP 2 Plugin controller doesn't load plugin model

My plugin controller 'PagesController.php' don't want to load it's related model. I always got the following error:

Database Error
Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'getPageById' at line 1

SQL Query: getPageById

Notice: If you want to customize this error message, create app\View\Errors\pdo_error.ctp

The plugin that I'm talking about is called 'CoasterCms'. Here's a tree with the most important folders and files in it:

-- Plugin
---- CoasterCms    
------ Controller
-------- CoasterCmsAppController.php
-------- NewsArticlesController.php
-------- PagesController.php
------ Model
-------- CoasterCmsAppModel.php
-------- NewsArticle.php
-------- Page.php
------ View
-------- NewsArticles
---------- add.ctp
---------- edit.ctp
---------- delete.ctp

The plugins 'PagesController.php':

<?php

class PagesController extends CoasterCmsAppController
{
    public function index()
    {
        ...
    }

    public function add()
    {
        ...
    }

    public function edit($id = null)
    {
        $this->Page->getPageById(1);
    }

    public function delete($pageId = null)
    {
        ...
    }
}

The plugins 'CoasterCmsAppController.php':

<?php

class CoasterCmsAppController extends Controller {
    public $helpers = array(
        'Html',
        'Form',
        'Session',
        'CoasterCms.CmsMenu'
    );

    public $components = array(
        'Session',
        'Flash',
        'Paginator',
        'Auth' => array(
            'loginAction' => array(
                'plugin' => 'coaster_cms',
                'controller' => 'users',
                'action' => 'login'
            ),
            'loginRedirect' => array(
                'plugin' => 'coaster_cms',
                'controller' => 'menus',
                'action' => 'index'
            ),
            'authenticate' => array(
                'Form' => array(
                    'passwordHasher' => 'Blowfish'
                )
            ),
            'authError' => 'You have no rights.'
        )
    );

    public function beforeRender()
    {
        parent::beforeRender();

        ...
    }
}

The plugins 'Page.php':

<?php

class Page extends CoasterCmsAppModel
{
    public $actsAs = array(
        'Slugable.Slugable' => array(
            'name' => 'slug'
        )
    );

    public $hasOne = array(
        'ModuleActionModule' => array(
            'className' => 'CoasterCms.ModuleActionModule',
            'foreignKey' => 'module_action_module_id'
        )
    );

    public $hasMany = array(
        'MenuPageLink' => array(
            'className' => 'CoasterCms.MenuPageLink',
            'foreignKey' => 'page_id'
        )
    );

    public $validate = array(
        ...
    );

    /**
     * Get a page.
     * 
     * @param int $id Pagina id.
     * @throws NotFoundException If id type is not numeric.
     * @throws NotFoundException If page doesn't exists.
     * @return array Pagina.
     */
    public function getPageById ($id) {
        if (!$id || !is_numeric($id)) {
            throw new NotFoundException(
                __('Invalid id.')
            );
        }

        $page = $this->find('first', array(
            'recursive' => -1,
            'fields' => array(
                'Module.id',
                'Module.name',
                'Module.controller'
            ),
            'contain' => array(
                'ModuleActionModule' => array(
                    'fields' => array(
                        'ModuleActionModule.id',
                        'ModuleActionModule.module_id',
                        'ModuleActionModule.module_action_id'
                    ),
                    'ModuleAction' => array(
                        'fields' => array(
                            'ModuleAction.name',
                        ),
                        'order' => array(
                            'ModuleAction.name' => 'asc'
                        )
                    )
                )
            ),
            'conditions' => array(
                'Module.id' => $id
            )
        ));

        if (!$page) {
            throw new NotFoundException(
                __('Invalid page.')
            );
        }

        return($page);
    }
}

The plugins 'CoasterCmsAppModel.php':

<?php

class CoasterCmsAppModel extends Model {
    public $actsAs = array(
        'Containable'
    );
}

I'm getting a headache with this problem since my naming conventions are okay. Somebody who can help me please?

Thanks a lot!

Upvotes: 2

Views: 262

Answers (1)

Rohit Rasela
Rohit Rasela

Reputation: 445

It seems your model object using the default model created by cakephp not by plugin. If you did not load the plugin model in you controller file. Pease try to use public $uses = array('CoasterCms.Page'); to load the Plugin's model.

<?php

    class PagesController extends CoasterCmsAppController
    {
       public $uses = array('CoasterCms.Page');
    ....

    }

Ref: https://book.cakephp.org/2.0/en/plugins/how-to-create-plugins.html#plugin-controllers

Upvotes: 1

Related Questions