1
0

relationship.class.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Juju\ORM;
  3. require_once(realpath(dirname(__DIR__).'/Data/earray.class.php'));
  4. use \Juju\{Data\EArray, ORM};
  5. class Relationship extends EArray {
  6. private $model;
  7. private $name;
  8. private $alias;
  9. private $removed = [];
  10. private $added = [];
  11. private $table;
  12. public static function from(ORM $model, string $name, array $alias, array $data){
  13. return new Relationship([
  14. 'data'=> $data,
  15. 'model'=>$model,
  16. 'name'=>$name,
  17. 'alias'=>$alias
  18. ]);
  19. }
  20. protected function __construct(array $data){
  21. parent::__construct($data['data']);
  22. $this->model = $data['model'];
  23. $this->name = $data['name'];
  24. $this->alias = $data['alias'];
  25. if(isset($this->alias['through'])){
  26. $this->table = $this->model->pdo->table($this->alias['through']);
  27. }
  28. $self = $this;
  29. $this
  30. ->on('set', function(&$offset, &$value) use($self){
  31. $self->added = array_merge($self->added, [$value]);
  32. $self->removed = array_diff($self->removed, [$value]);
  33. })->on('unset', function($offset, $value) use($self){
  34. $self->removed = array_merge($self->removed, [$value]);
  35. $self->added = array_diff($self->added, [$value]);
  36. });
  37. }
  38. public function add(ORM $model){
  39. $this[$model->id] = $model;
  40. return $this;
  41. }
  42. public function remove(ORM $model){
  43. $index = $this->index($model);
  44. if($index !== false){
  45. unset($this->data[$index]);
  46. }
  47. return $this;
  48. }
  49. public function has(ORM $model){
  50. return $this->index($model) !== false;
  51. }
  52. public function index(ORM $model){
  53. return array_search($model, $this->data);
  54. }
  55. public function dirty(){
  56. return count($this->removed) + count($this->added) > 0;
  57. }
  58. public function reset(){
  59. foreach($this->added as $model){
  60. $index = array_search($model, $this->data);
  61. if($index !== false){
  62. unset($this->data[$index]);
  63. }
  64. }
  65. $this->added = [];
  66. foreach($this->removed as $model){
  67. $this->data = array_merge($this->data, [$model]);
  68. }
  69. $this->removed = [];
  70. }
  71. public function save(){
  72. if($this->dirty()){
  73. $model = $this->model;
  74. $alias = $this->alias;
  75. $name = $this->name;
  76. $class = "\\Models\\{$alias['model']}";
  77. if(!isset($model->has_many[$name])){
  78. throw new \Exception("Invalid relationship {$name}");
  79. }
  80. $rem_args = [];
  81. if(isset($alias['through'])){
  82. $key = $class::table_name().$class::foreign_key_suffix();
  83. $filter = [
  84. $alias['foreign_key']=> $model->id
  85. ];
  86. foreach($this->removed as $item){
  87. $filter[$key] = $item->id;
  88. $this->table->delete($filter);
  89. }
  90. foreach($this->added as $item){
  91. $filter[$key] = $item->id;
  92. $this->table->insert($filter);
  93. }
  94. }else{
  95. $table = $model->pdo->table($alias['model']);
  96. $key = $class::primary_key();
  97. $data = [
  98. $alias['foreign_key']=> null
  99. ];
  100. foreach($this->removed as $item){
  101. $table->update($data, [
  102. $key=> $item->id
  103. ]);
  104. }
  105. $data = [
  106. $alias['foreign_key']=> $model->id
  107. ];
  108. foreach($this->added as $item){
  109. $table->update($data, [
  110. $key=> $item->id
  111. ]);
  112. }
  113. }
  114. $this->removed = [];
  115. $this->added = [];
  116. }
  117. }
  118. public function offsetSet($offset, $value){
  119. $this->fire('set', $value->id, $value);
  120. $this->data[$value->id] = $value;
  121. }
  122. public function filter(array $filter){
  123. $items = [];
  124. foreach($this->data as $item){
  125. $item->load();
  126. foreach($filter as $key => $value){
  127. $val = $item[$key];
  128. if($val !== $value){
  129. unset($item);
  130. break;
  131. }
  132. }
  133. if(isset($item)){
  134. $items[$item->id] = $item;
  135. }
  136. }
  137. unset($item);
  138. return $items;
  139. }
  140. }
  141. ?>