phptemplatesfuelphpdatabase-abstraction

FuelPHP basics, using Model result in View


I am a little confused using fuelPHP 1.7.

The controller

class Controller_Website extends Controller
{
    public function action_index()
    {
        // http://fuelphp.com/docs/general/views.html

        $data = Website::get_results();

        //var_dump($data) // (data is found here);

        $views = array();
        $views['head'] = View::forge('common/head', $data);
        $views['header'] = View::forge('common/header', $data);
        $views['sidebar'] = View::forge('common/sidebar', $data);
        $views['content'] = View::forge('common/content', $data);
        $views['footer'] = View::forge('common/footer', $data);

        // return the rendered HTML to the Request
        return View::forge('website', $views)->render();
     }
}

The model

class Website extends \Model
{
    public static function get_results()
    {
        // Database interactions
        $result = DB::select('menu', 'url', 'title', 'text')
            ->from('aaa_website')
            ->where('id', '=', 1035)
            ->and_where('visible', '1')
            ->execute();

        return $result;
    }
}

All well sofar. Data is queried and found in the controller. What I am trying to accomplish is to use the data in my:

(nested) view

<html>
<head>
    <?php echo $head; ?>
</head>
<body>
<header>
    <div class="container">
        <?php echo $header; ?>
    </div>
</header>
<div class="row">
    <div class="container">
        <div class="col-md-4">
            <?php echo $sidebar; ?>
        </div>
        <div class="col-md-8">
            <?php echo $content; ?>
        </div>
    </div>
</div>
<footer>
    <div class="container">
        <?php echo $footer; ?>
    </div>
</footer>
</body>
</html>

Head view (nested):

<title><?php echo $title; ?></title>

Content view (nested):

<h1><?php echo $title; ?></h1>
<div class="welcome_user"><?php echo $text; ?></div>

And so on.

The variables in the view in this example are not available because they are not explicitly set in the controller. Do they have to be set explicitly or is passing the data object also possible? If so, how do I access this objects data in the right way? FuelPHP is lacking good examples here and I am stuck now.

How do I do it?


Solution

  • The view data is converted from array indexed to view variable named. So:

    View::forge('something', array('param' => 'value'));
    

    Will correspond to the following view:

    <h1><?=$param?></h1>
    

    Where things are going wrong is is that you pass the plain DB result to the view. You'd need to get the first result from the database result, like this:

    class Website extends \Model
    {
        public static function get_results()
        {
            // Database interactions
            $result = DB::select('menu', 'url', 'title', 'text')
                ->from('aaa_website')
                ->where('id', '=', 1035)
                ->and_where('visible', '1')
                ->as_assoc()
                ->execute()
                ->to_array();
    
            return reset($result);
        }
    }
    

    Note that I've first used ->to_array() to convert the result object to an array, then reset() to get the first result. I've also added ->as_assoc() to make sure you get an array result, ->as_object() would give you a stdClass instance.