如何在 Nette 中配置外键?

How to configure foreign keys in Nette?

我已经阅读了一段时间关于数据库选择等的 Nette 教程,但我仍然不明白,Nette 是如何知道的,什么是外键,什么不是。我需要定义它吗?如果是这样,如何?文档中没有关于它的内容。

根据这个documentation,我不必定义外键。因为我习惯了 Java JPA,所以对我来说听起来有点奇怪。

Nette 会生成数据库吗? (到目前为止我不这么认为)。

Nette\Database 不生成数据库,它检查数据库并发现外键 - 请参阅 ActiveRow 文档,特别是 M:1 relation:

的描述

Has one relation is a common use-case. Book has one author. Book has one translator. Getting related row is mainly done by ref() method. Ref() method accepts two arguments: target table name and source joining column. See example:

$book = $context->table('book')->get(1);
$book->ref('author', 'author_id');

[…]

All of this is fine, but it's somewhat cumbersome, don't you think? Database already contains the foreign keys definitions so why not use them automatically? Let's do that!

If we call property, which does not exist, ActiveRow tries to resolve the calling property name as “has one” relation. [emphasis mine] Getting this property is the same as calling ref() method with just one argument. We will call the only argument the key. Key will be resolved to particular foreign key relation. The passed key is matched against row columns, and if it matches, foreign key defined on the matched column is used for getting data from related target table.

您可以在 source code.

中查看它的内部工作原理