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

Run query on user upgrade (code example) 1.x

Useful if you need to grant additional awards or status when a user pays for an upgrade.

  1. Jake Bunce
    You can edit the library/ExtendUserUpgrade/UserUpgradeModel.php file to set your own query or code to execute when a user pays for an upgrade. The file already includes a query that I used for the person I made this for:

    Code:
    <?php
    
    class ExtendUserUpgrade_UserUpgradeModel extends XFCP_ExtendUserUpgrade_UserUpgradeModel
    {
    	public function upgradeUser($userId, array $upgrade, $allowInsertUnpurchasable = false, $endDate = null)
    	{
    		$retval = parent::upgradeUser($userId, $upgrade, $allowInsertUnpurchasable, $endDate);
    
    		$db = XenForo_Application::get('db');
    
    		$db->query("
    			UPDATE iConomy
    			SET Balance = Balance + 3000
    			WHERE username = (
    				SELECT username
    				FROM xf_user
    				WHERE user_id = " . $userId . "
    			)
    		");
    
    		return $retval;
    	}
    }
    
    But he ended up needing to connect to a second database to update user records for his minecraft server. Here is an example with a second database connection. $db is the database for XenForo. $mcdb is the database for his minecraft server. This allowed him to query both databases to update point values:

    Code:
    <?php
    
    class ExtendUserUpgrade_UserUpgradeModel extends XFCP_ExtendUserUpgrade_UserUpgradeModel
    {
    	public function upgradeUser($userId, array $upgrade, $allowInsertUnpurchasable = false, $endDate = null)
    	{
    		$retval = parent::upgradeUser($userId, $upgrade, $allowInsertUnpurchasable, $endDate);
    
    		$db = XenForo_Application::get('db');
    		$mcdb = new Zend_Db_Adapter_Pdo_Mysql(array(
    			'host'     => 'localhost',
    			'username' => 'dbuser',
    			'password' => 'dbpass',
    			'dbname'   => 'dbname'
    		));
    
    		$username = $db->fetchOne("
    			SELECT username
    			FROM xf_user
    			WHERE user_id = " . $userId . "
    		");
    
    		$mcdb->query("
    			UPDATE iConomy
    			SET Balance = Balance + 3000
    			WHERE username = '" . addslashes($username) . "'
    		");
    
    		return $retval;
    	}
    }
    
    Dan, nacs, bubbl3 and 4 others like this.

Recent Reviews

  1. Andy.N
    Andy.N
    5/5,
    Version: 1.x
    This is a little addon that is extremely powerful if used properly. As always, Jake is top notch in providing help as well as making thoughtful addons that extend the power of Xenforo platform.