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

Invalid argument supplied for foreach

Discussion in 'XenForo Development Discussions' started by Mythotical, Nov 8, 2010.

  1. Mythotical

    Mythotical Well-Known Member

    Ok this is odd, haven't gotten this error ever and I can not figure it out.

    Here is the error:
    Model File: LatestThreads.php
    PHP:
    class xPortal_Model_LatestThreads extends XenForo_Model
    {
        public function 
    getThreads($id)
        {
            
    $ltOption XenForo_Application::get('options')->xportal_latestThreads;
            
    $lThreads $this->limitQueryResults('
                SELECT thread_id, title, user_id, username, post_date, last_post_user_id, last_post_username
                FROM xf_thread WHERE node_id = ' 
    $ltOption ' ORDER BY thread_id '5);

            return 
    $this->_getDb()->fetchAll($lThreads);
        }
    }
    Pulling data in ControllerPublic/Index.php:
    PHP:
            $xpThreadModel $this->getModelFromCache('xPortal_Model_LatestThreads');
            
    $t $xpThreadModel->getThreads(5);
            
    $viewParams = array(
                
    't' => $t
            
    );
            return 
    $this->responseView('xPortal_ViewPublic_Portal''xportal_index'$viewParams);
    ViewPublic/Portal.php file:
    PHP:
    class xPortal_ViewPublic_Portal extends XenForo_ViewPublic_Base
    {
        public function 
    renderHtml()
        {
            
    $blocksModel = new xPortal_Model_Blocks;
            
    $left $blocksModel->getLeft();
            
    $mid $blocksModel->getMid();
            
    $right $blocksModel->getRight();
            
            
    $leftBlocks "";
            foreach (
    $left AS $lft)
            {
                
    $leftTemplate $lft['temp_template'];
                
    $leftBlocks .= $this->createTemplateObject($leftTemplate);
            }
            
    $this->_params['leftBlocks'] = $leftBlocks;
            
            
    $midBlocks "";
            foreach (
    $mid AS $m)
            {
                
    $midTemplate $m['temp_template'];
                
    $midBlocks .= $this->createTemplateObject($midTemplate);
            }
            
    $this->_params['midBlocks'] = $midBlocks;
            
            
    $rightBlocks "";
            foreach (
    $right AS $rgt)
            {
                
    $rightTemplate $rgt['temp_template'];
                
    $rightBlocks .= $this->createTemplateObject($rightTemplate); 
            }
            
    $this->_params['rightBlocks'] = $rightBlocks;
        }
    }
    Might be a bit of info but giving all info I think would help in determining the issue.
     
  2. Jeremy

    Jeremy XenForo Moderator Staff Member

    Are you trying to extend a function that returns a view? If so, you gotta do $var = parent::actionBlah(); and then set either $var->params OR $var->subView->params.

    Edit: actually... what's the template code?
     
  3. Mythotical

    Mythotical Well-Known Member

    HTML:
    <div class="heading">Latest Threads</div>
    <div class="secondaryContent">
    <ol>
    <xen:foreach loop="$t" value="$i">
    <li>{xen:raw $i.title}</li>
    </xen:foreach>
    </ol>
    </div>
    <div>&nbsp;</div>
    Thanks KK
     
  4. Shadab

    Shadab Well-Known Member

    Could be that your model isn't returning any threads. What's the output if you replace your template with:
    Code:
    <div class="heading">Latest Threads</div>
    <div class="secondaryContent">
    	{xen:helper dump, $t}
    </div>
     
  5. Jeremy

    Jeremy XenForo Moderator Staff Member

    I haven't really looked into it, but is renderHTML() overwriting a function in XenForo to render the template? If it is, do a Zend_Debug::dump($this->_params[]). As it is because $t is not set in the template (that was my problem for BB Code Manager).
     
  6. Shadab

    Shadab Well-Known Member

    Actually none of the base view classes contain any render*() methods; so we don't have to worry about unintentionally overriding them.
     
    Jeremy likes this.
  7. Jeremy

    Jeremy XenForo Moderator Staff Member

    I hadn't looked for the fact that I'm writing a paper, but the snippets of code confused the heck out of me. But wasn't sure if his View was overwriting anything. Also not sure where his template is being used, but I'm most likely going to go with what you originally mentioned and say it isn't returning something.
     
  8. Mythotical

    Mythotical Well-Known Member

    Returns NULL in my block.
     
  9. Jeremy

    Jeremy XenForo Moderator Staff Member

    Which means that your getThreads() method is not working / returning any results. Attempt Zend_Debug::dump($t); and Zend_Debug::dump($ltOption); to see the results. Then take it and run the query yourself to see the results. Plus, $id never gets used, so throw that out (no need for extra variables).
     
    Shadab likes this.
  10. Mythotical

    Mythotical Well-Known Member

    Sure thing I will go play with it after some well deserved sleep.
     
  11. Mythotical

    Mythotical Well-Known Member

    Ok apparently it is doing the query right but not returning any threads for some reason. I will just dig more on it after some sleep.
     
  12. Shadab

    Shadab Well-Known Member

    Just curious, why are you creating your own model to fetch the latest threads when you can easily accomplish this via xenforo's thread model (XenForo_Model_Thread) ?
     
    Jeremy likes this.
  13. Mythotical

    Mythotical Well-Known Member

    Yep I can and I will, this is for the fetching for the Articles block and Tutorials block as well as the latest addons block.
     
  14. Mythotical

    Mythotical Well-Known Member

    I still haven't gotten this stupid error figured out. I get a return of NULL I have tried to alter quite a few things but I have to be missing something.
     
  15. Lawrence

    Lawrence Well-Known Member

    Steve, I tested your model for you, on my add-on I am currently working on, and dumped the results of $t:

    PHP:
    array(5) {
      [
    0] => array(7) {
        [
    "thread_id"] => int(1)
        [
    "title"] => string(22"How to Create an Addon"
        
    ["user_id"] => int(1)
        [
    "username"] => string(8"lawrence"
        
    ["post_date"] => int(1286387673)
        [
    "last_post_user_id"] => int(1)
        [
    "last_post_username"] => string(8"lawrence"
      
    }
      [
    1] => array(7) {
        [
    "thread_id"] => int(2)
        [
    "title"] => string(9"Test Post"
        
    ["user_id"] => int(2)
        [
    "username"] => string(6"Dakota"
        
    ["post_date"] => int(1287580701)
        [
    "last_post_user_id"] => int(2)
        [
    "last_post_username"] => string(6"Dakota"
      
    }
      [
    2] => array(7) {
        [
    "thread_id"] => int(3)
        [
    "title"] => string(17"Test For Sticking"
        
    ["user_id"] => int(2)
        [
    "username"] => string(6"Dakota"
        
    ["post_date"] => int(1289659226)
        [
    "last_post_user_id"] => int(2)
        [
    "last_post_username"] => string(6"Dakota"
      
    }
      [
    3] => array(7) {
        [
    "thread_id"] => int(4)
        [
    "title"] => string(29"Sticky Permission for a Group"
        
    ["user_id"] => int(2)
        [
    "username"] => string(6"Dakota"
        
    ["post_date"] => int(1289659513)
        [
    "last_post_user_id"] => int(2)
        [
    "last_post_username"] => string(6"Dakota"
      
    }
      [
    4] => array(7) {
        [
    "thread_id"] => int(5)
        [
    "title"] => string(25"Attachment Uploading Test"
        
    ["user_id"] => int(1)
        [
    "username"] => string(8"lawrence"
        
    ["post_date"] => int(1289666674)
        [
    "last_post_user_id"] => int(1)
        [
    "last_post_username"] => string(8"lawrence"
      
    }
    }
    So it does work, and $t does come back as an array.

    Here is what I did to get it to work. I created a new Model (remember it is within my Add-ons directory). I copied your code exactly, within the function, with the exception of setting $ltOptions to a value I know exists in my DB.

    PHP:
    <?php

    /*
     * Model for Testing for Steve
     *
    */

    class XenAms_Model_FetchThreads extends XenForo_Model
    {

        public function 
    getLatestThreads()
        {
            
    $ltOption 2;
            
    $lThreads $this->limitQueryResults('
                SELECT thread_id, title, user_id, username, post_date, last_post_user_id, last_post_username
                FROM xf_thread WHERE node_id = ' 
    $ltOption ' ORDER BY thread_id '5);

            return 
    $this->_getDb()->fetchAll($lThreads);
        }
    }
    I then edited one of my Listeners I am using to call the function and spit out the results. Here is the code I used to test your Model:

    PHP:
    $t $this->_getThreadData();
    Zend_Debug::dump($t);

        protected function 
    _getThreadData()
        {

            return 
    $this->_getThreadsModel()->getLatestThreads();
        }

        public function 
    _getThreadsModel()
        {

            return 
    XenForo_Model::create('XenAms_Model_FetchThreads');
        }
    As you can see by the very first php code block in this post, $t came back as an array with 5 threads. It looks like to me, that your node_id set by your options was not correct. When I set it to a value I know doesn't exist, $t dumps with this:
    PHP:
    array(0) {
    }
    It looks like this is causing the problem (is the setting an array, or maybe the node_id put in doesn't exist?):

    PHP:
    $ltOption XenForo_Application::get('options')->xportal_latestThreads
     
  16. Mythotical

    Mythotical Well-Known Member

    Ahhh that would explain it then. Odd though guess back to the drawing board on that. Thanks though, now for latest threads overall using XF how would I handle that for displaying the latest 5 threads from the entire forum?
     
  17. Lawrence

    Lawrence Well-Known Member

    You can use the same model for the latest threads of the forum, like this:

    PHP:
    class XenAms_Model_FetchThreads extends XenForo_Model
    {

        public function 
    getLatestThreads($ltOption 0)
        {
            
    $whereclause"";

            if (
    $ltOption)
            {
                
    $whereclause =  " WHERE node_id = ".$ltOption;
            }

            
    $lThreads $this->limitQueryResults('
                SELECT thread_id, title, user_id, username, post_date, last_post_user_id, last_post_username
                FROM xf_thread ' 
    $whereclause ' ORDER BY thread_id DESC'5);

            return 
    $this->_getDb()->fetchAll($lThreads);

        }

    }
    Have you already taken permissions into account?
     
  18. Mythotical

    Mythotical Well-Known Member

    Lawrence, what about the error I sent you in a convo?
     
  19. Lawrence

    Lawrence Well-Known Member

    Sorry, I had the flu bug for a bit, and then spent the last two days learning node types, and then all the possible different combination's you can have with the current ones. I needed to focus on that for displaying of a banner based on where the user is within the site, reguardless if it is a Page, Forum, or Category, or a child of any of those.

    For the error you sent me: is your Threads.php method located in xPortal/ or xPortal/Model/ ? The error shows xPortal/ so if Threads.php is not located in that directory it won't find the method.
     
  20. Mythotical

    Mythotical Well-Known Member

    Oooooh, I didn't know that.
     

Share This Page