Commit 25bf2fe6 authored by Alexander Butenko's avatar Alexander Butenko

Merge pull request #273 from avbdr/master

Support for separate key definition in hasOne relation
parents ba43d3bc 9cbabeb6
......@@ -139,17 +139,16 @@ class dbObject {
$modelName = $this->relations[$name][1];
switch ($relationType) {
case 'hasone':
$key = isset ($this->relations[$name][2]) ? $this->relations[$name][2] : $name;
$obj = new $modelName;
$obj->returnType = $this->returnType;
$this->data[$name] = $obj->byId($this->data[$name]);
return $this->data[$name];
return $this->data[$name] = $obj->byId($this->data[$key]);
break;
case 'hasmany':
$key = $this->relations[$name][2];
$obj = new $modelName;
$obj->returnType = $this->returnType;
$this->data[$name] = $obj->where($key, $this->data[$this->primaryKey])->get();
return $this->data[$name];
return $this->data[$name] = $obj->where($key, $this->data[$this->primaryKey])->get();
break;
default:
break;
......
......@@ -129,6 +129,11 @@ if (!is_object ($product->data['userId'])) {
exit;
}
$product = product::with('user')->byId(5);
if (!is_object ($product->data['user'])) {
echo "Error in with processing in getOne object";
exit;
}
$products = product::ArrayBuilder()->with('userId')->get(2);
if (!is_array ($products[0]['userId']) || !is_array ($products[1]['userId'])) {
......
......@@ -16,7 +16,8 @@ class product extends dbObject {
'productName' => Array ('text','required')
);
protected $relations = Array (
'userId' => Array ("hasOne", "user")
'userId' => Array ("hasOne", "user"),
'user' => Array ("hasOne", "user", "userId")
);
public function last () {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment