relationship.class.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. require_once('earray.class.php');
  3. class Relationship extends EArray {
  4. private $model;
  5. private $name;
  6. private $alias;
  7. private $removed = [];
  8. private $added = [];
  9. public static function from(ORM $model, string $name, array $alias, array $data){
  10. $earray = parent::from($data);
  11. $earray->model = $model;
  12. $earray->name = $name;
  13. $earray->alias = $alias;
  14. return $earray
  15. ->on('set', function(&$offset, &$value) use($earray){
  16. $earray->added = array_merge($earray->added, [$value]);
  17. $earray->removed = array_diff($earray->removed, [$value]);
  18. })->on('unset', function($offset, $value) use($earray){
  19. $earray->removed = array_merge($earray->removed, [$value]);
  20. $earray->added = array_diff($earray->added, [$value]);
  21. });
  22. }
  23. public function add(ORM $model){
  24. $this[] = $model;
  25. return $this;
  26. }
  27. public function remove(ORM $model){
  28. $index = $this->index($model);
  29. if($index !== false){
  30. unset($this->data[$index]);
  31. }
  32. return $this;
  33. }
  34. public function has(ORM $model){
  35. return $this->index($model) !== false;
  36. }
  37. public function index(ORM $model){
  38. return array_search($model, $this->data);
  39. }
  40. public function dirty(){
  41. return count($this->removed) + count($this->added) > 0;
  42. }
  43. public function reset(){
  44. foreach($this->added as $model){
  45. $index = array_search($model, $this->data);
  46. if($index !== false){
  47. unset($this->data[$index]);
  48. }
  49. }
  50. $this->added = [];
  51. foreach($this->removed as $model){
  52. $this->data = array_merge($this->data, [$model]);
  53. }
  54. $this->removed = [];
  55. }
  56. public function save(){
  57. if($this->dirty()){
  58. $model = $this->model;
  59. $alias = $this->$alias;
  60. $name = $this->name;
  61. $class = "Models\\{$alias['model']}";
  62. if(isset($model->has_many[$name])){
  63. throw new Exception("Invalid relationship {$name}");
  64. }
  65. if(isset($alias['through'])){
  66. $rem_sql = "delete from {$alias['through']} where {$alias['foreign_key']} = ? and ".$class::table_name().$class::foreign_key_suffix()." = ?";
  67. $rem_args = [$model->id];
  68. $add_sql = "insert into {$alias['through']} ({$alias['foreign_key']}, ".$class::table_name().$class::foreign_key_suffix().") values (?, ?)";
  69. }else{
  70. $rem_sql = "update {$alias['model']} set {$alias['foreign_key']} = null where ".$class::primary_key()." = ?";
  71. $rem_args = [];
  72. $add_sql .= "update {$alias['model']} set {$alias['foreign_key']} = ? where ".$class::primary_key()." = ?";
  73. }
  74. $class = get_class($models);
  75. foreach($this->removed as $item){
  76. $class::query(
  77. $rem_sql,
  78. 'ii',
  79. ...array_merge($rem_args, [$item->id])
  80. )->execute();
  81. }
  82. $this->removed = [];
  83. foreach($this->added as $item){
  84. $class::query(
  85. $add_sql,
  86. 'ii',
  87. $model->id,
  88. $item->id
  89. )->execute();
  90. }
  91. $this->added = [];
  92. }
  93. }
  94. }
  95. ?>