title: PHP: refactoring to OO -- How to model Joins
body: <p>Let's say I have two models:</p>
  
  <pre><code>class Book
  {
      public $book_id;
      public $book_author_id;
      public $title;
  }
  
  class Author
  {
      public $author_id;
      public $author_name;
  }
  </code></pre>
  
  <p>I'm used to writing something like this:</p>
  
  <pre><code>$DB-&gt;query("SELECT book_id, title, author_name 
              FROM book 
              LEFT JOIN author 
                  ON book_author_id = author_id
             ");
  </code></pre>
  
  <p>Let's assume, that I'm not interested in having separate queries for this association. How do I proceed? Here are some things I've heard:</p>
  
  <ul>
  <li>create a MySQL VIEW of the JOIN</li>
  <li>create a model class of the VIEW</li>
  </ul>
  
  <p>The application I'm working on involves dozens of tables and was highly optimized in procedural code (almost no SELECT * anywhere, for instance). I'm refactoring to make it more maintainable (I'm the original creator, too), but I would like to have the flexibility of using joins when I need to without compromising the structure of my files and DB calls.</p>
  
  <p>A possibly related question I have is related to including other models:</p>
  
  <pre><code>class Author
  {
      public $author_id;
      public $author_name;
  
      /* @var Book */ //&lt;--don't really fully understand this but I've seen something like it somewhere
      public (array) $authors_books;
  }
  </code></pre>
  
  <p>I'm still searching for answers, but if you could send a link my way, that would be appreciated.</p>