I need to pick 3 of the latest (10) entries from $data["entries"] and send those to the entries controller of the widgets module. I have no idea how to manage this in the most performant way.
Here's some code from the controller that calls the module:
$data["entries"] = Model_Entry::find('all', array('limit' => 10, 'order_by' => 'created_at'));
$data["latest"] = Request::forge('widgets/entries/view/', false)->execute($data["entries");
$data["entries"] structure:
[1]=>
object(Model_Entry)#31 (10) {
["_data":protected]=>
array(9) {
["id"]=>
string(1) "2"
["entry_title"]=>
string(4) "test"
["entry_status"]=>
string(1) "1"
["created_at"]=>
string(1) "0"
["updated_at"]=>
string(1) "0"
}
}
[2]=>
object(Model_Entry)#32 (10) {
}
...
Entries View:
foreach($entries as $entry):
echo $entry->id;
endforeach;
Would it also be possible to pick only the objects with a entry_status of 1, of those 10 entries, to avoid another query?
The following code should do what you want:
$data["entries"] = Model_Entry::query()
->where('entry_status', 1)
->order_by('created_at')
->limit(10)
->get();