summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord/Relations/TActiveRecordHasOne.php
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Data/ActiveRecord/Relations/TActiveRecordHasOne.php')
-rw-r--r--framework/Data/ActiveRecord/Relations/TActiveRecordHasOne.php42
1 files changed, 42 insertions, 0 deletions
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordHasOne.php b/framework/Data/ActiveRecord/Relations/TActiveRecordHasOne.php
new file mode 100644
index 00000000..048f776b
--- /dev/null
+++ b/framework/Data/ActiveRecord/Relations/TActiveRecordHasOne.php
@@ -0,0 +1,42 @@
+<?php
+Prado::using('System.Data.ActiveRecord.Relations.TActiveRecordRelation');
+
+class TActiveRecordHasOne extends TActiveRecordRelation
+{
+ /**
+ * Get the foreign key index values from the results and make calls to the
+ * database to find the corresponding foreign objects.
+ * @param array original results.
+ */
+ protected function collectForeignObjects(&$results)
+ {
+ $fkObject = $this->getContext()->getForeignRecordFinder();
+ $fkeys = $this->findForeignKeys($fkObject, $this->getSourceRecord());
+
+ $properties = array_values($fkeys);
+ $fields = array_keys($fkeys);
+
+ $indexValues = $this->getIndexValues($properties, $results);
+ $fkObjects = $this->findForeignObjects($fields,$indexValues);
+ $this->populateResult($results,$properties,$fkObjects,$fields);
+ }
+
+ /**
+ * Sets the foreign objects to the given property on the source object.
+ * @param TActiveRecord source object.
+ * @param array foreign objects.
+ */
+ protected function setObjectProperty($source, $properties, &$collections)
+ {
+ $hash = $this->getObjectHash($source, $properties);
+ $prop = $this->getContext()->getProperty();
+ if(isset($collections[$hash]) && count($collections[$hash]) > 0)
+ {
+ if(count($collections[$hash]) > 1)
+ throw new TActiveRecordException('ar_belongs_to_multiple_result');
+ $source->{$prop} = $collections[$hash][0];
+ }
+ }
+}
+
+?> \ No newline at end of file