C
C
chizh422019-04-13 23:46:34
PostgreSQL
chizh42, 2019-04-13 23:46:34

Symfony, Doctrine, PostgreSQL. How to properly organize relationships between entities?

Good day!
Began to get acquainted with Symfony. In the process, some issues related to entities arose.
There are tables in the database:
Users ( id , login, password, ...);
Salers (id, user_id, ..., FOREIGN KEY(user_id) REFERENCES Users(id) );
Managers (id, user_id, ..., FOREIGN KEY(user_id) REFERENCES Users(id) );
Customers (id, user_id, ..., FOREIGN KEY(user_id) REFERENCES Users(id) );
Those. the "Users" table contains common fields for all users. Depending on the type, an entry is created in a linked table, where attributes that are specific to each type of user are stored.
In the ORM doc and in googled articles, the links are somehow one-sidedly considered (from those that I found, of course).
Everywhere, relationships between different entities are considered. And when using this approach, the entity classes will look like this (I don’t remember the link annotations for memory, sorry (( ):

class User
{
  protected $id;
  protected $login;
  protected $password;
  // и т.д....
}

class Saler
{
  protected $id;
  
  /**
   * @OneToOne(targetEntity="User", ...)
   */
  protected $user;
  // и т.д....
}

Managers and Customers by analogy.
With User, everything is extremely clear. With the rest, not so much.
How to specify links in this case? And is it OneToOne?
With such a connection, access to common fields will be carried out through a construction like
$saler->user->getLogin();
Did I understand correctly? If so, then...
Is it possible to organize access to entity fields the way it would be with inheritance, while storing common fields in the Users table (as it is now in the database)?
class Saler extends User{...}
// $saler instanceof Saler;
$saler->getLogin(); // User-method

I tried to manually add "... extends User" to the entity classes, in the database it turned out that all tables were created completely with parent fields. This is not quite what we would like.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2019-04-13
@chizh42

Is it possible to organize access to entity fields in the same way as it would be with inheritance, while storing common fields in the Users table (as it is now in the database)?
That's it - the fields of the parent entity in a separate table, in their own only individual
Class Table Inheritance
UPD: A little confused with the type of inheritance, not a single table, but a class table of course. Perhaps you figured it out, but other notes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question