I came across a problem with foreach.
If one result is returned in the first foreach {foreach $children as $child}
, then it works. As there are more, an error pops up:
Nette\Database\ResultSet
implements only one way iterator.
The error is probably caused by the first ($children
) and the second ($invoices
) foreach being queried in the same table childern
. I need to list all children (eg $child->firstName
) and assign items to each (eg $invoice->date
$invoice->snackName
). However, I cannot cancel "JOIN ON children
" in the query.
Output:
child 1 name
- item 1
- item 2
child 2 name
- item 1
- item 2
Don't know what's wrong? Thanks
public function actionShow($year, $month)
{
$invoices = $this->database->query('
SELECT
o.date AS date,
o.snack AS snack
FROM
diet_orders o
LEFT JOIN children ch ON o.child_id = ch.id
WHERE
ch.user_id = ?
and YEAR (o.date) = ?
and MONTH (o.date) = ?
', $this->getUser()->id, $year, $month,'');
$children = $this->database->table('children')->where('user_id = ?', $this->getUser()->id);
$this->template->invoices = $invoices;
$this->template->children = $children;
}
Latte
{block content}
{foreach $children as $child}
{$child->fisrtName}
{foreach $invoices as $invoice}
{invoice->snack}
{/foreach}
{/foreach}
{/block}