Answer the question
In order to leave comments, you need to log in
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
Here, read my article, it just talks about your problem:
Implementing a tag system in the admin panel with the Sonat bundle...
How to implement a "global catalog of entity instances" in Doctrine?
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;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question