Skip to content

Commit bbb7990

Browse files
committed
Model updated
1 parent 3046a09 commit bbb7990

File tree

1 file changed

+76
-62
lines changed

1 file changed

+76
-62
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use Jenssegers\Mongodb\DatabaseManager as Resolver;
88
use Jenssegers\Mongodb\Builder as QueryBuilder;
99
use Jenssegers\Mongodb\Relations\BelongsTo;
10+
use Jenssegers\Mongodb\Relations\BelongsToMany;
1011

11-
use Carbon\Carbon;
1212
use DateTime;
1313
use MongoId;
1414
use MongoDate;
@@ -67,25 +67,19 @@ public function fromDateTime($value)
6767
*/
6868
protected function asDateTime($value)
6969
{
70-
// Convert timestamp
71-
if (is_numeric($value))
72-
{
73-
return Carbon::createFromTimestamp($value);
74-
}
75-
76-
// Convert string
77-
if (is_string($value))
70+
// Convert MongoDate to timestamp
71+
if ($value instanceof MongoDate)
7872
{
79-
return new Carbon($value);
73+
$value = $value->sec;
8074
}
8175

82-
// Convert MongoDate
83-
if ($value instanceof MongoDate)
76+
// Convert timestamp to string for DateTime
77+
if (is_int($value))
8478
{
85-
return Carbon::createFromTimestamp($value->sec);
79+
$value = "@$value";
8680
}
8781

88-
return Carbon::instance($value);
82+
return new DateTime($value);
8983
}
9084

9185
/**
@@ -121,83 +115,104 @@ public function getTable()
121115
}
122116

123117
/**
124-
* Define a one-to-one relationship.
125-
*
126-
* @param string $related
127-
* @param string $foreignKey
128-
* @param string $localKey
129-
* @return \Illuminate\Database\Eloquent\Relations\HasOne
130-
*/
131-
public function hasOne($related, $foreignKey = null, $localKey = null)
118+
* Define a one-to-one relationship.
119+
*
120+
* @param string $related
121+
* @param string $foreignKey
122+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
123+
*/
124+
public function hasOne($related, $foreignKey = null)
132125
{
133126
$foreignKey = $foreignKey ?: $this->getForeignKey();
134127

135128
$instance = new $related;
136129

137-
$localKey = $localKey ?: $this->getKeyName();
138-
139-
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
130+
return new HasOne($instance->newQuery(), $this, $foreignKey);
140131
}
141132

142133
/**
143-
* Define a one-to-many relationship.
144-
*
145-
* @param string $related
146-
* @param string $foreignKey
147-
* @param string $localKey
148-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
149-
*/
150-
public function hasMany($related, $foreignKey = null, $localKey = null)
134+
* Define a one-to-many relationship.
135+
*
136+
* @param string $related
137+
* @param string $foreignKey
138+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
139+
*/
140+
public function hasMany($related, $foreignKey = null)
151141
{
152142
$foreignKey = $foreignKey ?: $this->getForeignKey();
153143

154144
$instance = new $related;
155145

156-
$localKey = $localKey ?: $this->getKeyName();
157-
158-
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
146+
return new HasMany($instance->newQuery(), $this, $foreignKey);
159147
}
160148

161149
/**
162-
* Define an inverse one-to-one or many relationship.
163-
*
164-
* @param string $related
165-
* @param string $foreignKey
166-
* @param string $otherKey
167-
* @param string $relation
168-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
169-
*/
170-
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
150+
* Define an inverse one-to-one or many relationship.
151+
*
152+
* @param string $related
153+
* @param string $foreignKey
154+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
155+
*/
156+
public function belongsTo($related, $foreignKey = null)
171157
{
172-
// If no relation name was given, we will use this debug backtrace to extract
173-
// the calling method's name and use that as the relationship name as most
174-
// of the time this will be what we desire to use for the relatinoships.
175-
if (is_null($relation))
176-
{
177-
list(, $caller) = debug_backtrace(false);
178-
179-
$relation = $caller['function'];
180-
}
158+
list(, $caller) = debug_backtrace(false);
181159

182160
// If no foreign key was supplied, we can use a backtrace to guess the proper
183161
// foreign key name by using the name of the relationship function, which
184162
// when combined with an "_id" should conventionally match the columns.
163+
$relation = $caller['function'];
164+
185165
if (is_null($foreignKey))
186166
{
187167
$foreignKey = snake_case($relation).'_id';
188168
}
189169

190-
$instance = new $related;
191-
192170
// Once we have the foreign key names, we'll just create a new Eloquent query
193171
// for the related models and returns the relationship instance which will
194172
// actually be responsible for retrieving and hydrating every relations.
195-
$query = $instance->newQuery();
173+
$instance = new $related;
196174

197-
$otherKey = $otherKey ?: $instance->getKeyName();
175+
$query = $instance->newQuery();
198176

199-
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
177+
return new BelongsTo($query, $this, $foreignKey, $relation);
200178
}
179+
180+
/**
181+
* Define a many-to-many relationship.
182+
*
183+
* @param string $related
184+
* @param string $table
185+
* @param string $foreignKey
186+
* @param string $otherKey
187+
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
188+
*/
189+
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null)
190+
{
191+
$caller = $this->getBelongsToManyCaller();
192+
193+
// First, we'll need to determine the foreign key and "other key" for the
194+
// relationship. Once we have determined the keys we'll make the query
195+
// instances as well as the relationship instances we need for this.
196+
$foreignKey = $foreignKey ?: $this->getForeignKey() . 's';
197+
198+
$instance = new $related;
199+
200+
$otherKey = $otherKey ?: $instance->getForeignKey() . 's';
201+
// If no table name was provided, we can guess it by concatenating the two
202+
// models using underscores in alphabetical order. The two model names
203+
// are transformed to snake case from their default CamelCase also.
204+
if (is_null($collection))
205+
{
206+
$collection = snake_case(str_plural(class_basename($related)));
207+
}
208+
209+
// Now we're ready to create a new query builder for the related model and
210+
// the relationship instances for the relation. The relations will set
211+
// appropriate query constraint and entirely manages the hydrations.
212+
$query = $instance->newQuery();
213+
214+
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $caller['function']);
215+
}
201216

202217
/**
203218
* Get a new query builder instance for the connection.
@@ -251,7 +266,7 @@ public function dropColumn($columns)
251266
{
252267
$this->__unset($column);
253268
}
254-
269+
255270
// Perform unset only on current document
256271
return $query = $this->newQuery()->where($this->getKeyName(), $this->getKey())->unset($columns);
257272
}
@@ -265,7 +280,6 @@ public function dropColumn($columns)
265280
*/
266281
public function __call($method, $parameters)
267282
{
268-
// Unset method
269283
if ($method == 'unset')
270284
{
271285
return call_user_func_array(array($this, 'dropColumn'), $parameters);
@@ -274,4 +288,4 @@ public function __call($method, $parameters)
274288
return parent::__call($method, $parameters);
275289
}
276290

277-
}
291+
}

0 commit comments

Comments
 (0)