title: DB relationship (elegant for any ORM)
body: <p>I just seek for an advice, what is the best way to a create nice and elegant relationship (that could be used with ORM methods). The content of the database looks like this:</p>
  
  <pre><code>  mysql&gt; describe books;
    +--------+-------------+------+-----+---------+----------------+
    | Field  | Type        | Null | Key | Default | Extra          |
    +--------+-------------+------+-----+---------+----------------+
    | id     | int(11)     | NO   | PRI | NULL    | auto_increment |
    | title  | varchar(45) | YES  | MUL | NULL    |                |
    | author | varchar(45) | YES  |     | NULL    |                |
    | cover  | varchar(45) | YES  |     | NULL    |                |
    +--------+-------------+------+-----+---------+----------------+
  
    mysql&gt; describe users;
    +----------+-------------+------+-----+---------+----------------+
    | Field    | Type        | Null | Key | Default | Extra          |
    +----------+-------------+------+-----+---------+----------------+
    | id       | int(11)     | NO   | PRI | NULL    | auto_increment |
    | username | varchar(45) | YES  |     | NULL    |                |
    +----------+-------------+------+-----+---------+----------------+
  
   mysql&gt; describe users_books;
   +---------+---------+------+-----+---------+-------+
   | Field   | Type    | Null | Key | Default | Extra |
   +---------+---------+------+-----+---------+-------+
   | user_id | int(11) | YES  |     | NULL    |       |
   | book_id | int(11) | YES  |     | NULL    |       |
   +---------+---------+------+-----+---------+-------+
  
   mysql&gt; describe comments;   
   +---------+-------------+------+-----+---------+----------------+
   | Field   | Type        | Null | Key | Default | Extra          |
   +---------+-------------+------+-----+---------+----------------+
   | id      | int(11)     | NO   | PRI | NULL    | auto_increment |
   | body    | varchar(45) | YES  |     | NULL    |                |
   | rate    | varchar(45) | YES  |     | NULL    |                |
   | user_id | int(11)     | YES  |     | NULL    |                |
   +---------+-------------+------+-----+---------+----------------+
  
   mysql&gt; describe books_comments;
   +------------+-------------+------+-----+---------+-------+
   | Field      | Type        | Null | Key | Default | Extra |
   +------------+-------------+------+-----+---------+-------+
   | book_id    | int(11)     | YES  |     | NULL    |       |
   | comment_id | varchar(45) | YES  |     | NULL    |       |
   +------------+-------------+------+-----+---------+-------+
  </code></pre>
  
  <p>I don't have any issues with that model, but I think it can be improved. In this way I am fetching the records: </p>
  
  <pre><code>select body, rate, users.username,title, author from users
  inner join users_books on users.id = users_books.user_id
  inner join books on books.id = users_books.book_id
  inner join books_comments on users_books.book_id = books_comments.book_id
  inner join comments on books_comments.comment_id = comments.id
  where users.id = 1 and comments.user_id = 1;
  </code></pre>
  
  <p>While users can have many books, a comment is very specific for a book owned by a specific user. I think that is the point I am not getting it the right way. </p>