title: Working with Phalcon relations
body: <p>I've started to learn programming with Phalcon a few weeks ago and I have a question about the relationships, which are used for 'easy' programming.</p>
  
  <p><strong>The setup</strong>
  I've got a table full with products (Products model obvious), a table with categories (Category model) and a table with subcategories (Subcategory model). A product CAN be in a category or a subcategory (depends if the category has subcategories).</p>
  
  <p>So, I've currently got 4 models<br />
  - Product (matches with <strong>id</strong>)<br />
  - Category (matches with <strong>id</strong>)<br />
  - Subcategory (matches with <strong>id</strong>)<br />
  - CatRelation (used for <strong>linking</strong> the products)<br /></p>
  
  <p>The CatRelation table contains a:<br />
  - productId<br />
  - satId<br />
  - subId<br /></p>
  
  <p>Where the catId/subId cannot be filled both.</p>
  
  <p>So, when I'm in a category, I want to use the model to fetch all the <strong>products</strong> within that category by <strong>catId</strong>. The same goes for the <strong>subcategory</strong>.</p>
  
  <p>My current models look like this:</p>
  
  <pre><code>class Product extends ModelBase
  {
      public $id;
      public $name;
  
      /**
       * @return string
       */
      public function getSource()
      {
          return "products";
      }
  
      public function initialize()
      {
          $this-&gt;hasMany("id", "CatRelation", "productId");
      }
  }
  
  class Category extends ModelBase
  {
      /**
       * @return string
       */
      public function getSource()
      {
          return "categories";
      }
  
      public function initialize()
      {
          $this-&gt;hasMany("id", "CatRelation", "catId");
      }
  } 
  
  class Subcategory extends ModelBase
  {
      /**
       * @return string
       */
      public function getSource()
      {
          return "subcategories";
      }
  
      public function initialize()
      {
          $this-&gt;hasMany("id", "CatRelation", "subId");
      }
  } 
  
  class CatRelation extends ModelBase
  {
      public $id;
  
      public $productId;
      public $catId;
      public $subId;
  
      /**
       * @return string
       */
      public function getSource()
      {
          return "productCatRelations";
      }
  
      public function initialize()
      {
          $this-&gt;belongsTo("productId", "Product", "id");
          $this-&gt;belongsTo("catId", "Category", "id");
          $this-&gt;belongsTo("subId", "Subcategory", "id");
      }
  } 
  </code></pre>
  
  <p>I hope someone out there is willing to help me out! :)
  If you have some suggestions, please suggest them! I'm still learning, so that's only helping me.</p>
  
  <p>Cheers.</p>