| @@ 15-77 (lines=63) @@ | ||
| 12 | ||
| 13 | use Pulsar\Model; |
|
| 14 | ||
| 15 | class HasMany extends Relation |
|
| 16 | { |
|
| 17 | protected function initQuery() |
|
| 18 | { |
|
| 19 | $localKey = $this->localKey; |
|
| 20 | $value = $this->localModel->$localKey; |
|
| 21 | ||
| 22 | if ($value === null) { |
|
| 23 | $this->empty = true; |
|
| 24 | } |
|
| 25 | ||
| 26 | $this->query->where($this->foreignKey, $value); |
|
| 27 | } |
|
| 28 | ||
| 29 | public function getResults() |
|
| 30 | { |
|
| 31 | if ($this->empty) { |
|
| 32 | return; |
|
| 33 | } |
|
| 34 | ||
| 35 | return $this->query->execute(); |
|
| 36 | } |
|
| 37 | ||
| 38 | public function create(array $values = []) |
|
| 39 | { |
|
| 40 | $class = $this->foreignModel; |
|
| 41 | $model = new $class(); |
|
| 42 | $model->{$this->foreignKey} = $this->localModel->{$this->localKey}; |
|
| 43 | $model->create($values); |
|
| 44 | ||
| 45 | return $model; |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * Attaches a child model from this model. |
|
| 50 | * |
|
| 51 | * @param Model $model child model |
|
| 52 | * |
|
| 53 | * @return self |
|
| 54 | */ |
|
| 55 | public function attach(Model $model) |
|
| 56 | { |
|
| 57 | $model->{$this->foreignKey} = $this->localModel->{$this->localKey}; |
|
| 58 | $model->save(); |
|
| 59 | ||
| 60 | return $this; |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * Detaches a child model from this model. |
|
| 65 | * |
|
| 66 | * @param Model $model child model |
|
| 67 | * |
|
| 68 | * @return self |
|
| 69 | */ |
|
| 70 | public function detach(Model $model) |
|
| 71 | { |
|
| 72 | $model->{$this->foreignKey} = null; |
|
| 73 | $model->save(); |
|
| 74 | ||
| 75 | return $this; |
|
| 76 | } |
|
| 77 | } |
|
| 78 | ||
| @@ 15-79 (lines=65) @@ | ||
| 12 | ||
| 13 | use Pulsar\Model; |
|
| 14 | ||
| 15 | class HasOne extends Relation |
|
| 16 | { |
|
| 17 | protected function initQuery() |
|
| 18 | { |
|
| 19 | $value = $this->localModel->{$this->localKey}; |
|
| 20 | ||
| 21 | if ($value === null) { |
|
| 22 | $this->empty = true; |
|
| 23 | } |
|
| 24 | ||
| 25 | $this->query->where($this->foreignKey, $value) |
|
| 26 | ->limit(1); |
|
| 27 | } |
|
| 28 | ||
| 29 | public function getResults() |
|
| 30 | { |
|
| 31 | if ($this->empty) { |
|
| 32 | return; |
|
| 33 | } |
|
| 34 | ||
| 35 | return $this->query->first(); |
|
| 36 | } |
|
| 37 | ||
| 38 | public function create(array $values = []) |
|
| 39 | { |
|
| 40 | $class = $this->foreignModel; |
|
| 41 | $model = new $class(); |
|
| 42 | $model->{$this->foreignKey} = $this->localModel->{$this->localKey}; |
|
| 43 | $model->create($values); |
|
| 44 | ||
| 45 | return $model; |
|
| 46 | } |
|
| 47 | ||
| 48 | /** |
|
| 49 | * Attaches a child model to this model. |
|
| 50 | * |
|
| 51 | * @param Model $model child model |
|
| 52 | * |
|
| 53 | * @return self |
|
| 54 | */ |
|
| 55 | public function attach(Model $model) |
|
| 56 | { |
|
| 57 | $model->{$this->foreignKey} = $this->localModel->{$this->localKey}; |
|
| 58 | $model->save(); |
|
| 59 | ||
| 60 | return $this; |
|
| 61 | } |
|
| 62 | ||
| 63 | /** |
|
| 64 | * Detaches the child model from this model. |
|
| 65 | * |
|
| 66 | * @return self |
|
| 67 | */ |
|
| 68 | public function detach() |
|
| 69 | { |
|
| 70 | $model = $this->getResults(); |
|
| 71 | ||
| 72 | if ($model) { |
|
| 73 | $model->{$this->foreignKey} = null; |
|
| 74 | $model->save(); |
|
| 75 | } |
|
| 76 | ||
| 77 | return $this; |
|
| 78 | } |
|
| 79 | } |
|
| 80 | ||