HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.
Override Model method
nithish
New
Hey,
I'm trying to override the getCount($wheres = [], $unused = null) method present in Disscussion model using "themehooks".
Here's the code:
public function discussionModel_getCount($wheres = [], $unused = null)
{
return 3000;
}
I am able to create a new function using the same format but override does not seem to work.
Is there something that I'm missing?
Tagged:
0
Comments
That's not possible. The DiscussionModel is a PHP class and the getCount is a method of that class. You cannot override methods. What exactly do you want to achieve? Maybe it can be done in another way.
Lets say that I want to add another table that holds data about feature 'X' for a particular discussion.
Now, I'd like to fetch that data as well using the currently available methods.
The reason why I tried it in the first place is because the documentation says, "Magic methods only work in classes that extend Gdn_Pluggable". The DiscussionModel extends Gdn_Model which in turn extends Gdn_Pluggable.
"Magic methods allow you to create new methods and add them to existing objects."
You will need to use events for that. But maybe the approach is not the best. In order to save extra information you basically have four options.
Creating your own table is not always needed. But if you have to and you want that DiscussionModels getCount only returns discussions with feature X from table Feature, you need to do it like that (untested)
Look through the DiscussionModel to find other "fireEvents" to hook into and change the behaviour (or simply ask again 😉)
Thanks for the reply, I'll check it out!👍️