1

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,

4

1 回答 1

4

$test1是一个映射器对象,而不是一个数组。

因此,您的模板应如下所示:

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

注意:你不应该混淆映射器loadfind方法。该load方法将当前映射器对象与一条记录相结合。该find方法返回一个映射器对象数组。例如:

  • 的效果$test1->load('id=1')是将对象映射到$test1数据库行 id 1。
  • 的效果$list=$test1->find("name LIKE 'A%'")是返回一个名称以 A 开头的映射器数组
于 2013-11-13T10:10:53.340 回答