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

Having trouble extending a model. Can anyone spot my problem?

Discussion in 'XenForo Development Discussions' started by SpecialK, Aug 25, 2016.

  1. SpecialK

    SpecialK Member

    I am trying to extend the UserUpgrade model to add a new method to it, but it doesn't appear to be working. Here is what I have:

    1. A listener on load_class_model. The hint is "XenForo_Model_UserUpgrade" and the callback is TFP_ExpiredSubs_Listener_LoadClassModel::extendUserUpgradeModel

    2. My listener with the following content:
    PHP:
    <?php

    class TFP_ExpiredSubs_Listener_LoadClassModel
    {
        public static function 
    extendUserUpgradeModel($class, array &$extend)
        {
            if (
    $class == 'Xenforo_Model_UserUpgrade')
            {
                
    $extend[] = 'TFP_ExpiredSubs_Extend_Model_UserUpgrade';
            }
        }
    }
    and is located at TFP/ExpiredSubs/Listener/LoadClassModel.php

    3. My actual model extension with the following content:
    PHP:
    <?php

    class TFP_ExpiredSubs_Extend_Model_UserUpgrade extends XFCP_TFP_ExpiredSubs_Extend_Model_UserUpgrade {

        
    /**
         * Gets the expired upgrade records for the specified user.
         *
         * @param integer $userId
         *
         * @return array [upgrade id] => info (note, the id of the upgrade; not the user-specific record!)
         */

        
    public function getExpiredUserUpgradeRecordsForUser($userId)
        {
            return 
    $this->fetchAllKeyed('
             SELECT user_upgrade_active.*,
                user_upgrade.*
             FROM xf_user_upgrade_active AS user_upgrade_active
             LEFT JOIN xf_user_upgrade AS user_upgrade ON (user_upgrade.user_upgrade_id = user_upgrade_active.user_upgrade_id)
             WHERE user_upgrade_active.user_id = ?
                   AND end_date < ?
                   AND end_date > 0
          '
    'user_upgrade_id', array($userIdXenForo_Application::$time));
        }
    }
    and is located at TFP/ExpiredSubs/Extend/Model/UserUpgrade

    I am trying to extend an admin controller method and use my new method, but I am getting an error stating that the method doesn't exist. EG:

    PHP:
    <?php

    class TFP_ExpiredSubs_Extend_ControllerAdmin_User extends XFCP_TFP_ExpiredSubs_Extend_ControllerAdmin_User {

        public function 
    actionExtra()
        {
            
    $return parent::actionExtra();

            
    $userId $this->_input->filterSingle('user_id'XenForo_Input::UINT);
            
    $user $this->_getUserOrError($userId);

            
    /** @var XenForo_Model_UserUpgrade $upgradeModel */
            
    $expiredUpgradeModel $this->getModelFromCache('XenForo_Model_UserUpgrade');
            
    $expiredUpgradeRecords $expiredUpgradeModel->getExpiredUserUpgradeRecordsForUser($user['user_id']);


            return 
    $return;
        }
    }
    I'm pulling my hair out over this. I have a feeling I'm doing something obviously stupid but I'm not spotting it. Any help would be appreciated.
     
  2. Snog

    Snog Well-Known Member

    Assuming you have a listener extending the ControllerAdmin class, why even bother extending the model? Just create your model class and call the function directly.
     
  3. SpecialK

    SpecialK Member

    Yeah I definitely can do that. At this point I'm mostly just trying to figure out why it's not working.
     
  4. Snog

    Snog Well-Known Member

    Well, the question and answer session to establish why you're getting that error could end up being longer than just using the solution I gave. ;)

    Not the least of which would be the question, When do you get that error? and.. What's the full error trace?
     
  5. SpecialK

    SpecialK Member

    I'm extending the admin user controller and will be putting extra data on the "Extra" tab in a user's profile. The controller listener is working fine and the code for that is posted above. When I click on the extra tab, I get an undefined method error in the console:

    Code:
    Call to undefined method XenForo_Model_UserUpgrade::getExpiredUserUpgradeRecordsForUser()
     
  6. Snog

    Snog Well-Known Member

    Is this the path to the class?
    library/TFP/ExpiredSubs/Extend/Model/UserUpgrade.php
     
  7. SpecialK

    SpecialK Member

  8. Snog

    Snog Well-Known Member

    Did you get any errors when creating the loadclass_model listener?
     
  9. SpecialK

    SpecialK Member

    Nope, it saved just fine. This is my first time extending a model (Usually I just create new ones) so I'm sure it's something small that I'm overlooking. Don't spend too much time thinking about it, please. I was hoping that I had made an obvious mistake that could be pointed out easily.
     
  10. Snog

    Snog Well-Known Member

    Yeah, I don't see anything obvious. But it probably is so obvious I'm missing it too. ;)
     
  11. Thomas.B

    Thomas.B Well-Known Member

    If you change your method to

    public static function extendUserUpgradeModel($class, array &$extend)
    {

    if ($class == 'Xenforo_Model_UserUpgrade')
    {

    $extend[] = 'TFP_ExpiredSubs_Extend_Model_UserUpgrade';
    }
    }

    it should work. If you already stated the class name in the „Hint“ field of the code event listener, the check for the correct class name in your listener method is redundant. Also there is a typo in the class name (lowercase „f“ in „Xenforo“). Therefore the body of the if statement is never executed..
     
    SpecialK and Snog like this.
  12. Snog

    Snog Well-Known Member

    Good catch! The lower case f would do it. ;)

    The if class is redundant but wouldn't prevent the function from running. But combine that with the lower case f and it's a problem.

    See it was something obvious and so obvious I didn't see it. :(
     
  13. SpecialK

    SpecialK Member

    I know the check for the class is redundant, but I like having it there just because I've always done it that way, since before Hints existed.

    Thank you for spotting that typo! I knew it was something stupidly small.
     
  14. SpecialK

    SpecialK Member

    Although I guess checking for the class is what screwed me here in the first place. Maybe I should revisit that policy :)
     
  15. Thomas.B

    Thomas.B Well-Known Member

    Yeah, I'd recommend that. It's an additional source of error without any benefit..

    You're welcome!
     

Share This Page