Skip to content

Commit 0eed896

Browse files
committed
Models fixed and tests updated
1 parent bbb7990 commit 0eed896

File tree

4 files changed

+192
-145
lines changed

4 files changed

+192
-145
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 62 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Jenssegers\Mongodb\Relations\BelongsTo;
1010
use Jenssegers\Mongodb\Relations\BelongsToMany;
1111

12+
use Carbon\Carbon;
1213
use DateTime;
1314
use MongoId;
1415
use MongoDate;
@@ -67,19 +68,25 @@ public function fromDateTime($value)
6768
*/
6869
protected function asDateTime($value)
6970
{
70-
// Convert MongoDate to timestamp
71-
if ($value instanceof MongoDate)
71+
// Convert timestamp
72+
if (is_numeric($value))
73+
{
74+
return Carbon::createFromTimestamp($value);
75+
}
76+
77+
// Convert string
78+
if (is_string($value))
7279
{
73-
$value = $value->sec;
80+
return new Carbon($value);
7481
}
7582

76-
// Convert timestamp to string for DateTime
77-
if (is_int($value))
83+
// Convert MongoDate
84+
if ($value instanceof MongoDate)
7885
{
79-
$value = "@$value";
86+
return Carbon::createFromTimestamp($value->sec);
8087
}
8188

82-
return new DateTime($value);
89+
return Carbon::instance($value);
8390
}
8491

8592
/**
@@ -115,68 +122,84 @@ public function getTable()
115122
}
116123

117124
/**
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)
125+
* Define a one-to-one relationship.
126+
*
127+
* @param string $related
128+
* @param string $foreignKey
129+
* @param string $localKey
130+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
131+
*/
132+
public function hasOne($related, $foreignKey = null, $localKey = null)
125133
{
126134
$foreignKey = $foreignKey ?: $this->getForeignKey();
127135

128136
$instance = new $related;
129137

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

133143
/**
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)
144+
* Define a one-to-many relationship.
145+
*
146+
* @param string $related
147+
* @param string $foreignKey
148+
* @param string $localKey
149+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
150+
*/
151+
public function hasMany($related, $foreignKey = null, $localKey = null)
141152
{
142153
$foreignKey = $foreignKey ?: $this->getForeignKey();
143154

144155
$instance = new $related;
145156

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

149162
/**
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)
163+
* Define an inverse one-to-one or many relationship.
164+
*
165+
* @param string $related
166+
* @param string $foreignKey
167+
* @param string $otherKey
168+
* @param string $relation
169+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
170+
*/
171+
public function belongsTo($related, $foreignKey = null, $otherKey = null, $relation = null)
157172
{
158-
list(, $caller) = debug_backtrace(false);
173+
// If no relation name was given, we will use this debug backtrace to extract
174+
// the calling method's name and use that as the relationship name as most
175+
// of the time this will be what we desire to use for the relatinoships.
176+
if (is_null($relation))
177+
{
178+
list(, $caller) = debug_backtrace(false);
179+
180+
$relation = $caller['function'];
181+
}
159182

160183
// If no foreign key was supplied, we can use a backtrace to guess the proper
161184
// foreign key name by using the name of the relationship function, which
162185
// when combined with an "_id" should conventionally match the columns.
163-
$relation = $caller['function'];
164-
165186
if (is_null($foreignKey))
166187
{
167188
$foreignKey = snake_case($relation).'_id';
168189
}
169190

191+
$instance = new $related;
192+
170193
// Once we have the foreign key names, we'll just create a new Eloquent query
171194
// for the related models and returns the relationship instance which will
172195
// actually be responsible for retrieving and hydrating every relations.
173-
$instance = new $related;
174-
175196
$query = $instance->newQuery();
176197

177-
return new BelongsTo($query, $this, $foreignKey, $relation);
198+
$otherKey = $otherKey ?: $instance->getKeyName();
199+
200+
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
178201
}
179-
202+
180203
/**
181204
* Define a many-to-many relationship.
182205
*
@@ -266,7 +289,7 @@ public function dropColumn($columns)
266289
{
267290
$this->__unset($column);
268291
}
269-
292+
270293
// Perform unset only on current document
271294
return $query = $this->newQuery()->where($this->getKeyName(), $this->getKey())->unset($columns);
272295
}
@@ -280,6 +303,7 @@ public function dropColumn($columns)
280303
*/
281304
public function __call($method, $parameters)
282305
{
306+
// Unset method
283307
if ($method == 'unset')
284308
{
285309
return call_user_func_array(array($this, 'dropColumn'), $parameters);

tests/ModelTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,11 +312,14 @@ public function testUnset()
312312
public function testDates()
313313
{
314314
$user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
315+
315316
$this->assertInstanceOf('Carbon\Carbon', $user->birthday);
316317

317318
$check = User::find($user->_id);
319+
318320
$this->assertInstanceOf('Carbon\Carbon', $check->birthday);
319321
$this->assertEquals($user->birthday, $check->birthday);
322+
320323

321324
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
322325
$this->assertEquals('John Doe', $user->name);

0 commit comments

Comments
 (0)