S
S
smash_wp2016-03-14 14:54:39
MySQL
smash_wp, 2016-03-14 14:54:39

What is the best way to organize the table structure?

Online store selling books. There are comments for the book, author, publisher.
If everything is stuffed into one entity, then when adding a book comment, the database will get something like this:
Comments table:
Fields:
id - 1
user_id - 37
comment - blabla
book_id - 2
author_id - null
publisher_id - null
Where book_id - book id, to which a comment was added
author_id - the id of the author to which the comment was added
publisher_id - the id of the publisher to which the comment was added
In my opinion, such a table is not a normal solution (although maybe I'm not experienced enough :) ).
There is an option to create 3 tables with comments for each entity (for example, BookComment - comments to the book, AuthorComment - comments to the author, etc.)
Or create 3 additional tables to link authors, books and publishers with comments.
Perhaps there is a more "adequate" solution. Tell me please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Pavlov, 2016-03-14
@lexxpavlov

Here, read my article, it just talks about your problem:
Implementing a tag system in the admin panel with the Sonat bundle...

B
BoShurik, 2016-03-14
@BoShurik

How to implement a "global catalog of entity instances" in Doctrine?

I
Ildar, 2017-06-19
@vistoyn

Fields:
id - Comment ID
user_id - User ID
comment - Comment text
type - Comment type
foreign_id - Foreign key
type Make as constants in Entity

class Comment{
  
  const TYPE_BOOK = 1;
  const TYPE_AUTHOR = 2;
  const TYPE_PUBLISHER = 3;

  /**
   * @var bigint
   *
   * @ORM\Column(name="id", type="bigint")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  public $id;
  
  
  /**
   * @var bigint
   *
   * @ORM\Column(name="user_id", type="bigint")
   */
  public $user_id;
  
  
  /**
   * @var string
   *
   * @ORM\Column(name="comment", type="text")
   */
  public $comment;
  
  
  /**
   * @var smallint
   *
   * @ORM\Column(name="type", type="smallint")
   */
  public $type;
  
  
  /**
   * @var bigint
   *
   * @ORM\Column(name="foreign_id", type="bigint")
   */
  public $foreign_id;
}

$comment = new Comment();
$comment->user_id = 37;
$comment->comment = 'комментарий';
$comment->type = Comment::TYPE_BOOK;
$comment->foreign_id = $book->id;

In this case, you will not have connections, but they are not really needed, and you can do without them.
Comments are still displayed for the book, author, publisher.
Those. type and foreign_id are always specified in the select query.
If you need to display all comments in general, then do left join and filter: display comments separately by Books, authors and publishers

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question