简体   繁体   English

如何在Nette中配置外键?

[英]How to configure foreign keys in Nette?

I've been reading for a while Nette tutorial for database selects etc, but I still do not understand, how does Nette know, what is foreign key and what is not. 我已经阅读了一段时间Nette有关数据库选择的教程,但我仍然不明白,Nette怎么知道,什么是外键,什么不是。 Do I need to define it? 我需要定义它吗? If so, how? 如果是这样,怎么办? There nothing about it in documentation. 文档中没有关于它的任何内容。

According to this documentation , I do not have to define foreign keys. 根据本文档 ,我不必定义外键。 As I'm used to Java JPA, it sounds a bit strange for me. 正如我习惯Java JPA一样,这对我来说有点奇怪。

Does Nette even generates database? Nette甚至生成数据库吗? (So far I do not think so). (到目前为止,我不这么认为)。

Nette\\Database does not generate database, it inspects the database and discovers the foreign keys – see ActiveRow documentation, in particular the description of M:1 relation : Nette \\ Database不会生成数据库,它会检查数据库并发现外键–请参阅ActiveRow文档,尤其是M:1关系的描述:

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()方法完成。 Ref() method accepts two arguments: target table name and source joining column. Ref()方法接受两个参数:目标表名称和源联接列。 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. 如果我们调用不存在的属性,则ActiveRow尝试将调用属性名称解析为“具有一个”关系。 [emphasis mine] Getting this property is the same as calling ref() method with just one argument. [强调我的]获得此属性与仅使用一个参数调用ref()方法相同。 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. 传递的键与行列进行匹配,如果匹配,则在匹配的列上定义的外键用于从相关目标表获取数据。

You can see how does it work internally in the source code . 您可以在源代码中看到它在内部如何工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM