From 977f7dec2aa816136d0144747ff04a3687277e62 Mon Sep 17 00:00:00 2001 From: Andrey Kutejko Date: Sat, 17 Aug 2013 10:00:57 +0300 Subject: [PATCH] separate relation dql --- ipf/orm/relation.php | 11 +---------- ipf/orm/relation/association.php | 22 ++++++++++------------ ipf/orm/relation/foreignkey.php | 14 +++++++++++++- ipf/orm/relation/localkey.php | 8 ++++++++ 4 files changed, 32 insertions(+), 23 deletions(-) diff --git a/ipf/orm/relation.php b/ipf/orm/relation.php index da16b0e..04284ec 100644 --- a/ipf/orm/relation.php +++ b/ipf/orm/relation.php @@ -157,16 +157,7 @@ abstract class IPF_ORM_Relation implements ArrayAccess $this->definition['type'] == IPF_ORM_Relation::ONE_COMPOSITE); } - public function getRelationDql($count) - { - $component = $this->getTable()->getComponentName(); - - $dql = 'FROM ' . $component - . ' WHERE ' . $component . '.' . $this->definition['foreign'] - . ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')'; - - return $dql; - } + abstract public function getRelationDql($count, $context); abstract public function fetchRelatedFor(IPF_ORM_Record $record); diff --git a/ipf/orm/relation/association.php b/ipf/orm/relation/association.php index 93dfffc..29fc2b2 100644 --- a/ipf/orm/relation/association.php +++ b/ipf/orm/relation/association.php @@ -11,23 +11,20 @@ class IPF_ORM_Relation_Association extends IPF_ORM_Relation return $this->definition['refTable']; } - public function getRelationDql($count, $context = 'record') + public function getRelationDql($count, $context) { - $table = $this->definition['refTable']; $component = $this->definition['refTable']->getComponentName(); + $thisTable = $this->getTable()->getComponentName(); + $sub = substr(str_repeat('?, ', $count), 0, -2); switch ($context) { case "record": - $sub = substr(str_repeat("?, ", $count),0,-2); - $dql = 'FROM ' . $this->getTable()->getComponentName(); - $dql .= '.' . $component; - $dql .= ' WHERE ' . $this->getTable()->getComponentName() - . '.' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')'; + $dql = 'FROM ' . $thisTable . '.' . $component . + ' WHERE ' . $thisTable . '.' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')'; break; case "collection": - $sub = substr(str_repeat("?, ", $count),0,-2); - $dql = 'FROM ' . $component . '.' . $this->getTable()->getComponentName(); - $dql .= ' WHERE ' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')'; + $dql = 'FROM ' . $component . '.' . $thisTable . + ' WHERE ' . $component . '.' . $this->definition['local'] . ' IN (' . $sub . ')'; break; } return $dql; @@ -39,9 +36,10 @@ class IPF_ORM_Relation_Association extends IPF_ORM_Relation if (empty($id) || ! $this->definition['table']->getAttribute(IPF_ORM::ATTR_LOAD_REFERENCES)) { $coll = new IPF_ORM_Collection($this->getTable()); } else { - $coll = $this->getTable()->getConnection()->query($this->getRelationDql(1), array($id)); + $coll = $this->getTable()->getConnection()->query($this->getRelationDql(1, 'record'), array($id)); } $coll->setReference($record, $this); return $coll; } -} \ No newline at end of file +} + diff --git a/ipf/orm/relation/foreignkey.php b/ipf/orm/relation/foreignkey.php index 8830a4b..b0c64f0 100644 --- a/ipf/orm/relation/foreignkey.php +++ b/ipf/orm/relation/foreignkey.php @@ -34,7 +34,7 @@ class IPF_ORM_Relation_ForeignKey extends IPF_ORM_Relation $related = new IPF_ORM_Collection($this->getTable()); } else { - $query = $this->getRelationDql(1); + $query = $this->getRelationDql(1, ''); $related = $this->getTable()->getConnection()->query($query, $id); } $related->setReference($record, $this); @@ -42,6 +42,18 @@ class IPF_ORM_Relation_ForeignKey extends IPF_ORM_Relation return $related; } + public function getRelationDql($count, $context) + { + $table = $this->getTable(); + $component = $table->getComponentName(); + + $dql = 'FROM ' . $component + . ' WHERE ' . $component . '.' . $this->definition['foreign'] + . ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')'; + + return $dql; + } + public function getCondition($alias = null) { if ( ! $alias) { diff --git a/ipf/orm/relation/localkey.php b/ipf/orm/relation/localkey.php index 635402b..edd225f 100644 --- a/ipf/orm/relation/localkey.php +++ b/ipf/orm/relation/localkey.php @@ -28,6 +28,14 @@ class IPF_ORM_Relation_LocalKey extends IPF_ORM_Relation return $related; } + public function getRelationDql($count, $context) + { + $component = $this->getTable()->getComponentName(); + return 'FROM ' . $component . + ' WHERE ' . $component . '.' . $this->definition['foreign'] . + ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')'; + } + public function getCondition($alias = null) { if ( ! $alias) { -- 2.49.0