1. 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)

Discussion in 'XenForo Development Discussions' started by Marcus, Sep 28, 2013.

  1. Marcus

    Marcus Well-Known Member

    This from a callback returns an error:
    PHP:
    $threads XenForo_Model::create('XenForo_Model_Thread')->getThreads(
         array(
    'discussion_state' => 'visible')
    );
    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)
    );

    Do you know what causes the error? What is the right way to access a model from xen:callback?
     
    Last edited: Sep 28, 2013
  2. Marcus

    Marcus Well-Known Member

    Is there no way to work with existing models?
     
  3. Chris D

    Chris D XenForo Developer Staff Member

    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())
     
    Jeremy and Marcus like this.
  4. Marcus

    Marcus Well-Known Member

    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?
     
  5. Marcus

    Marcus Well-Known Member

    I wrote $fetchoptions instead of $fetchOptions. It works great now. Thanks !!!!
     
    Chris D likes this.

Share This Page