2

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,

tereško
  • 58,060
  • 25
  • 98
  • 150
NMW
  • 33
  • 7
  • Is the `Post` model in your plugin too? – Martin Bean Dec 15 '13 at 15:19
  • @MartinBean, there is no Post model inside the plugin. All the models available in, MyProject->app->Model – NMW Dec 15 '13 at 15:24
  • I think the problem is your plugin’s looking for a `Post` model in your plugin then, rather than the “global” app. Try setting the `className` key to `App.Post`, or allow your users be able to set which `Post` model class they want to use, like CakePHP allows you to specify what model to use for users in authentication. – Martin Bean Dec 15 '13 at 15:33
  • 1
    Post your code for "Post" model here. – XuDing Dec 16 '13 at 03:59
  • This is confusing because it looks like your Post model and Comment model are both in your main app. If this is the case, why do you have a Comments controller in your plugin? Is this Comments controller also associated to a Plugin model named Comments? – JadedCore Jan 24 '14 at 16:46

0 Answers0