Load Extra data in posts

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.
 
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" : '')
        );
 
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.
 
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;

    }
 
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;
    }
 
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?
 
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.
 
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.

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