Question

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.

Does anyone have any ideas about why this is happening?

Was it helpful?

Solution

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.

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top