• 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

Fuhrmann

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

ragtek

Guest
#2
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" : '')
        );
 
R

ragtek

Guest
#4
i've edited my post;)
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.
 

Fuhrmann

Well-known member
#6
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;

    }
 

Fuhrmann

Well-known member
#7
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;
    }
 

James

Well-known member
#8
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?
 

Arik

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