Creating relations abstract classes
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace ORM;
|
||||
|
||||
class BelongsToMany {
|
||||
|
||||
private $localObject;
|
||||
private $foreignObject;
|
||||
private $pivotTable;
|
||||
private $localInPivot;
|
||||
private $remoteInPivot;
|
||||
private $remoteFilter;
|
||||
private $remoteLimit;
|
||||
|
||||
function __construct($localObject, $foreignObject, $pivotTable, $localInPivot, $remoteInPivot, $remoteFilter = Array(), $remoteLimit = Array()) {
|
||||
$this->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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user