学说中的主/外键关系
在我的 mysql 数据库上,我有两个表:“User”和“UserProfile”。表“UserProfile”有一个名为“user_id”的外键列,该列链接到“User”表的“id”列。现在,当我使用学说从数据库表生成所有实体类时,创建的“UserProfile”类包含一个名为“user”的属性(属于“User”类型),并且不包含任何名为“user_id”的属性。 可以吗?
现在,如果我想在给定 user_id 的情况下查找用户的个人资料,我需要编写如下内容:
$user_profile_repo = $this->em->getRepository("UserProfile");
$user_profile = $user_profile_repo->findOneBy(array("user_id"=>$id));
但由于生成的实体类不包含“user_id”属性,因此上述代码将不起作用。现在我需要知道如何进行调整才能使上述代码正常工作?谢谢。
On my mysql db, I have two tables: "User" And "UserProfile". The table "UserProfile" has a foreign key column named 'user_id' which links to "User" table's "id" column. Now, when I am generating all entity classes from my db tables using doctrine, the created "UserProfile" class contains a property named 'user'(which is of "User" type) and doesn't contain any property named 'user_id'. Is it ok?
Now, if I want to find a user's profile, given the user_id, I needed to write something like this:
$user_profile_repo = $this->em->getRepository("UserProfile");
$user_profile = $user_profile_repo->findOneBy(array("user_id"=>$id));
But as the generated entity class doesn't include the "user_id" property, the above code won't work. Now I need to know how can I do the tweak to make the above code work please? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
数据库中表/列的实际名称并不重要。您可以在评论中设置它们。
它应该是这样的:
在这种情况下,用户具有列 id,并且用户配置文件
一旦生成就有 user_id,您应该在 User 中获取 getProfile() 方法,并且在 UserProfile 中您应该获取 getUser()
它类似于5.7 oneToOne 双向: http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html
the actual names of the tables/columns in the database are not really important. you can set them in the comments.
it should be something like this:
in this case a user has the column id, and the userprofile has user_id
once generated, you should get in the User the method getProfile() and in the UserProfile you should get getUser()
it is similar to 5.7 oneToOne bidirectional: http://www.doctrine-project.org/docs/orm/2.0/en/reference/association-mapping.html