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 { ...@@ -139,17 +139,16 @@ class dbObject {
$modelName = $this->relations[$name][1]; $modelName = $this->relations[$name][1];
switch ($relationType) { switch ($relationType) {
case 'hasone': case 'hasone':
$key = isset ($this->relations[$name][2]) ? $this->relations[$name][2] : $name;
$obj = new $modelName; $obj = new $modelName;
$obj->returnType = $this->returnType; $obj->returnType = $this->returnType;
$this->data[$name] = $obj->byId($this->data[$name]); return $this->data[$name] = $obj->byId($this->data[$key]);
return $this->data[$name];
break; break;
case 'hasmany': case 'hasmany':
$key = $this->relations[$name][2]; $key = $this->relations[$name][2];
$obj = new $modelName; $obj = new $modelName;
$obj->returnType = $this->returnType; $obj->returnType = $this->returnType;
$this->data[$name] = $obj->where($key, $this->data[$this->primaryKey])->get(); return $this->data[$name] = $obj->where($key, $this->data[$this->primaryKey])->get();
return $this->data[$name];
break; break;
default: default:
break; break;
......
...@@ -129,6 +129,11 @@ if (!is_object ($product->data['userId'])) { ...@@ -129,6 +129,11 @@ if (!is_object ($product->data['userId'])) {
exit; 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); $products = product::ArrayBuilder()->with('userId')->get(2);
if (!is_array ($products[0]['userId']) || !is_array ($products[1]['userId'])) { if (!is_array ($products[0]['userId']) || !is_array ($products[1]['userId'])) {
......
...@@ -16,7 +16,8 @@ class product extends dbObject { ...@@ -16,7 +16,8 @@ class product extends dbObject {
'productName' => Array ('text','required') 'productName' => Array ('text','required')
); );
protected $relations = Array ( protected $relations = Array (
'userId' => Array ("hasOne", "user") 'userId' => Array ("hasOne", "user"),
'user' => Array ("hasOne", "user", "userId")
); );
public function last () { 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