Nette 数据库:未找到连接查询的参考
Nette Database: No reference found for Join query
如何使用 JOINS 在 renderDefault 函数中创建查询?我一直在看 nette 文档有一段时间了,但似乎找不到答案。所以想请教有经验的人。谁能知道答案?
** 假设您收到了这个查询**
SELECT a.id, a.firstname, a.lastname, b.name FROM racers a JOIN types b ON b.id = a.type
并且您想像这样创建它:
public function renderDefault(): void
{
$this->template->racers = $this->database->table('racers');
}
我的一个想法是做这样的事情
public function renderDefault(): void
{
$this->template->racers = $this->database->table('racers')
->select('racers.id, racers.firstname, racers.lastname, types.name')
->joinWhere('types', 'types.id = racers.type');
}
出于某种原因,“我的”查询似乎一直有效,直到我需要在拿铁咖啡的 foreach 循环中使用它。
这就是为什么我想知道什么是更好的方法。
{foreach $racers as $racer}
<tr>
<th scope="row">{$racer->id}</th>
<td>{$racer->firstname}</td>
<td>{$racer->lastname}</td>
<td>{$racer->name}</td>
</tr>
{/foreach}
错误:
Nette\InvalidArgumentException
No reference found for $racers->types.
如果您想使用更简单的database explorer API over regular SQL queries through the core API,您需要在数据库中正确设置外键。
资源管理器 API 将允许您直接使用 table,推断哪些列 select 以及哪些 table 自动使用:
public function renderDefault(): void {
$this->template->racers = $this->database->table('racers');
}
<tr n:foreach="$racers as $racer">
<th scope="row">{$racer->id}</th>
<td>{$racer->firstname}</td>
<td>{$racer->lastname}</td>
<td>{$racer->type->name}</td>
</tr>
如何使用 JOINS 在 renderDefault 函数中创建查询?我一直在看 nette 文档有一段时间了,但似乎找不到答案。所以想请教有经验的人。谁能知道答案?
** 假设您收到了这个查询**
SELECT a.id, a.firstname, a.lastname, b.name FROM racers a JOIN types b ON b.id = a.type
并且您想像这样创建它:
public function renderDefault(): void
{
$this->template->racers = $this->database->table('racers');
}
我的一个想法是做这样的事情
public function renderDefault(): void
{
$this->template->racers = $this->database->table('racers')
->select('racers.id, racers.firstname, racers.lastname, types.name')
->joinWhere('types', 'types.id = racers.type');
}
出于某种原因,“我的”查询似乎一直有效,直到我需要在拿铁咖啡的 foreach 循环中使用它。 这就是为什么我想知道什么是更好的方法。
{foreach $racers as $racer}
<tr>
<th scope="row">{$racer->id}</th>
<td>{$racer->firstname}</td>
<td>{$racer->lastname}</td>
<td>{$racer->name}</td>
</tr>
{/foreach}
错误:
Nette\InvalidArgumentException
No reference found for $racers->types.
如果您想使用更简单的database explorer API over regular SQL queries through the core API,您需要在数据库中正确设置外键。
资源管理器 API 将允许您直接使用 table,推断哪些列 select 以及哪些 table 自动使用:
public function renderDefault(): void {
$this->template->racers = $this->database->table('racers');
}
<tr n:foreach="$racers as $racer">
<th scope="row">{$racer->id}</th>
<td>{$racer->firstname}</td>
<td>{$racer->lastname}</td>
<td>{$racer->type->name}</td>
</tr>