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?
Take out all the things that have in your head about Owning side
and Inversed 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 of Owning side
and Inversed 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:
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.