php - Doctrine: owning side and inverse side

10

Hellow, in de Doctrine documentations says: 'Doctrine will only check the owning side of an association for changes.'

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

Customer entity:

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:

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? (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?

264

Answer

Solution:

Take out all the things that have in your head aboutOwning side andInversed side. These are nothing but some concepts that help Doctrine hydrate data into related models.

Read the following quotation from the Doctrine doc. This may somewhat help understand the concept ofOwning side andInversed side.

"Owning side" and "inverse side" are technical concepts of the ORM technology, not concepts of your domain model. What you consider as the owning side in your domain model can be different from what the owning side is for Doctrine. These are unrelated.

Another quotation from the Doctrine doc:

Doctrine will only check the owning side of an association for changes.

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:

  • OneToOne - One instance of the current Entity refers to One instance of the referred Entity.
  • The owning side of a OneToOne assocation is the entity with the table containing the foreign key

Here one instance of Company refers to one instance of the referred entity Customer or vice versa.

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.

With your example, Doctrine will create tables like the below:

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.

People are also looking for solutions to the problem: javascript - DataTable not displaying AJAX JSON data

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.