Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Model not loading?
  • I've run into a weird issue, and could use some advice from some more seasoned FuelPHP-ers.

    At the top of my view, I have a line of code (variable names changed for explanation)
    $things = Model_Items::find_by_id(array(1, 2, 3));

    Later in the view, I loop through the items in $data,
    foreach($things as $item)
    but Fuel tells me $item is undefined (and xdebug indicates that $things is undefined at this point)

    I would assume a DB configuration issue, except that when I do php oil console and call Model_Items::find_by_id, I get the correct response.

    What am I missing? Where should I be looking to get to the bottom of this problem? I am stumped.
  • HarroHarro
    Accepted Answer
    You should not use code in  your views, views should contain only HTML, and simple php structures for display purposes only. If you need code, like in this case to retrieve additional data, use a Presenter.

    Views are only generated (executed if you will) when the View object is cast to string, which happens in the echo at the very end of your index.php. By that time the request has long finished and the framework is already in its cleanup phase.
  • Harro, you are correct. This is actually code I've inherited from a previous developer, and it is working on the live version of the site (but not locally). Before I go changing things — and this is definitely on the list — I want to make sure I've got the current app running.

    Any ideas why I'd be getting an error, or where to look, aside from the misuse of the view?
  • What is the exact setup, and the exact error, if any?

    If the model can be loaded, $things can never be undefined, it gets the return value of the find(), which is either null or an array of model records.

    If $things is really undefined (as in a PHP Notice error), then the model call isn't executed.

    This all happens in the same view file? No subviews, data passing constructs, etc?
  • Fuel\Core\PhpErrorException [ Notice ]:
    Undefined variable: item
    <? foreach($things as $item): ?>
    <li><?= View::forge('blocks/item_default', array('item'=> $item) );?></li>
    <? endforeach; ?>
    The page is a subview (APPPATH/views/home/index.php)
    included as <?= $body?>
    within APPPATH/views/basic.php

    When I breakpoint (I'm set up with xdebug), I can run the appropriate query.
    The code at the top of the view does not appear to get executed. I even put just a plain
    <?
    print 'Hello World';
    ?>
    at the top of the file, and set a breakpoint. No break.
  • Ah, a subview.

    Variables are not inherited automatically. If you have a view that includes sub-views, you need to pass the variables required to it explicitly.

    How is that view assigned to $body?
  • Reporting back with the solution. The person/people who developed the site included subviews using PHP's short open tags. <?= $body ?>.  My php.ini file did not have short_open_tags = On, and once I changed that, things started loading. OMG.

    Harro, thank you for taking the time to respond; without your comments, I would not have found my way to this solution.

    --RP

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion