123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <?php
- namespace Juju\ORM {
- require_once(realpath(dirname(__DIR__).'/earray.class.php'));
- use \Juju\EArray;
- use \Juju\ORM;
- class Relationship extends EArray {
- private $model;
- private $name;
- private $alias;
- private $removed = [];
- private $added = [];
- public static function from(ORM $model, string $name, array $alias, array $data){
- $earray = parent::from($data);
- $earray->model = $model;
- $earray->name = $name;
- $earray->alias = $alias;
- return $earray
- ->on('set', function(&$offset, &$value) use($earray){
- $earray->added = array_merge($earray->added, [$value]);
- $earray->removed = array_diff($earray->removed, [$value]);
- })->on('unset', function($offset, $value) use($earray){
- $earray->removed = array_merge($earray->removed, [$value]);
- $earray->added = array_diff($earray->added, [$value]);
- });
- }
- public function add(ORM $model){
- $this[] = $model;
- return $this;
- }
- public function remove(ORM $model){
- $index = $this->index($model);
- if($index !== false){
- unset($this->data[$index]);
- }
- return $this;
- }
- public function has(ORM $model){
- return $this->index($model) !== false;
- }
- public function index(ORM $model){
- return array_search($model, $this->data);
- }
- public function dirty(){
- return count($this->removed) + count($this->added) > 0;
- }
- public function reset(){
- foreach($this->added as $model){
- $index = array_search($model, $this->data);
- if($index !== false){
- unset($this->data[$index]);
- }
- }
- $this->added = [];
- foreach($this->removed as $model){
- $this->data = array_merge($this->data, [$model]);
- }
- $this->removed = [];
- }
- public function save(){
- if($this->dirty()){
- $model = $this->model;
- $alias = $this->$alias;
- $name = $this->name;
- $class = "\\Models\\{$alias['model']}";
- if(isset($model->has_many[$name])){
- throw new \Exception("Invalid relationship {$name}");
- }
- if(isset($alias['through'])){
- $rem_sql = "delete from {$alias['through']} where {$alias['foreign_key']} = ? and ".$class::table_name().$class::foreign_key_suffix()." = ?";
- $rem_args = [$model->id];
- $add_sql = "insert into {$alias['through']} ({$alias['foreign_key']}, ".$class::table_name().$class::foreign_key_suffix().") values (?, ?)";
- }else{
- $rem_sql = "update {$alias['model']} set {$alias['foreign_key']} = null where ".$class::primary_key()." = ?";
- $rem_args = [];
- $add_sql .= "update {$alias['model']} set {$alias['foreign_key']} = ? where ".$class::primary_key()." = ?";
- }
- $class = get_class($models);
- foreach($this->removed as $item){
- $class::query(
- $rem_sql,
- 'ii',
- ...array_merge($rem_args, [$item->id])
- )->execute();
- }
- $this->removed = [];
- foreach($this->added as $item){
- $class::query(
- $add_sql,
- 'ii',
- $model->id,
- $item->id
- )->execute();
- }
- $this->added = [];
- }
- }
- }
- }
- ?>
|