title: Retrieving many related information
body: <p>I am working on an admin system that allows users to manage their brewery profile and beer inventory.</p>
  
  <p>I have three related tables: Users, Breweries, and Beers. There is a 1-to-1 relationship between Users and Breweries, and a 1-to-many relationship between Breweries and Beers. Each table has a corresponding DAO: UserDAO, BreweryDAO, and BeerDAO, respectively.</p>
  
  <p>If a user wants to add a beer, for example, they would go to a page, <code>beer.php?brewery=123</code> where <code>123</code> is the ID of the brewery that this new beer will be added to. If a user wants to update a beer, they would go to the same page, e.g. <code>beer.php?id=456</code>, where <code>456</code> is the ID of the beer that will be edited.</p>
  
  <p>Now, when I go to the beer page, I need two pieces of information: the brewery and the beer, if they are editing. I also need to ensure that the brewery belongs to the logged-in user.</p>
  
  <p>I can think of two ways of doing this:</p>
  
  <p><strong>Option 1</strong>: Using many queries with no JOINs</p>
  
  <pre><code>// get relevant data for adding a new beer
  if (isset($_GET['brewery'])) {
      $brewery = $breweryDAO-&gt;getBreweryById($_GET['brewery']);
      // check if no brewery or brewery doesn't belong to user
      if (!$brewery || $brewery-&gt;user_id != $currentUser-&gt;id) {
          // error
      }
  // get relevant data for editing a beer
  } elseif (isset($_GET['id'])) {
      $beer = $beerDAO-&gt;getBeerById($_GET['id']);
      if ($beer) {
          $brewery = $breweryDAO-&gt;getBreweryById($beer-&gt;id);
      }
      // check if no beer, no brewery, or brewery doesn't belong to user 
      if (!$beer || empty($brewery) || $brewery-&gt;user_id != $currentUser-&gt;id) {
          // error
      }
  } else {
      // error
  }
  </code></pre>
  
  <p><strong>Option 2</strong>: Using a single queries with JOINs</p>
  
  <pre><code>// get relevant data for adding a new beer
  if (isset($_GET['brewery'])) {
      // get user's brewery
      $brewery = $breweryDAO-&gt;getBreweryByIdAndUser($_GET['brewery'], $currentUser-&gt;id);
      // check if no brewery was found 
      if (!$brewery) {
          // error
      }
  // get relevant data for editing an existing beer
  } elseif (isset($_GET['id'])) {
      // get user's beer AND brewery information all together
      $beer = $beerDAO-&gt;getBeerByIdAndUser($_GET['id'], $currentUser-&gt;id);
      // check if no beer was found
      if (!$beer) {
          // error
      }
  } else {
      // error
  }
  </code></pre>
  
  <p>When retrieving a beer, is it better to make separate queries (<strong>Option 1</strong>) or use single JOINed queries (<strong>Option 2</strong>) ? My concern is about which would be a better approach to querying multiple information that depend on each other. I usually do <strong>Option 1</strong>, but it can get pretty messy the more relationships there are. I am not too concerned about speed. Is there another way to structure my code as both options seem messy?</p>