Skip to content

Commit 9cbabeb

Browse files
committed
Support for separate key definition in hasOne relation
1 parent 57cf446 commit 9cbabeb

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

Diff for: dbObject.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,16 @@ public function __get ($name) {
139139
$modelName = $this->relations[$name][1];
140140
switch ($relationType) {
141141
case 'hasone':
142+
$key = isset ($this->relations[$name][2]) ? $this->relations[$name][2] : $name;
142143
$obj = new $modelName;
143144
$obj->returnType = $this->returnType;
144-
$this->data[$name] = $obj->byId($this->data[$name]);
145-
return $this->data[$name];
145+
return $this->data[$name] = $obj->byId($this->data[$key]);
146146
break;
147147
case 'hasmany':
148148
$key = $this->relations[$name][2];
149149
$obj = new $modelName;
150150
$obj->returnType = $this->returnType;
151-
$this->data[$name] = $obj->where($key, $this->data[$this->primaryKey])->get();
152-
return $this->data[$name];
151+
return $this->data[$name] = $obj->where($key, $this->data[$this->primaryKey])->get();
153152
break;
154153
default:
155154
break;

Diff for: tests/dbObjectTests.php

+5
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,11 @@ function createTable ($name, $data) {
129129
exit;
130130
}
131131

132+
$product = product::with('user')->byId(5);
133+
if (!is_object ($product->data['user'])) {
134+
echo "Error in with processing in getOne object";
135+
exit;
136+
}
132137

133138
$products = product::ArrayBuilder()->with('userId')->get(2);
134139
if (!is_array ($products[0]['userId']) || !is_array ($products[1]['userId'])) {

Diff for: tests/models/product.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ class product extends dbObject {
1616
'productName' => Array ('text','required')
1717
);
1818
protected $relations = Array (
19-
'userId' => Array ("hasOne", "user")
19+
'userId' => Array ("hasOne", "user"),
20+
'user' => Array ("hasOne", "user", "userId")
2021
);
2122

2223
public function last () {

0 commit comments

Comments
 (0)