query

  • Thread starter Thread starter Deleted member 10469
  • Start date Start date
D

Deleted member 10469

Guest
Hello, how make a query for get à 5 last thread please ?

I have that:
PHP:
                    <?php
                        $sujets = XenForo_Model::create('XenForo_Model_Thread');
 
                        $whereConditions = array('id' => '1,2');
                        $fetchOptions = array('limit' => 3, 'orderBy' => 'title desc');
 
                        $threads = $sujets->getThreads($whereConditions, $fetchOptions);
                        var_dump($threads);
 
                    ?>

But only 'limit' worked.

xenforo function used (in Model/Thread.php) :
PHP:
<?php
  /**
    * Gets threads that match the given conditions.
    *
    * @param array $conditions Conditions to apply to the fetching
    * @param array $fetchOptions Collection of options that relate to fetching
    *
    * @return array Format: [thread id] => info
    */
    public function getThreads(array $conditions, array $fetchOptions = array())
    {
        $whereConditions = $this->prepareThreadConditions($conditions, $fetchOptions);
 
        $sqlClauses = $this->prepareThreadFetchOptions($fetchOptions);
        $limitOptions = $this->prepareLimitFetchOptions($fetchOptions);
 
        $forceIndex = (!empty($fetchOptions['forceThreadIndex']) ? 'FORCE INDEX (' . $fetchOptions['forceThreadIndex'] . ')' : '');
 
        return $this->fetchAllKeyed($this->limitQueryResults(
            '
                SELECT thread.*
                    ' . $sqlClauses['selectFields'] . '
                FROM xf_thread AS thread ' . $forceIndex . '
                ' . $sqlClauses['joinTables'] . '
                WHERE ' . $whereConditions . '
                ' . $sqlClauses['orderClause'] . '
            ', $limitOptions['limit'], $limitOptions['offset']
        ), 'thread_id');
    }
 
$conditions['id'] is not a valid condition.

If you check out XenForo_Model_Thread::prepareThreadConditions you will see it's looking for $conditions['node_id']... or indeed other types of ID.

Also if you check out public function prepareThreadFetchOptions you will see there is no "orderBy" index. It's looking for $fetchOptions['order'] (title) and $fetchOptions['orderDirection'] (desc)

Finally, you asked for the last 5 threads... your code if it was correct would get you the last 3 threads in forums 1 and 2 arranged by the title, descending.


So... to do what you asked for...

PHP:
$sujets = XenForo_Model::create('XenForo_Model_Thread');
$whereConditions = array('node_id' => '1,2');

$fetchOptions = array('limit' => 5, 'order' => 'post_date', 'orderDirection' => 'desc');
 
$threads = $sujets->getThreads($whereConditions, $fetchOptions);
var_dump($threads);

That should work to get the last 5 threads in node 1 and 2 in descending post date order.
 
Thank you for the very detailed explanation :)

I started in OOP and I saw that I could use on my website easily XenForo So I try to understand its Mechanisms :)
 
Oh yes also, I have this:

PHP:
<?php
// thread info (associative array)
 
echo XenForo_Link::buildPublicLink('canonical:threads', $thread); // thread URL
echo XenForo_Link::buildPublicLink('canonical:threads/add-reply', $thread); // add reply URL
?>

Are there other similar code very useful to know please ? :)
 
Oh yes also, I have this:

PHP:
<?php
// thread info (associative array)
 
echo XenForo_Link::buildPublicLink('canonical:threads', $thread); // thread URL
echo XenForo_Link::buildPublicLink('canonical:threads/add-reply', $thread); // add reply URL
?>

Are there other similar code very useful to know please ? :)
Does that work?

This works:
PHP:
XenForo_Template_Helper_Core::link('canonical:threads', $thread);
 
Back
Top Bottom