BelongsToManyExtended.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace ORM;
  3. class BelongsToManyExtended {
  4. private $localObject;
  5. private $foreignObject;
  6. private $pivotTable;
  7. private $localInPivot;
  8. private $remoteInPivot;
  9. private $remoteFilter;
  10. private $remoteLimit;
  11. function __construct($localObject, $foreignObject, $pivotTable, $localInPivot, $remoteInPivot, $remoteFilter = Array(), $remoteLimit = Array()) {
  12. $this->localObject = $localObject;
  13. $this->foreignObject = $foreignObject;
  14. $this->pivotTable = $pivotTable;
  15. $this->localInPivot = $localInPivot;
  16. $this->remoteInPivot = $remoteInPivot;
  17. $this->remoteFilter = $remoteFilter;
  18. $this->remoteLimit = $remoteLimit;
  19. }
  20. function get(){
  21. $limitsSql = '';
  22. foreach ($this->remoteLimit as $key => $value) {
  23. $limitsSql .= "$key $value ";
  24. }
  25. $pivotTable = '{' . $this->pivotTable . '}';
  26. $sql = "SELECT * FROM $pivotTable WHERE $this->localInPivot = ? $limitsSql";
  27. $relations = DBInstance::query($sql, Array($this->localObject->id), $this->localObject::_connectionName);
  28. $objects = Array();
  29. if (empty($relations)) {
  30. return Array();
  31. }
  32. foreach ($relations as $relation) {
  33. $relation->childElement = $this->foreignObject::findOne(array_merge(Array("id" => Array("=", $relation->{$this->remoteInPivot})), $this->remoteFilter));
  34. $objects[] = $relation;
  35. }
  36. if (empty($objects)) {
  37. return Array();
  38. }
  39. return $objects;
  40. }
  41. }