I’m working with two plugins:
Kikioboeru/Blog plugin has a model named
This plugin is not aware of the
FinlayDaG33k/Comments plugin (this is by design).
Now in a query for the
Articles table, I want to include the comments that are associated with them.
However, I cannot do it like this, because again, the Blog plugin is not aware of the Comments plugin (and as such, it doesn’t have a hard-coded association):
$articles = $this->Articles->find('all')->contain(['Comments']);
My question is, is there a way to add this association from the
FinlayDaG33k/Comments plugin is “aware” of the
Kikioboeru/Blog plugin but the
Kikioboeru/Blog plugin is not aware of the
How about subclassing the Table class yourself, and adding the required HasMany association in your subclass initialize method? I’ve not tried this myself, just an idea…
I’ve came to that idea ye by doing it with an extend, however, since only the parent class (the table in
Kikioboeru/Blog) will be loaded, the child class’ (
FinlayDaG33k/Comments) initialize method will not be ran :\
I could load the table from the Comments plugin (instead of the one in the Blog plugin) but this means the Blog plugin will become “aware” (more like dependent) of the Comments plugin, which is not something we want :\
I’ve managed to get “around” it with a dirty fix in my controller:
'foreignKey' => 'article_id',
'targetForeignKey' => 'comment_id',
'joinTable' => 'articles_comments'
However, I’d still like to have a way to do this from the models themselves :\
You can put that code in the bootstrap.php, using the TableRegistry to load the table.
That is an option, however, I still think it’ll become a mess in the long run when we have more models and tables and etc.
Write a listener for one of the system startup events, and build a more intelligent function that can handle this for all required models? Still a bit of a mess, but maybe more self-contained, and potentially one that doesn’t require code changes when you add new models; it might iterate the Table folder and find tables that have a certain flag set, caching the resulting list?
That does sound like an option.
hmm… tough one…