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

how to make an add-on to overwrite an exist method ?!

Discussion in 'XenForo Development Discussions' started by TBDragon, Mar 31, 2014.

  1. TBDragon

    TBDragon Active Member

    hi every one

    i just have in mind to make an add-on to sort the RM categories

    well the method i need to overwrite is


    XenResource_Model_Category
    getAllCategories()

    i change the code manually to this

    PHP:

            
    return $this->fetchAllKeyed('
                SELECT resource_category.*
                    ' 
    $joinOptions['selectFields'] . '
                FROM xf_resource_category AS resource_category
                ' 
    $joinOptions['joinTables'] . '
                ORDER BY resource_category.category_title
            '
    'resource_category_id');
    but for future when upgrading i will need to do that again also =(

    so i was thinking of making an add-on to do an overwriting for that method

    but i didnt know how to do it as i have low experience with XF development ^^


    thanks for help
     
    Last edited: Mar 31, 2014
  2. Chris D

    Chris D XenForo Developer Staff Member

    Extend the class that the method is in, and give the method the same name. It will overwrite the existing method instead of extending it.
     
    TBDragon likes this.
  3. TBDragon

    TBDragon Active Member

    u mean like this right ?!

    PHP:
    class TBDragon_RmSortingCategories_Category extends XenResource_Model_Category
    {

        public function 
    getAllCategories(array $fetchOptions = array())
        {
            
    $joinOptions $this->prepareCategoryFetchOptions($fetchOptions);

            return 
    $this->fetchAllKeyed('
                SELECT resource_category.*
                    ' 
    $joinOptions['selectFields'] . '
                FROM xf_resource_category AS resource_category
                ' 
    $joinOptions['joinTables'] . '
                ORDER BY resource_category.category_title
            '
    'resource_category_id');
        }

    if yes then its ok but what next

    i make an addon and go to make Listener so i can let XF know from where it call the code

    but i lost there =_= dono which Listener i should use !!
     
  4. Chris D

    Chris D XenForo Developer Staff Member

    Basically, follow this tutorial:

    http://xenforo.com/community/threads/how-to-add-more-actions-to-an-existing-controller.11202/

    Instead of using the load_class_controller event, use the load_class_model event.

    Then, basically, yes, this would overwrite that method:

    PHP:
        public function getAllCategories(array $fetchOptions = array())
        {
            
    $joinOptions $this->prepareCategoryFetchOptions($fetchOptions);

            return 
    $this->fetchAllKeyed('
                SELECT resource_category.*
                    ' 
    $joinOptions['selectFields'] . '
                FROM xf_resource_category AS resource_category
                ' 
    $joinOptions['joinTables'] . '
                ORDER BY resource_category.category_title
            '
    'resource_category_id');
        }
     
    TBDragon likes this.
  5. TBDragon

    TBDragon Active Member

    ah i see cuz i try the load_class _modle but it give me error

    but i use the tut and its work great

    thanks
     
  6. TBDragon

    TBDragon Active Member

    hi @Chris D there is a small issue now

    the addon work fine but its work also in the ACP
    i mean its order the categories in admin.php?resource-categories/ and some how its miss as i dono where is the child and the parent :(
    so is there way to exclude or stop working at ACP and just let it work and sort the categories in the forum ?!

    thanks
     
  7. Chris D

    Chris D XenForo Developer Staff Member

    Nope, Models are generally unaware of where they are being called from.

    The solution would be to extend the methods in the controllers which call the overwritten method.
     
    Brogan and TBDragon like this.
  8. TBDragon

    TBDragon Active Member

    so what i understand

    to let the addon Listener load the controller and this controller call the overwritten method ?!

    if yes then how i can know the controller for the RM ?! and if no how to do it then ?!

    waiting u and thanks
     

Share This Page