Answer the question
In order to leave comments, you need to log in
How to understand OneToMany(bidirectional, unidirectional) and OnDelete associations in Doctrine2?
This is a small introduction (can be skipped), the questions are below (in bold):
I'm using Zend Framework 3. And there is Doctrine2.
I swim in this matter.
Read this https://www.doctrine-project.org/projects/doctrine...
Read this https
:
//www.doctrine-project.org/projects/doctrine ... child(parent-child) in a one-to-many relationship. The descendant (child) contains a foreign key, refers to the parent. Which intuitively does not converge with the docks of the doctrine (for me at least). And there it is also written about the violation of referential integrity when updating / deleting an ancestor.
From the docks I understood:
- there are two sides: owning side
own(owner, descendant ) and inverse side reverse side( ancestor ).
- owning side contains the foreign key
Owning side owns the relation, owns the foreign key. No key, no relationship.
And in the case of a bidirect relationship, it must have the inversedBy attribute.
ManyToOne is always its own side.
OneToMany is always the other side.
Question 1: What is the difference between a unidirectional ManyToOne and a bidirectional ManyToOne? What is the technical difference, nuances? When and what to choose?
According to the docs, the generated SQL queries are no different: a field is added and it becomes a foreign key.
Only the description of entities (fields of php-classes) differ.
//пример из доков - однонаправленное многие-к-одному
CREATE TABLE Address (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE User (
id INT AUTO_INCREMENT NOT NULL,
address_id INT DEFAULT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE User ADD FOREIGN KEY (address_id) REFERENCES Address(id);
//пример из доков - двунаправленное один-ко-многим
CREATE TABLE Product (
id INT AUTO_INCREMENT NOT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
CREATE TABLE Feature (
id INT AUTO_INCREMENT NOT NULL,
product_id INT DEFAULT NULL,
PRIMARY KEY(id)
) ENGINE = InnoDB;
ALTER TABLE Feature ADD FOREIGN KEY (product_id) REFERENCES Product(id);
Answer the question
In order to leave comments, you need to log in
What is the difference between unidirectional ManyToOne and bidirectional? What is the technical difference, nuances?
When and what to choose?
When multiple categories can be selected for an article - what is the connection?
Will there be a 3rd table here?
What does cascade={"persist"} mean?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question