Override Model method

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?

Comments

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator

    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.

  • R_JR_J Cheerleader & Troubleshooter Munich Moderator



    "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.

    1. Save the information in the Attributes column of the Discussion table
    2. Create a new column in the Discussion table
    3. Use the UserDiscussion table
    4. Create your own table

    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)


    public function discussionModel_beforeGetCount_handler($sender, $args) {
        $sender->sql()->join('Feature f', 'd.DiscussionID = f.DiscussionID');
        // And, if needed, something like that:
        $args['Wheres'] = ['f.FeatureID' => 5];
    }
    


    Look through the DiscussionModel to find other "fireEvents" to hook into and change the behaviour (or simply ask again 😉)


    nithish
  • Thanks for the reply, I'll check it out!👍️

Sign In or Register to comment.