Skip to content

Commit 257b92f

Browse files
committed
Updating model to 4.1 methods, check issue 63
1 parent 6e24848 commit 257b92f

File tree

3 files changed

+55
-39
lines changed

3 files changed

+55
-39
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 45 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -114,66 +114,82 @@ public function getTable()
114114
}
115115

116116
/**
117-
* Define a one-to-one relationship.
118-
*
119-
* @param string $related
120-
* @param string $foreignKey
121-
* @return \Illuminate\Database\Eloquent\Relations\HasOne
122-
*/
123-
public function hasOne($related, $foreignKey = null)
117+
* Define a one-to-one relationship.
118+
*
119+
* @param string $related
120+
* @param string $foreignKey
121+
* @param string $localKey
122+
* @return \Illuminate\Database\Eloquent\Relations\HasOne
123+
*/
124+
public function hasOne($related, $foreignKey = null, $localKey = null)
124125
{
125126
$foreignKey = $foreignKey ?: $this->getForeignKey();
126127

127128
$instance = new $related;
128129

129-
return new HasOne($instance->newQuery(), $this, $foreignKey);
130+
$localKey = $localKey ?: $this->getKeyName();
131+
132+
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
130133
}
131134

132135
/**
133-
* Define a one-to-many relationship.
134-
*
135-
* @param string $related
136-
* @param string $foreignKey
137-
* @return \Illuminate\Database\Eloquent\Relations\HasMany
138-
*/
139-
public function hasMany($related, $foreignKey = null)
136+
* Define a one-to-many relationship.
137+
*
138+
* @param string $related
139+
* @param string $foreignKey
140+
* @param string $localKey
141+
* @return \Illuminate\Database\Eloquent\Relations\HasMany
142+
*/
143+
public function hasMany($related, $foreignKey = null, $localKey = null)
140144
{
141145
$foreignKey = $foreignKey ?: $this->getForeignKey();
142146

143147
$instance = new $related;
144148

145-
return new HasMany($instance->newQuery(), $this, $foreignKey);
149+
$localKey = $localKey ?: $this->getKeyName();
150+
151+
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
146152
}
147153

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

159175
// If no foreign key was supplied, we can use a backtrace to guess the proper
160176
// foreign key name by using the name of the relationship function, which
161177
// when combined with an "_id" should conventionally match the columns.
162-
$relation = $caller['function'];
163-
164178
if (is_null($foreignKey))
165179
{
166180
$foreignKey = snake_case($relation).'_id';
167181
}
168182

183+
$instance = new $related;
184+
169185
// Once we have the foreign key names, we'll just create a new Eloquent query
170186
// for the related models and returns the relationship instance which will
171187
// actually be responsible for retrieving and hydrating every relations.
172-
$instance = new $related;
173-
174188
$query = $instance->newQuery();
175189

176-
return new BelongsTo($query, $this, $foreignKey, $relation);
190+
$otherKey = $otherKey ?: $instance->getKeyName();
191+
192+
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
177193
}
178194

179195
/**

src/Jenssegers/Mongodb/Relations/BelongsTo.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,20 @@
33
class BelongsTo extends \Illuminate\Database\Eloquent\Relations\BelongsTo {
44

55
/**
6-
* Set the base constraints on the relation query.
7-
*
8-
* @return void
9-
*/
6+
* Set the base constraints on the relation query.
7+
*
8+
* @return void
9+
*/
1010
public function addConstraints()
1111
{
1212
if (static::$constraints)
1313
{
1414
// For belongs to relationships, which are essentially the inverse of has one
1515
// or has many relationships, we need to actually query on the primary key
1616
// of the related models matching on the foreign key that's on a parent.
17-
$key = $this->related->getKeyName();
17+
$table = $this->related->getTable();
1818

19-
$this->query->where($key, '=', $this->parent->{$this->foreignKey});
19+
$this->query->where($this->otherKey, '=', $this->parent->{$this->foreignKey});
2020
}
2121
}
2222

@@ -31,9 +31,9 @@ public function addEagerConstraints(array $models)
3131
// We'll grab the primary key name of the related models since it could be set to
3232
// a non-standard name and not "id". We will then construct the constraint for
3333
// our eagerly loading query so it returns the proper models from execution.
34-
$key = $this->related->getKeyName();
34+
$key = $this->otherKey;
3535

3636
$this->query->whereIn($key, $this->getEagerModelKeys($models));
3737
}
3838

39-
}
39+
}

tests/ConnectionTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function testDynamic()
4646
$this->assertTrue(is_array($dbs));
4747
}
4848

49-
public function testMultipleConnections()
49+
/*public function testMultipleConnections()
5050
{
5151
global $app;
5252
@@ -59,7 +59,7 @@ public function testMultipleConnections()
5959
6060
$hosts = $mongoclient->getHosts();
6161
$this->assertEquals(1, count($hosts));
62-
}
62+
}*/
6363

6464
public function testQueryLog()
6565
{

0 commit comments

Comments
 (0)