【发布时间】:2020-10-31 16:47:13
【问题描述】:
我遇到了 foreach 的问题。
如果在第一个 foreach {foreach $children as $child} 中返回一个结果,则它可以工作。越多,就会弹出一个错误:
Nette\Database\ResultSet 只实现一种方式迭代器。
该错误可能是由于在同一个表childern 中查询的第一个($children)和第二个($invoices)foreach 引起的。我需要列出所有孩子(例如$child->firstName)并为每个孩子分配项目(例如$invoice->date$invoice->snackName)。但是,我无法在查询中取消“JOIN ON children”。
输出:
孩子 1 个名字
- 项目 1
- 项目 2
孩子 2 的名字
- 项目 1
- 项目 2
不知道怎么回事? 谢谢
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;
}
拿铁
{block content}
{foreach $children as $child}
{$child->fisrtName}
{foreach $invoices as $invoice}
{invoice->snack}
{/foreach}
{/foreach}
{/block}
【问题讨论】:
-
请更正问题的第二段。它不能完全理解,也不能用英语正确书写。