CakePHP model association not working inside plugin

1k views Asked by At

By using the CakePHP 2.4 framework i'm trying implement a Admin site and Json Plugin for access it's content data.

So, simply i used bake commands and created the admin site and plugin for it. Model class created for site only(app) not for the plugin. Im planing to access the app Model classes for Plugin controllers also.

-MyProject
--app
--Model
 --AppModel.php
 --Post.php
 --Comment.php
--Plugin
 --MyPlugin
   --Model
     --MyPluginAppModel.php

*Please note that all other files and structure remain same.

I have Model Association, One Post Has Many Comments

So in my Plugin controller (CommentsController.php) i used this code

$temp = $this->Comment->Post->findById($post_id);

and its giving me the error " Call to a member function findById() on a non-object"

But when i access,

$temp = $this->Comment->findById($comment_id);

this is working fine.

Then i found two working solutions for this as follows.

1-Create same model classes for the plugin

2- Use bindModel function inside the controller

(This belongsTo Relationship already there in the Comment.php model.)

this->Comment->bindModel(
        array('belongsTo' => array(
                'Post' => array(
                    'className' => 'Post',
                    'foreignKey' => 'post_id',
                )
            )
        )
    );

Seems both methods are useless and not the way to do(Hopefully).

So my question, what is right way to do this? Did i missed anything ?

Mainly, why i cant access the Model Association inside the Plugin ??

Please help. Any suggestion/answer is appreciated.

Thanks,

0

There are 0 answers