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

Make it easier to extend prepareXConditions functions

Discussion in 'XenForo Suggestions' started by Chris D, Sep 23, 2013.

  1. Chris D

    Chris D XenForo Developer Staff Member

    This is a typical function you see in XenForo models and they are not very easy to extend:. This is from XenForo_Model_User:

    PHP:
        public function prepareUserConditions(array $conditions, array &$fetchOptions)
        {
            
    $db $this->_getDb();
            
    $sqlConditions = array();

            if (!empty(
    $conditions['user_id']))
            {
                if (
    is_array($conditions['user_id']))
                {
                    
    $sqlConditions[] = 'user.user_id IN(' $db->quote($conditions['user_id']) . ')';
                }
                else
                {
                    
    $sqlConditions[] = 'user.user_id = ' $db->quote($conditions['user_id']);
                }
            }
    The problem with extending this is if I want to pass additional $conditions to the function, it's not as easy as it could be.

    The key problem, as I see it, is $sqlConditions = array();

    You can't return the parent in the normal fashion because each time that resets the $sqlConditions to a blank array.

    The workaround is to grab the string returned by the parent function which is the valid SQL WHERE clause and append your own clause onto that. Nowhere near ideal.

    Is the solution quite simple?

    What about this?

    PHP:
        public function prepareUserConditions(array $conditions, array &$fetchOptions, array $sqlConditions = array())
        {
            
    $db $this->_getDb();

            if (!empty(
    $conditions['user_id']))
            {
                if (
    is_array($conditions['user_id']))
                {
                    
    $sqlConditions[] = 'user.user_id IN(' $db->quote($conditions['user_id']) . ')';
                }
                else
                {
                    
    $sqlConditions[] = 'user.user_id = ' $db->quote($conditions['user_id']);
                }
            }
    Now we can do this:

    PHP:
        public function prepareUserConditions(array $conditions, array &$fetchOptions, array $sqlConditions = array())
        {
            
    $db $this->_getDb();

            if (!empty(
    $conditions['my_condition']))
            {
                if (
    is_array($conditions['my_condition']))
                {
                    
    $sqlConditions[] = 'user.my_condition IN(' $db->quote($conditions['my_condition']) . ')';
                }
                else
                {
                    
    $sqlConditions[] = 'user.my_condition = ' $db->quote($conditions['my_condition']);
                }
            }

            return 
    parent::prepareUserConditions($conditions$fetchOptions$sqlConditions);
    Of course I may have overlooked some other solution, or a reason this may not work... but it seems like it should work to me.
     
  2. Valhalla

    Valhalla Well-Known Member

    I would like to see this.
     
  3. Liam W

    Liam W Well-Known Member

    I had to extend a few of those recently, and it really pissed me off...

    I ended up returning the parent with my sql conditions appended to it... Like you said, less than ideal.
     
    Iversia likes this.
  4. Milano

    Milano Well-Known Member

    I workaround this way

    PHP:
    public function prepareUserConditions(array $conditions, array &$fetchOptions)
        {
            
    $result parent::prepareUserConditions($conditions$fetchOptions);

            
    $sqlConditions = array($result);
       
            if (!empty(
    $conditions['my_condition']))
            {
                
    // .....
            
    }

            if (
    count($sqlConditions) > 1)
            {
                return 
    $this->getConditionsForClause($sqlConditions);
            }
            else
            {
                return 
    $result;
            }
        }
     
    Valhalla likes this.

Share This Page