简体   繁体   English

使用MongoDB和Lithium遍历结果

[英]Looping over results with MongoDB and Lithium

I've just started using Lithium and have come across a (probably very simple...) problem where I can't iterate over the results of a simple query. 我刚刚开始使用锂电,遇到了一个(可能非常简单...)问题,在该问题上我无法迭代简单查询的结果。 I have compared my code to various samples and I can't see any differences but something must be! 我已经将我的代码与各种示例进行了比较,虽然看不到任何差异,但一定有所不同!

// Controller
namespace app\controllers;

use app\models\POI;

class POIsController extends \lithium\action\Controller {

    public function index($category) {

       $data = POI::find('all', array('limit' => 10));

       $this->set(array('data' => $data));
    }
}



// Model
namespace app\models;

class POI extends \lithium\data\Model {
    protected $_meta = array(
        'source' => 'POI'
    );
}



// View
print $data->count(); // outputs 10

foreach($data as $poi):?>
    <?php print $poi->Name;?>
<?php endforeach; ?>

The loop in the View only displays the first item's Name field and misses out the other 9 which are apparently there. “视图”中的循环仅显示第一个项目的“名称”字段,而漏掉了其他显然存在的其他9个字段。

Does anyone have any ideas about why this is happening? 有谁知道为什么会这样吗?

As always the answer pops up just after asking the question... 一如既往,在提出问题后就会弹出答案...

My model doesn't have the usual ID set up (it has key in field "ID") so I had to add that to the schema and the meta data otherwise I guess all the models were thought to have the same empty key and so wouldn't iterate. 我的模型没有设置通常的ID(它在字段“ ID”中具有键),因此我必须将其添加到架构和元数据中,否则我猜所有模型都被认为具有相同的空键,因此不会迭代。

Updated Model code: 更新的型号代码:

namespace app\models;

class POI extends \lithium\data\Model {
    protected $_meta = array(
        'source' => 'POI',
        'key' => 'ID'
    );

    public $_schema = array(
        'ID' => array('type'=>'id'),
        'Name' => array('type'=>'string','null'=>false)
    );

}

Hope this can help someone else in the future! 希望这可以在将来对其他人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM