Moving the relations to a dedicated Namespace

This commit is contained in:
2026-08-19 10:59:49 -03:00
parent 64bce1da47
commit 46dd177be9
7 changed files with 124 additions and 46 deletions
-21
View File
@@ -1,21 +0,0 @@
<?php
namespace ORM;
class HasMany {
private $foreignObject;
private $remoteField;
private $localElement;
function __construct($foreignObject, $remoteField, $localElement) {
$this->foreignObject = $foreignObject;
$this->remoteField = $remoteField;
$this->localElement = $localElement;
}
function get() {
return $this->foreignObject::findMany(Array($this->remoteField => Array('=', $this->localElement->id)));
}
}
-22
View File
@@ -1,22 +0,0 @@
<?php
namespace ORM
;
class HasOne{
private $foreignObject;
private $remoteField;
private $localElement;
function __construct($foreignObject, $remoteField, $localElement) {
$this->foreignObject = $foreignObject;
$this->remoteField = $remoteField;
$this->localElement = $localElement;
}
function get(){
return $this->foreignObject::findOne(Array($this->remoteField => Array('=', $this->localElement->id)));
}
}
@@ -1,7 +1,19 @@
<?php <?php
namespace ORM; namespace ORM\Relations;
/**
* BelongsTo
*
* Defines that, this object is part of an other class.
* The local field must match the id of an other class.
*
* Table Post = (id, name, content)
* Table Comment = (id, name, content, postid)
*
* This relation will be created on the comment class
* Making reference to the post class
*/
class BelongsTo { class BelongsTo {
private $foreignObject; private $foreignObject;
@@ -1,7 +1,22 @@
<?php <?php
namespace ORM; namespace ORM\Relations;
use ORM\DBInstance;
use ORM\Collection;
/**
* Belongs to Many
*
* Defines that this object is related to many other instances
* of other classes throw a pivot table.
*
* Table Post = (id, name, content)
* Table Tag = (id, name)
* Table Post_Tag = (id, postid, tagid)
*
* This relation will be created in both classes
*/
class BelongsToMany { class BelongsToMany {
private $localObject; private $localObject;
@@ -12,6 +27,17 @@ class BelongsToMany {
private $remoteFilter; private $remoteFilter;
private $remoteLimit; private $remoteLimit;
/**
* Build the Connection
*
* @param mixed $localObject
* @param mixed $foreignObject
* @param mixed $pivotTable
* @param mixed $localInPivot
* @param mixed $remoteInPivot
* @param mixed $remoteFilter
* @param mixed $remoteLimit
*/
function __construct($localObject, $foreignObject, $pivotTable, $localInPivot, $remoteInPivot, $remoteFilter = Array(), $remoteLimit = Array()) { function __construct($localObject, $foreignObject, $pivotTable, $localInPivot, $remoteInPivot, $remoteFilter = Array(), $remoteLimit = Array()) {
$this->localObject = $localObject; $this->localObject = $localObject;
$this->foreignObject = $foreignObject; $this->foreignObject = $foreignObject;
@@ -1,6 +1,9 @@
<?php <?php
namespace ORM; namespace ORM\Relations;
use ORM\DBInstance;
use ORM\Collection;
class BelongsToManyExtended { class BelongsToManyExtended {
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace ORM\Relations;
/**
* HasMany
*
* Defines that, this object has many instances of other Object
*
* Table Post = (id, name, content)
* Table Comment = (id, name, content, postid)
*
* This relation will be created on the Post class
* Making reference to the Comment class
*/
class HasMany
{
private $foreignObject;
private $remoteField;
private $localElement;
/**
* Return the Relation
*
* @param mixed $foreignObject
* @param mixed $remoteField
* @param mixed $localElement
*/
function __construct($foreignObject, $remoteField, $localElement)
{
$this->foreignObject = $foreignObject;
$this->remoteField = $remoteField;
$this->localElement = $localElement;
}
/**
* Get the data of the relation
*/
function get()
{
return $this->foreignObject::findMany(array($this->remoteField => array('=', $this->localElement->id)));
}
}
+35
View File
@@ -0,0 +1,35 @@
<?php
namespace ORM\Relations;
/**
* Has One
*
* Defines that, this object has a child only one object in other class.
*
* Table user = (id, name)
* Table phone = (id, number, userid)
*
* This relation will be created on the User class
* making reference to the Phone class
*/
class HasOne
{
private $foreignObject;
private $remoteField;
private $localElement;
function __construct($foreignObject, $remoteField, $localElement)
{
$this->foreignObject = $foreignObject;
$this->remoteField = $remoteField;
$this->localElement = $localElement;
}
function get()
{
return $this->foreignObject::findOne(array($this->remoteField => array('=', $this->localElement->id)));
}
}