| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace ORM;
- class BelongsToManyExtended {
- private $localObject;
- private $foreignObject;
- private $pivotTable;
- private $localInPivot;
- private $remoteInPivot;
- private $remoteFilter;
- private $remoteLimit;
- private $softDelete;
- function __construct($localObject, $foreignObject, $pivotTable, $localInPivot, $remoteInPivot, $remoteFilter = Array(), $remoteLimit = Array(), $softDelete) {
- $this->localObject = $localObject;
- $this->foreignObject = $foreignObject;
- $this->pivotTable = $pivotTable;
- $this->localInPivot = $localInPivot;
- $this->remoteInPivot = $remoteInPivot;
- $this->remoteFilter = $remoteFilter;
- $this->remoteLimit = $remoteLimit;
- $this->softDelete = $softDelete;
- }
- function get() : Collection {
- $limitsSql = '';
- foreach ($this->remoteLimit as $key => $value) {
- $limitsSql .= "$key $value ";
- }
- $pivotTable = '{' . $this->pivotTable . '}';
- $sql = '';
- if($this->softDelete){
- $sql = "SELECT * FROM $pivotTable WHERE $this->localInPivot = ? AND deleted_at IS NULL $limitsSql";
- } else {
- $sql = "SELECT * FROM $pivotTable WHERE $this->localInPivot = ? $limitsSql";
- }
- $relations = DBInstance::query($sql, Array($this->localObject->id), $this->localObject::_connectionName);
- $objects = new Collection();
- if (empty($relations)) {
- return new Collection();
- }
- 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 new Collection();
- }
- return $objects;
- }
- }
|