R
R
russeljo2020-02-06 09:24:06
Doctrine ORM
russeljo, 2020-02-06 09:24:06

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);


In fact, in the admin panel, this will be a form for adding an entry to the database, where there will be a selection field (for example, select a language for an article). I can describe unidirectional or bidirectional in entities, what will change? Maybe there are some concrete examples.

Question 2. When multiple categories can be selected for an article - what is the relationship? ManyToMany - one article can have several categories, at the same time one category can include several articles. Or am I confusing something? Will there be a 3rd table here?

Question 3. What does cascade={"persist"} mean?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
index0h, 2020-02-08
@index0h

What is the difference between unidirectional ManyToOne and bidirectional? What is the technical difference, nuances?

Example is shit. At you as a matter of fact identical relations both in the first case and in the second (from a DB). Probably meant dependencies between your entities. This really can be done. Roughly speaking unidirectional: User knows the Address. Bidirectional - User knows his Address AND Address corresponding to him User knows his own. In the database, indeed, both options look the same. The difference is how the EntityManager works with them.
When and what to choose?

The default is unidirectional. If you suddenly need - finish the feedback.
When multiple categories can be selected for an article - what is the connection?

Many articles fit many categories.
Will there be a 3rd table here?

Yes
What does cascade={"persist"} mean?

By executing EntityManager::persist($myEntityName), the $myEntityName dependencies will cascade persist and there is no need to do it manually.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question