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

Accessing Model from Callback functions (multiple Errors)

Marcus

Well-known member
#1
This from a callback returns an error:
PHP:
$threads = XenForo_Model::create('XenForo_Model_Thread')->getThreads(
     array('discussion_state' => 'visible')
);
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 41 bytes) in ...\library\Zend\Db\Statement\Mysqli.php on line 304
This is returning a similar error:

PHP:
$threadModel = new XenForo_Model_Thread;

$threads = $threadModel->getThreads(
  array('discussion_state' => 'visible'), $fetchOptions
);
This returns a different error:
PHP:
$threads = XenForo_Model::create('XenForo_Model_Thread')->getThreads(
  array('forum_id' => 32)
);
  1. Argument 2 passed to XenForo_Model_Thread::getThreads() must be an array, null given, called in ...\library\Wb\Callback\ForumList.php on line 20 and defined in ...\xenforo\library\XenForo\Model\Thread.php, line 500
  2. Argument 2 passed to XenForo_Model_Thread::prepareThreadConditions() must be an array, null given, called in ...\library\XenForo\Model\Thread.php on line 502 and defined in ...\library\XenForo\Model\Thread.php, line 314:
    313: </div>
    314: <!-- end block: sidebar_online_users -->';
    315: $__compilerVar10 .= $__compilerVar11;

  3. Argument 1 passed to XenForo_Model_Thread::prepareThreadFetchOptions() must be an array, null given, called in ...\library\XenForo\Model\Thread.php on line 504 and defined in ...\library\XenForo\Model\Thread.php, line 81:
    80: $__compilerVar7 .= '
    81: ';
    82: foreach ($renderedNodes AS $node)

  4. Argument 1 passed to XenForo_Model::prepareLimitFetchOptions() must be an array, null given, called in ...\library\XenForo\Model\Thread.php on line 505 and defined in ...\library\XenForo\Model.php, line 367:
    366: $__compilerVar17 .= '
    367: <div class="facebookLike shareControl">
    368: ';

Do you know what causes the error? What is the right way to access a model from xen:callback?
 
Last edited:

Chris D

XenForo developer
Staff member
#3
You can access models and their functions from callback tags. My comment is a bit out of context and relates to something else (you're not extending a Model here).

The first block of code is failing due to a memory limit. You're fetching all of your threads on your board without any limits or other constraints. If you have thousands of threads then you're going to very quickly hit various memory limits.

The second block (and indeed the first block of code) work fine for me in a Callback tag (the first block of code is more correct, though).

The third block of code is working fine for me as well. The error you're getting suggests you should be passing an array as the second parameter... so instead of:

PHP:
$threads = XenForo_Model::create('XenForo_Model_Thread')->getThreads(
  array('forum_id' => 32)
);
It should be:

PHP:
$threads = XenForo_Model::create('XenForo_Model_Thread')->getThreads(
  array('forum_id' => 32), array()
);
But it's important to note: You shouldn't have to to supply a second parameter. The fact that it is complaining suggests you may be extending the getThreads function somewhere and the second parameter is not being set as = array() in the callback signature, e.g. it suggests you have extended getThreads like:

PHP:
public function getThreads(array $conditions, array $fetchOptions)
Instead of:

PHP:
public function getThreads(array $conditions = array(), array $fetchOptions = array())
 

Marcus

Well-known member
#4
Thanks! I have set a limit to only return one single thread. However I still get the memory error:

PHP:
    $fetchoptions = array(
      'permissionCombinationId' => $visitor['permission_combination_id'],
      'limit' => 1
    );

  $threads = XenForo_Model::create('XenForo_Model_Thread')->getThreads(
    array('discussion_state' => 'visible'), 
    $fetchOptions
    );
My community has over 100.000 threads, but if I return a single thread, that should be okay, wouldn't it?