diff --git a/include/class.orm.php b/include/class.orm.php index 02e5d738b965b6e4c963a6d78afc28d5546ba91e..8e02c3f7fd29c6c6ad3a41286aa987f3ea411931 100644 --- a/include/class.orm.php +++ b/include/class.orm.php @@ -229,6 +229,17 @@ class ModelMeta implements ArrayAccess { return $this->fields; } + function getByPath($path) { + if (is_string($path)) + $path = explode('__', $path); + $root = $this; + foreach ($path as $P) { + list($root, ) = $root['joins'][$P]['fkey']; + $root = $root::getMeta(); + } + return $root; + } + /** * Create a new instance of the model, optionally hydrating it with the * given hash table. The constructor is not called, which leaves the @@ -2249,9 +2260,22 @@ class SqlCompiler { // Handle relationship comparisons with model objects elseif ($value instanceof VerySimpleModel) { $criteria = array(); - foreach ($value->pk as $f=>$v) { - $f = $field . '__' . $f; - $criteria[$f] = $v; + // Avoid a join if possible. Use the local side of the + // relationship + if (count($value->pk) === 1) { + $path = explode('__', $field); + $relationship = array_pop($path); + $lmodel = $model::getMeta()->getByPath($path); + $local = $lmodel['joins'][$relationship]['local']; + $path = $path ? (implode('__', $path) . '__') : ''; + foreach ($value->pk as $v) { + $criteria["{$path}{$local}"] = $v; + } + } + else { + foreach ($value->pk as $f=>$v) { + $criteria["{$field}__{$f}"] = $v; + } } $filter[] = $this->compileQ(new Q($criteria), $model, $slot); } diff --git a/include/staff/ticket-view.inc.php b/include/staff/ticket-view.inc.php index c634971b3fd1dc97a10008b8b95344b4ce69393c..36c1d34ef922b6606fa3df26c61f73a3cd4893b1 100644 --- a/include/staff/ticket-view.inc.php +++ b/include/staff/ticket-view.inc.php @@ -309,7 +309,7 @@ if($ticket->isOverdue()) <span id="user-<?php echo $ticket->getOwnerId(); ?>-email"><?php echo $ticket->getEmail(); ?></span> </td> </tr> -<?php if ($user->getOrgId()) { ?> +<?php if ($user->getOrganization()) { ?> <tr> <th><?php echo __('Organization'); ?>:</th> <td><i class="icon-building"></i>