diff --git a/src/ORM/BelongsTo.php b/src/ORM/BelongsTo.php new file mode 100644 index 0000000..2274f3e --- /dev/null +++ b/src/ORM/BelongsTo.php @@ -0,0 +1,24 @@ +foreignObject = $foreignObject; + $this->localField = $localField; + $this->remoteField = $remoteField; + $this->localElement = $localElement; + } + + + function get() { + return $this->foreignObject::findOne(Array($this->remoteField => Array('=', $this->localElement->{$this->localField}))); + } + +} diff --git a/src/ORM/BelongsToMany.php b/src/ORM/BelongsToMany.php new file mode 100644 index 0000000..20137fd --- /dev/null +++ b/src/ORM/BelongsToMany.php @@ -0,0 +1,49 @@ +localObject = $localObject; + $this->foreignObject = $foreignObject; + $this->pivotTable = $pivotTable; + $this->localInPivot = $localInPivot; + $this->remoteInPivot = $remoteInPivot; + $this->remoteFilter = $remoteFilter; + $this->remoteLimit = $remoteLimit; + } + + function get() { + + $limitsSql = ''; + foreach ($this->remoteLimit as $key => $value) { + $limitsSql .= "$key $value "; + } + + $pivotTable = '{' . $this->pivotTable . '}'; + $sql = "SELECT $this->remoteInPivot FROM $pivotTable WHERE $this->localInPivot = ? $limitsSql"; + + $relations = DBInstance::query($sql, Array($this->localObject->id), $this->localObject::_connectionName); + + $ids = Array(); + foreach ($relations as $relation) { + $ids[] = $relation->{$this->remoteInPivot}; + } + + if (empty($ids)) { + return Array(); + } + + return $this->foreignObject::findMany(array_merge(Array('id' => Array('IN', $ids)), $this->remoteFilter), Array('*'), $this->remoteLimit); + } + +} diff --git a/src/ORM/BelongsToManyExtended.php b/src/ORM/BelongsToManyExtended.php new file mode 100644 index 0000000..d1fb14c --- /dev/null +++ b/src/ORM/BelongsToManyExtended.php @@ -0,0 +1,53 @@ +localObject = $localObject; + $this->foreignObject = $foreignObject; + $this->pivotTable = $pivotTable; + $this->localInPivot = $localInPivot; + $this->remoteInPivot = $remoteInPivot; + $this->remoteFilter = $remoteFilter; + $this->remoteLimit = $remoteLimit; + } + + function get(){ + + $limitsSql = ''; + foreach ($this->remoteLimit as $key => $value) { + $limitsSql .= "$key $value "; + } + + $pivotTable = '{' . $this->pivotTable . '}'; + $sql = "SELECT * FROM $pivotTable WHERE $this->localInPivot = ? $limitsSql"; + + $relations = DBInstance::query($sql, Array($this->localObject->id), $this->localObject::_connectionName); + $objects = Array(); + + if (empty($relations)) { + return Array(); + } + + foreach ($relations as $relation) { + $relation->childElement = $this->foreignObject::findOne(array_merge(Array("id" => Array("=", $relation->{$this->remoteInPivot})), $this->remoteFilter)); + $objects[] = $relation; + } + + if (empty($objects)) { + return Array(); + } + return $objects; + } + +} diff --git a/src/ORM/HasMany.php b/src/ORM/HasMany.php new file mode 100644 index 0000000..a8a422f --- /dev/null +++ b/src/ORM/HasMany.php @@ -0,0 +1,21 @@ +foreignObject = $foreignObject; + $this->remoteField = $remoteField; + $this->localElement = $localElement; + } + + function get() { + return $this->foreignObject::findMany(Array($this->remoteField => Array('=', $this->localElement->id))); + } + +} diff --git a/src/ORM/HasOne.php b/src/ORM/HasOne.php new file mode 100644 index 0000000..23996eb --- /dev/null +++ b/src/ORM/HasOne.php @@ -0,0 +1,22 @@ +foreignObject = $foreignObject; + $this->remoteField = $remoteField; + $this->localElement = $localElement; + } + + function get(){ + return $this->foreignObject::findOne(Array($this->remoteField => Array('=', $this->localElement->id))); + } + +}