From 268d4c24c777e629a06c0886dde16d725bcf9932 Mon Sep 17 00:00:00 2001
From: Jared Hancock <jared@osticket.com>
Date: Fri, 28 Nov 2014 21:14:29 -0600
Subject: [PATCH] orm: Add support for a view model

Define a static ::getQuery() method and define 'view'=>true in the model
meta data and the SQL code retrieved from getQuery will be used inline when
joining the model rather than using a table.
---
 include/class.orm.php | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/include/class.orm.php b/include/class.orm.php
index 3508d9273..8d32361b8 100644
--- a/include/class.orm.php
+++ b/include/class.orm.php
@@ -33,6 +33,7 @@ class ModelMeta implements ArrayAccess {
         'table' => false,
         'defer' => array(),
         'select_related' => array(),
+        'view' => false,
     );
     var $model;
 
@@ -1605,7 +1606,11 @@ class MySqlCompiler extends SqlCompiler {
         if ($extra instanceof Q) {
             $constraints[] = $this->compileQ($extra, $model, self::SLOT_JOINS);
         }
-        return $join.$this->quote($rmodel::$meta['table'])
+        // Support inline views
+        $table = ($rmodel::$meta['view'])
+            ? $rmodel::getQuery($this)
+            : $this->quote($rmodel::$meta['table']);
+        return $join.$table
             .' '.$alias.' ON ('.implode(' AND ', $constraints).')';
     }
 
-- 
GitLab