|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Mehedi\LaravelDynamoDB\Eloquent\Relations; |
| 4 | + |
| 5 | +use Illuminate\Database\Eloquent\Model; |
| 6 | +use Illuminate\Database\Eloquent\Relations\HasOne as BaseHasOne; |
| 7 | + |
| 8 | +class HasOne extends BaseHasOne |
| 9 | +{ |
| 10 | + public function addConstraints() |
| 11 | + { |
| 12 | + if (static::$constraints) { |
| 13 | + /** @var \Mehedi\LaravelDynamoDB\Eloquent\Builder $query */ |
| 14 | + $query = $this->getRelationQuery(); |
| 15 | + |
| 16 | + $query->query->key([ |
| 17 | + $this->getForeignPartitionKeyName() => $this->getForeignPartionKey(), |
| 18 | + $this->getForeignSortKeyName() => $this->getForeignSortKey(), |
| 19 | + ]); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * Get foreign partition key name |
| 25 | + * |
| 26 | + * @return string |
| 27 | + */ |
| 28 | + public function getForeignPartitionKeyName() |
| 29 | + { |
| 30 | + return $this->foreignKey[0]; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Get foreign partition key name |
| 35 | + * |
| 36 | + * @return string |
| 37 | + */ |
| 38 | + public function getForeignPartionKey() |
| 39 | + { |
| 40 | + return $this->parent->getAttribute($this->localKey[0]); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Get foreign sort key name |
| 45 | + * |
| 46 | + * @return mixed |
| 47 | + */ |
| 48 | + public function getForeignSortKeyName() |
| 49 | + { |
| 50 | + return $this->foreignKey[1]; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Get foreign partition key value |
| 55 | + * |
| 56 | + * @return string |
| 57 | + */ |
| 58 | + public function getForeignSortKey() |
| 59 | + { |
| 60 | + return $this->parent->getAttribute($this->localKey[1]); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get the results of the relationship. |
| 65 | + * |
| 66 | + * @return mixed |
| 67 | + */ |
| 68 | + public function getResults() |
| 69 | + { |
| 70 | + if (is_null($this->getParentKey())) { |
| 71 | + return $this->getDefaultFor($this->parent); |
| 72 | + } |
| 73 | + |
| 74 | + return $this->query->find(null) ?: $this->getDefaultFor($this->parent); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the key value of the parent's local key. |
| 79 | + * |
| 80 | + * @return mixed |
| 81 | + */ |
| 82 | + public function getParentKey() |
| 83 | + { |
| 84 | + return $this->parent->getKey(); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Set the constraints for an eager load of the relation. |
| 89 | + * |
| 90 | + * @param array $models |
| 91 | + * @return void |
| 92 | + */ |
| 93 | + public function addEagerConstraints(array $models) |
| 94 | + { |
| 95 | + dd($models); |
| 96 | + $whereIn = $this->whereInMethod($this->parent, $this->localKey); |
| 97 | + |
| 98 | + $this->getRelationQuery()->{$whereIn}( |
| 99 | + $this->foreignKey, $this->getKeys($models, $this->localKey) |
| 100 | + ); |
| 101 | + } |
| 102 | +} |
0 commit comments