1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Question about extending default models, controllers, etc

Discussion in 'XenForo Development Discussions' started by ChrisStatzer, Oct 7, 2010.

  1. ChrisStatzer

    ChrisStatzer New Member

    While I have a fairly good grasp of the structure of XF, I am primarily a python developer so I really have no good sense of "right and wrong" so to speak. I can look at something in Python and say "OMG thats crap!", but php not so much.

    I am busy adding a thread rating system (you know the ones, with the little JS stars) and wanted to extend the Thread model. Here was my approach:


    class ThreadRating_Model_ThreadRating extends XenForo_Model_Thread
        public function 
    prepareThread(array $thread, array $forum, array $nodePermissions null, array $viewingUser null) {
    $thread parent::prepareThread($thread$forum$nodePermissions$viewingUser);
    $thread['rating'] = 2/* code to fetch avg rating here */
    return $thread;

    This looks like dammit to me.... Is this intended usage? It works fine. School me please.

  2. Deceptor

    Deceptor Active Member

    Looks right to me :) You just need to extend the class through a code listener which I presume you've done?
  3. ChrisStatzer

    ChrisStatzer New Member

    Oh yeah, its all functional etc. Just wanted to make sure my use of parent and manipulating the return value was cool.
  4. Deceptor

    Deceptor Active Member

    Yep everything there is fine. Just think of it as: Fetch -> Manipulate -> Return :)
  5. ChrisStatzer

    ChrisStatzer New Member

    Exactly what I would do in Python so I feel a lot better about it now :)

  6. ragtek

    ragtek Guest

    Chris are you working on an thread rate add-on?
    Would you pls share this with us?:)
  7. Indigo

    Indigo Active Member

    Technically, it should extend XFCP_ThreadRating_Model_ThreadRating. Otherwise, if someone installs another addon that extends the thread model, then installs yours (if your code listener executes last), it would skip over the other addon's extension.

    How it would execute:
    class XFCP_OtherAddon_Model_Thread extends XenForo_Model_Thread {} // generated
    class OtherAddon_Model_Thread extends XFCP_OtherAddon_Model_Thread code }
    XFCP_ThreadRating_Model_ThreadRating extends OtherAddon_Model_Thread {} // generated
    class ThreadRating_Model_ThreadRating extends XenForo_Model_Thread code }
    So, your thread rating model would end up "skipping over" the other addon and breaking the chain.
    samat and Vladislav Rastrusny like this.

Share This Page