Fox Alex
Fox Alex

Reputation: 133

Doctrine orm one to many with reference key also containing strings

I have an entity called books and another one users. Each book can have many authors(users). So when I add a book I select the users that are registered on the site. But if the author of the book doesn't have an account on the site I want to type his name by hand.

+----+---------+-----------+--+
| id | book_id | author_id |  |
 +----+---------+-----------+--+
|  1 |       1 | 1         |  |
|  2 |       1 | Mr.Paul   |  |
+----+---------+-----------+--+

See how the author_id is a string but also an reference key to an user? Can I do this with doctrine or I will have to manage the inserts myself?

Edit:I have no idea what the right title should be Edit2:A possible solution will be to make another table containing only authors that don't have an account on the website.

Upvotes: 0

Views: 44

Answers (1)

Simon Müller
Simon Müller

Reputation: 451

I would recommend following approach

You have a table books a table accounts and the following:

authors_books

+----+---------+-----------+--+
| id | book_id | author_id |  |
+----+---------+-----------+--+
|  1 |       1 | 1         |  |
|  2 |       1 | 2         |  |
+----+---------+-----------+--+

authors

 +----+---------+------------+--+
| id | name     | account_id |  |
+----+---------+------------+--+
|  1 | Mr. Paul | 1          |  |
|  2 | Simon    | (empty)    |  |
+----+---------+-------------+--+

the account_id can be empty

Upvotes: 2

Related Questions