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

Load Extra data in posts

Discussion in 'XenForo Development Discussions' started by Fuhrmann, Oct 8, 2011.

  1. Fuhrmann

    Fuhrmann Well-Known Member

    I am making a addon and i want to load extra data from my table.

    xf_dados_posts:
    dados_id
    user_id
    post_id
    dados_date
    dados_tag
    dados_state

    I have extended the model XenForo_Model_Post like this:

    PHP:
    <?php
    /**
    *
    * @see XenForo_Model_Post
    * @author Fuhrmann
    *
    */
    class DadosPosts_Extend_Model_Post extends XFCP_DadosPosts_Extend_Model_Post
    {

        const 
    FETCH_DADOS 0x050;

        public function 
    getPostsInThread($threadId, array $fetchOptions = array())
        {
            
    $fetchOptions['join'] += self::FETCH_DADOS;

            return 
    parent::getPostsInThread($threadId$fetchOptions);
        }

        public function 
    preparePostJoinOptions(array $fetchOptions)
        {
            
    $selectFields '';
            
    $joinTables '';

            if (
    $fetchOptions['join'] & self::FETCH_DADOS)
                {
                    
    $selectFields .= ',
                        dados_posts.user_id AS dados_posts.user_id,
                        dados_posts.post_id AS dados_posts.post_id'
    ;

                    
    $joinTables .= '
                        INNER JOIN xf_dados_posts AS dados_posts ON
                            (dados.post_id = post.post_id AND dados.user_id = post.user_id)'
    ;

                }
            return 
    parent::preparePostJoinOptions($fetchOptions);
        }

        
    /**
        * @return DadosPosts_Model_DadosPosts
        */
        
    protected function _getDadosModel()
        {
            return 
    $this->getModelFromCache('DadosPosts_Model_DadosPosts');
        }
    But does not work. IS there another way to do that? I want my extra data avaliable in the thread.
     
  2. ragtek

    ragtek Guest

    PHP:
    public function preparePostJoinOptions(array $fetchOptions)
        {
            
    $selectFields '';
            
    $joinTables '';

            if (
    $fetchOptions['join'] & self::FETCH_DADOS)
                {
                    
    $selectFields .= ',
                        dados_posts.user_id AS dados_posts.user_id,
                        dados_posts.post_id AS dados_posts.post_id'
    ;

                    
    $joinTables .= '
                        INNER JOIN xf_dados_posts AS dados_posts ON
                            (dados.post_id = post.post_id AND dados.user_id = post.user_id)'
    ;

                }
            return 
    parent::preparePostJoinOptions($fetchOptions);
        }
    this doesn't work

    your $selectFields & $joinTables aren't sent to the parent method.

    run as first the parent method and save the return

    then run your conditions and add them to the return of the parent method because this returns
    PHP:
            return array(
                
    'selectFields' => $selectFields,
                
    'joinTables'   => $joinTables,
                
    'orderClause'  => ($orderBy "ORDER BY $orderBy'')
            );
     
    Fuhrmann likes this.
  3. Fuhrmann

    Fuhrmann Well-Known Member

    This is sad! =(

    I am at least 4 hours trying to do that.

    Hope someone knows!
    Thanks, ragtek!
     
  4. ragtek

    ragtek Guest

    i've edited my post;)
     
  5. Fuhrmann

    Fuhrmann Well-Known Member

    I just saw now. I will try!
     
  6. Fuhrmann

    Fuhrmann Well-Known Member

    I have tryed this way (dont know if it is what you was talking about)

    PHP:
    public function preparePostJoinOptions(array $fetchOptions)
        {
            
    $response parent::preparePostJoinOptions($fetchOptions);

            if (
    $fetchOptions['join'] & self::FETCH_DADOS)
                {
                    
    $response['selectFields'] .= ',
                        dados_posts.user_id AS dados_posts.user_id,
                        dados_posts.post_id AS dados_posts.post_id'
    ;

                    
    $response['joinTables'] .= '
                        INNER JOIN xf_dados_posts AS dados_posts ON
                            (dados.post_id = post.post_id AND dados.user_id = post.user_id)'
    ;
                }

            return 
    $response;

        }
     
  7. Fuhrmann

    Fuhrmann Well-Known Member

    I got it!
    If someone wants to know, here is the answer:

    PHP:
    public function preparePostJoinOptions(array $fetchOptions)
        {
            
    $array parent::preparePostJoinOptions($fetchOptions);

            if(!empty(
    $fetchOptions['join']))
            {
                if(
    $fetchOptions['join'] & self::FETCH_USER)
                {
                    
    $array['selectFields'] .= ',
                        dados_posts.user_id AS dados_posts_user_id,
                        dados_posts.post_id AS dados_posts_post_id'
    ;
                    
    $array['joinTables'] .= '
                        LEFT JOIN xf_dados_posts AS dados_posts ON
                            (dados_posts.post_id = post.post_id AND dados_posts.user_id = post.user_id)'
    ;
                }
            }

            return 
    $array;
        }
     
  8. James

    James Well-Known Member

    Isn't it redundant to check the fetchoptions twice? If its not empty (first if) then it'll return true for the second if won't it?
     
  9. Fuhrmann

    Fuhrmann Well-Known Member

    That's true James. Thanks.
     
  10. Arik

    Arik Well-Known Member

    No.

    The second isn't checking if it's set. It's checking to see if it contains the self::FETCH_USER bit. That's why there's only one ampersand.
     
  11. Fuhrmann

    Fuhrmann Well-Known Member

    Yes...now i see. It was giving a error after i remove the first "if".
     
  12. James

    James Well-Known Member

    Woops didn't notice the single ampersand.
     

Share This Page