Question about extending default models, controllers, etc

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:

PHP:
<?php

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.

Chris
 
Looks right to me :) You just need to extend the class through a code listener which I presume you've done?
 
Oh yeah, its all functional etc. Just wanted to make sure my use of parent and manipulating the return value was cool.
Chris
 
Chris are you working on an thread rate add-on?
Would you pls share this with us?:)
 
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:
PHP:
class XFCP_OtherAddon_Model_Thread extends XenForo_Model_Thread {} // generated
class OtherAddon_Model_Thread extends XFCP_OtherAddon_Model_Thread { code }
class 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.
 
Top Bottom