| @@ 13-47 (lines=35) @@ | ||
| 10 | */ |
|
| 11 | namespace Pulsar\Relation; |
|
| 12 | ||
| 13 | class BelongsTo extends Relation |
|
| 14 | { |
|
| 15 | protected function initQuery() |
|
| 16 | { |
|
| 17 | $value = $this->relation->{$this->localKey}; |
|
| 18 | ||
| 19 | if ($value === null) { |
|
| 20 | $this->empty = true; |
|
| 21 | } |
|
| 22 | ||
| 23 | $this->query->where($this->foreignKey, $value) |
|
| 24 | ->limit(1); |
|
| 25 | } |
|
| 26 | ||
| 27 | public function getResults() |
|
| 28 | { |
|
| 29 | if ($this->empty) { |
|
| 30 | return; |
|
| 31 | } |
|
| 32 | ||
| 33 | return $this->query->first(); |
|
| 34 | } |
|
| 35 | ||
| 36 | public function create(array $values = []) |
|
| 37 | { |
|
| 38 | $class = $this->model; |
|
| 39 | $model = new $class($values); |
|
| 40 | $model->save(); |
|
| 41 | ||
| 42 | $this->relation->{$this->localKey} = $model->{$this->foreignKey}; |
|
| 43 | $this->relation->save(); |
|
| 44 | ||
| 45 | return $model; |
|
| 46 | } |
|
| 47 | } |
|
| 48 | ||
| @@ 15-47 (lines=33) @@ | ||
| 12 | ||
| 13 | use Pulsar\Query; |
|
| 14 | ||
| 15 | class HasOne extends Relation |
|
| 16 | { |
|
| 17 | protected function initQuery() |
|
| 18 | { |
|
| 19 | $value = $this->relation->{$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->model; |
|
| 41 | $model = new $class($values); |
|
| 42 | $model->{$this->foreignKey} = $this->relation->{$this->localKey}; |
|
| 43 | $model->save(); |
|
| 44 | ||
| 45 | return $model; |
|
| 46 | } |
|
| 47 | } |
|
| 48 | ||