简体   繁体   English

Symfony&Doctrine2:“ mappedBy”注释的值?

[英]Symfony&Doctrine2: Value of `mappedBy` annotation?

I am learning Symfony 3 with Doctrine 2. 我正在使用教义2学习Symfony 3。

When I have OneToMany relationship in Doctrine entity, what exactly I have to put into mappedBy annotation? 当我在Doctrine实体中具有OneToMany关系时,我到底要在mappedBy注释中放什么呢?

  • Is it the table name of current entity? 它是当前实体的表名吗?
  • Or is it the entity shortcut ? 还是实体快捷方式
  • Or is it the actual class name? 还是实际的班级名称?

Imagine this simple example: 想象一下这个简单的例子:

<?php
namespace AppBundle\Entity;

/**
 * @ORM\Entity
 * @ORM\Table(name="blog_category")
 */
class Category
{
    // ...

    /**
     * @ORM\OneToMany(targetEntity="Article", mappedBy="category")
     */
    private $articles;

    // ...

}

Why is the "category" correct value for mappedBy ? 为什么“类别”的正确值是mappedBy Why isn't it "blog_category" or "Category" (uppercase "C")? 为什么不是“ blog_category”“ Category” (大写的“ C”)? Or "AppBundle:Category" ? 还是“ AppBundle:类别”

Now I figured it out. 现在我想通了。 It is the name of related's entity class variable :-) 它是相关实体类变量的名称:-)

在此处输入图片说明

To give a complement, almost everything you could do in doctrine (could surely be applied for any other ORM/ODM), in almost every contexts (QueryBuilder, findBy methods, ...) you'll use property names rather than column names. 作为补充,几乎在理论上您可以做的所有事情(肯定可以应用于任何其他ORM / ODM),在几乎每种情况下(QueryBuilder,findBy方法...),您都将使用属性名称而不是名称。

The reason is quite simple, an ORM deals with objects and their properties, abstracting the real tables and their columns, no matter of the database engine or anything else. 原因很简单,无论数据库引擎是什么,ORM都会处理对象及其属性,抽象出真实的表及其列。

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

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