title: FuelPHP - How do I access a custom sql statement using ORM?
body: <h2>Question</h2>
  
  <p>Is it possible to create a custom SQL query and treat its results like you treat tables with FuelPHPs ORM?</p>
  
  <hr>
  
  <h2>Example</h2>
  
  <p>I have an SQL statement that pivots a table for me. The image below shows what the pivot SQL statement does. On the left we have the 'properties' table and on the right we have the result of the SQL pivot statement below.</p>
  
  <p><strong>SQL Pivot Statement Results:</strong>
  <img src="https://i.stack.imgur.com/mSPdk.png" alt="On the left we have the &#39;properties&#39; table and on the right we have the result of the following SQL pivot."></p>
  
  <p><strong>SQL Pivot Statement:</strong></p>
  
  <pre><code>SELECT
    item_id,
    MAX(IF(property_name = 'color', value, NULL)) AS color,
    MAX(IF(property_name = 'size', value, NULL)) AS size,
    ...
    ...
    ...
  FROM
    properties
  GROUP BY
    item_id;
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <p>Can I execute the above statement, then access the columns through the normal ORM methods such as </p>
  
  <pre><code>echo $table-&gt;color;
  $table-&gt;color = 'blue';
  $table-&gt;save();
  </code></pre>
  
  <hr>
  
  <p>Also, I looked at <a href="http://fuelphp.com/docs/packages/orm/eav.html" rel="nofollow noreferrer">FuelPHP EAV</a> and it looks like it might be what I need... but I couldn't get it working. Is it what I need? </p>
  
  <p>I got the code above from <a href="http://buysql.com/mysql/14-how-to-automate-pivot-tables.html" rel="nofollow noreferrer">a buysql.com tutorial on pivot tables</a>. It does exactly what I need but not sure how to integrate with ORM. </p>