We are working our way through the long tutorial of the fat free framework. We have made our connection to the database and understand it’s workings. However, we are stuck in the section about mapping a table and loading a single record.

This is the related code:

$f3->route('GET /',
    function($f3){

    $db=new DB\SQL(
        'mysql:host=localhost;port=3306;dbname=liselore',
        'jow',
        ''
    );

        $test1 = new DB\SQL\Mapper($db,'test1');
        $test1->load('id=1');

        $f3->set('result', $test1);

        $template = new Template;
        echo $template->render('views/homepage.html');

    }
);

$f3->run();

This is the template file:

<repeat group="{{ @result }}" value="{{ @item }}">
    <li>{{ @item.name  }} : {{@item.age}}</li>
</repeat>

I don’t get an error, so i can’t see what’s wrong here. Some help would be appreciated.

Thx,

有帮助吗?

解决方案

$test1 is a mapper object, not an array.

Therefore, your template should look like:

<li>{{@result->name}} : {{@result->age}}</li>

NB: you should not confuse the mapper load and find methods. The load method hydrates the current mapper object with one record. The find method returns an array of mapper objects. E.g:

  • The effect of $test1->load('id=1') is to map the object $test1 with the database row id 1.
  • The effect of $list=$test1->find("name LIKE 'A%'") is to return an array of mappers whose name start with a A
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top