|
| 1 | +<?php namespace Jenssegers\Eloquent; |
| 2 | + |
| 3 | +use Illuminate\Database\Eloquent\Relations\HasOne; |
| 4 | +use Illuminate\Database\Eloquent\Relations\HasMany; |
| 5 | +use Jenssegers\Mongodb\Relations\BelongsTo; |
| 6 | +use Jenssegers\Mongodb\Relations\BelongsToMany; |
| 7 | +use Jenssegers\Mongodb\Query\Builder as QueryBuilder; |
| 8 | + |
| 9 | +abstract class Model extends \Illuminate\Database\Eloquent\Model { |
| 10 | + |
| 11 | + /** |
| 12 | + * Define a one-to-one relationship. |
| 13 | + * |
| 14 | + * @param string $related |
| 15 | + * @param string $foreignKey |
| 16 | + * @param string $localKey |
| 17 | + * @return \Illuminate\Database\Eloquent\Relations\HasOne |
| 18 | + */ |
| 19 | + public function hasOne($related, $foreignKey = null, $localKey = null) |
| 20 | + { |
| 21 | + // Check if it is a relation with an original model. |
| 22 | + if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model')) |
| 23 | + { |
| 24 | + return parent::hasOne($related, $foreignKey, $localKey); |
| 25 | + } |
| 26 | + |
| 27 | + $foreignKey = $foreignKey ?: $this->getForeignKey(); |
| 28 | + |
| 29 | + $instance = new $related; |
| 30 | + |
| 31 | + $localKey = $localKey ?: $this->getKeyName(); |
| 32 | + |
| 33 | + return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Define a one-to-many relationship. |
| 38 | + * |
| 39 | + * @param string $related |
| 40 | + * @param string $foreignKey |
| 41 | + * @param string $localKey |
| 42 | + * @return \Illuminate\Database\Eloquent\Relations\HasMany |
| 43 | + */ |
| 44 | + public function hasMany($related, $foreignKey = null, $localKey = null) |
| 45 | + { |
| 46 | + // Check if it is a relation with an original model. |
| 47 | + if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model')) |
| 48 | + { |
| 49 | + return parent::hasMany($related, $foreignKey, $localKey); |
| 50 | + } |
| 51 | + |
| 52 | + $foreignKey = $foreignKey ?: $this->getForeignKey(); |
| 53 | + |
| 54 | + $instance = new $related; |
| 55 | + |
| 56 | + $localKey = $localKey ?: $this->getKeyName(); |
| 57 | + |
| 58 | + return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * Define an inverse one-to-one or many relationship. |
| 63 | + * |
| 64 | + * @param string $related |
| 65 | + * @param string $foreignKey |
| 66 | + * @param string $otherKey |
| 67 | + * @param string $relation |
| 68 | + * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
| 69 | + */ |
| 70 | + public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null) |
| 71 | + { |
| 72 | + // Check if it is a relation with an original model. |
| 73 | + if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model')) |
| 74 | + { |
| 75 | + return parent::belongsTo($related, $foreignKey, $otherKey, $relation); |
| 76 | + } |
| 77 | + |
| 78 | + // If no relation name was given, we will use this debug backtrace to extract |
| 79 | + // the calling method's name and use that as the relationship name as most |
| 80 | + // of the time this will be what we desire to use for the relatinoships. |
| 81 | + if (is_null($relation)) |
| 82 | + { |
| 83 | + list(, $caller) = debug_backtrace(false); |
| 84 | + |
| 85 | + $relation = $caller['function']; |
| 86 | + } |
| 87 | + |
| 88 | + // If no foreign key was supplied, we can use a backtrace to guess the proper |
| 89 | + // foreign key name by using the name of the relationship function, which |
| 90 | + // when combined with an "_id" should conventionally match the columns. |
| 91 | + if (is_null($foreignKey)) |
| 92 | + { |
| 93 | + $foreignKey = snake_case($relation).'_id'; |
| 94 | + } |
| 95 | + |
| 96 | + $instance = new $related; |
| 97 | + |
| 98 | + // Once we have the foreign key names, we'll just create a new Eloquent query |
| 99 | + // for the related models and returns the relationship instance which will |
| 100 | + // actually be responsible for retrieving and hydrating every relations. |
| 101 | + $query = $instance->newQuery(); |
| 102 | + |
| 103 | + $otherKey = $otherKey ?: $instance->getKeyName(); |
| 104 | + |
| 105 | + return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Define a many-to-many relationship. |
| 110 | + * |
| 111 | + * @param string $related |
| 112 | + * @param string $collection |
| 113 | + * @param string $foreignKey |
| 114 | + * @param string $otherKey |
| 115 | + * @param string $relation |
| 116 | + * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
| 117 | + */ |
| 118 | + public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null, $relation = null) |
| 119 | + { |
| 120 | + // Check if it is a relation with an original model. |
| 121 | + if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model')) |
| 122 | + { |
| 123 | + return parent::belongsToMany($related, $collection, $foreignKey, $otherKey, $relation); |
| 124 | + } |
| 125 | + |
| 126 | + // If no relationship name was passed, we will pull backtraces to get the |
| 127 | + // name of the calling function. We will use that function name as the |
| 128 | + // title of this relation since that is a great convention to apply. |
| 129 | + if (is_null($relation)) |
| 130 | + { |
| 131 | + $caller = $this->getBelongsToManyCaller(); |
| 132 | + |
| 133 | + $name = $caller['function']; |
| 134 | + } |
| 135 | + |
| 136 | + // First, we'll need to determine the foreign key and "other key" for the |
| 137 | + // relationship. Once we have determined the keys we'll make the query |
| 138 | + // instances as well as the relationship instances we need for this. |
| 139 | + $foreignKey = $foreignKey ?: $this->getForeignKey() . 's'; |
| 140 | + |
| 141 | + $instance = new $related; |
| 142 | + |
| 143 | + $otherKey = $otherKey ?: $instance->getForeignKey() . 's'; |
| 144 | + |
| 145 | + // If no table name was provided, we can guess it by concatenating the two |
| 146 | + // models using underscores in alphabetical order. The two model names |
| 147 | + // are transformed to snake case from their default CamelCase also. |
| 148 | + if (is_null($collection)) |
| 149 | + { |
| 150 | + $collection = $instance->getTable(); |
| 151 | + } |
| 152 | + |
| 153 | + // Now we're ready to create a new query builder for the related model and |
| 154 | + // the relationship instances for the relation. The relations will set |
| 155 | + // appropriate query constraint and entirely manages the hydrations. |
| 156 | + $query = $instance->newQuery(); |
| 157 | + |
| 158 | + return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $relation); |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Get a new query builder instance for the connection. |
| 163 | + * |
| 164 | + * @return Builder |
| 165 | + */ |
| 166 | + protected function newBaseQueryBuilder() |
| 167 | + { |
| 168 | + $connection = $this->getConnection(); |
| 169 | + |
| 170 | + // Check the connection type |
| 171 | + if ($connection instanceof \Jenssegers\Mongodb\Connection) |
| 172 | + { |
| 173 | + return new QueryBuilder($connection); |
| 174 | + } |
| 175 | + |
| 176 | + return parent::newBaseQueryBuilder(); |
| 177 | + } |
| 178 | + |
| 179 | +} |
0 commit comments