简体   繁体   English

学说:拥有方和逆方

[英]Doctrine: owning side and inverse side

Hellow, in de Doctrine documentations says: 'Doctrine will only check the owning side of an association for changes.'你好,在 de Doctrine 文档中说:“Doctrine 只会检查关联的拥有方是否有变化。”

I have read other posts here, but I can't find an example that makes me understand why if there is a change in the inverse side this would not be persisted by doctrine.我在这里读过其他帖子,但我找不到一个例子让我理解为什么如果反面发生变化,这将不会被学说坚持下去。

I take the example of the post: Understanding of “owning side” and “inverse side” concepts in Doctrine我以帖子为例: 对Doctrine中“拥有方”和“反方”概念的理解

Customer entity: Customer实体:

class Customer
{
    // ...

    /** ONE-TO-ONE BIDIRECTIONAL, OWNING SIDE
     * @ORM\OneToOne(targetEntity="Company", inversedBy="customer")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id")
     */
    private $company;

    // ...

    /**
     * Set company method
     *
     * @param Company $company
     */
    public function setCompany( Company $company )
    {
       $this->company = $company; 
       $company->setCustomer( $this );
    }
}

Company entity: Company实体:

class Company
{
    // ...

    /** ONE-TO-ONE BIDIRECTIONAL, INVERSE SIDE
     * @OneToOne(targetEntity="Customer", mappedBy="company")
     */
    private $customer;

    // ...
}

and I have two questions: 1. How would be setCustomer method in Company Entity?我有两个问题: 1. 如何在公司实体中设置客户方法? (to reflect this database model) 2. In that case it could be that a change in the Company Entity could not be persisted by doctrine? (以反映该数据库模型) 2. 在那种情况下,公司实体的变更可能无法通过原则持久化?

Take out all the things that have in your head about Owning side and Inversed side .把你脑海中关于Owning sideInversed side所有东西都Inversed side These are nothing but some concepts that help Doctrine hydrate data into related models .这些只是帮助 Doctrine 将数据水合为相关模型的一些概念

Read the following quotation from the Doctrine doc.阅读以下来自 Doctrine 文档的引文。 This may somewhat help understand the concept of Owning side and Inversed side .这可能在一定程度上有助于理解Owning sideInversed side的概念。

"Owning side" and "inverse side" are technical concepts of the ORM technology, not concepts of your domain model. “拥有方”和“反向方”是 ORM 技术的技术概念,而不是域模型的概念。 What you consider as the owning side in your domain model can be different from what the owning side is for Doctrine.您认为域模型中的拥有方可能与 Doctrine 的拥有方不同。 These are unrelated.这些是无关的。

Another quotation from the Doctrine doc:来自 Doctrine doc 的另一个引文:

Doctrine will only check the owning side of an association for changes. Doctrine 只会检查关联的拥有方是否有更改。

This means the owning side of an association is the entity with the table containing the foreign key .这意味着关联的拥有方是包含外键的表的实体 Therefore, the table containing foreign key will only be considered by doctrine for changes.因此,包含外键的表只会被学说考虑进行更改。

Once again from the Doctrine doc:再次来自 Doctrine 文档:

  • OneToOne - One instance of the current Entity refers to One instance of the referred Entity . OneToOne -当前实体的一个实例引用被引用实体的一个实例
  • The owning side of a OneToOne assocation is the entity with the table containing the foreign key OneToOne关联的拥有方是包含外键的表的实体

Here one instance of Company refers to one instance of the referred entity Customer or vice versa.这里Company 的一个实例是指被引用实体Customer 的一个实例,反之亦然。

When we talk about OneToOne association like your example above, the owning side would be the entity with the table containing the foreign key, therefore, Customer entity.当我们像上面的示例一样谈论OneToOne关联时,拥有方将是包含外键的表的实体,因此,客户实体。

With your example, Doctrine will create tables like the below:对于您的示例,Doctrine 将创建如下表:

CREATE TABLE Company (
    id INT AUTO_INCREMENT NOT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;

CREATE TABLE Customer (
    id INT AUTO_INCREMENT NOT NULL,
    company_id INT DEFAULT NULL,
    PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE Customer ADD FOREIGN KEY (company_id) REFERENCES Company(id);

Now if you want to get data for a customer associated with a company then the query would be:现在,如果您想获取与公司关联的客户的数据,那么查询将是:

SELECT Company.id AS CompanyID, Customer.id AS CustomerID
FROM Company
LEFT JOIN Customer ON Company.id = Customer.company.id;

The returned result from this type of query will be hydrated into both models by Doctrine.此类查询的返回结果将被 Doctrine 合并到两个模型中。

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

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