• 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?

#1
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($userId, XenForo_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.
 

Snog

Well-known member
#2
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.
 

Snog

Well-known member
#4
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
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()
 
#9
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.
 

Thomas.B

Well-known member
#11
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..
 

Snog

Well-known member
#12
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..
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
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..
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
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.
Although I guess checking for the class is what screwed me here in the first place. Maybe I should revisit that policy :)