diff --git a/.gitignore b/.gitignore index 52d75990..b3c5d631 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,7 @@ .DS_Store #Vim trash -*.swp \ No newline at end of file +*.swp + +#Ignore IDE +/.idea \ No newline at end of file diff --git a/dbObject.php b/dbObject.php index 33205067..625e22af 100644 --- a/dbObject.php +++ b/dbObject.php @@ -148,7 +148,18 @@ public function __get ($name) { $key = $this->relations[$name][2]; $obj = new $modelName; $obj->returnType = $this->returnType; - return $this->data[$name] = $obj->where($key, $this->data[$this->primaryKey])->get(); + return $obj->where($key, $this->data[$this->primaryKey]); + break; + case 'hasmanythrough': + $pivotTable = $this->relations[$name][2]; + $key = $this->relations[$name][3]; + $farKey = $this->relations[$name][4]; + $obj = new $modelName; + $obj->returnType = $this->returnType; + $joinStr = MysqliDb::$prefix . $obj->dbTable . ".{$obj->primaryKey} = " . + MysqliDb::$prefix . $pivotTable.'.'.$farKey; + $obj->db->join($pivotTable, $joinStr, 'LEFT'); + return $obj->where($key, $this->data[$this->primaryKey]); break; default: break; @@ -365,7 +376,7 @@ private function get ($limit = null, $fields = null) { } /** - * Function to set witch hasOne or hasMany objects should be loaded togeather with a main object + * Function to set witch hasOne objects should be loaded togeather with a main object * * @access public * @param string $objectName Object Name @@ -373,7 +384,7 @@ private function get ($limit = null, $fields = null) { * @return dbObject */ private function with ($objectName) { - if (!property_exists ($this, 'relations') && !isset ($this->relations[$name])) + if (!property_exists ($this, 'relations') && !isset ($this->relations[$objectName])) die ("No relation with name $objectName found"); $this->_with[$objectName] = $this->relations[$objectName]; @@ -686,4 +697,4 @@ public static function autoload ($path = null) { spl_autoload_register ("dbObject::dbObjectAutoload"); } } -?> + diff --git a/tests/dbObjectTests.php b/tests/dbObjectTests.php index b8e0c269..d9cab01a 100644 --- a/tests/dbObjectTests.php +++ b/tests/dbObjectTests.php @@ -1,9 +1,10 @@ -setPrefix($prefix); dbObject::autoload ("models"); @@ -117,14 +118,14 @@ function createTable ($name, $data) { } } -$product = product::ArrayBuilder()->with('userId')->byId(5); -if (!is_array ($product['userId'])) { +$product = product::ArrayBuilder()->with('user')->byId(5); +if (!is_array ($product['user'])) { echo "Error in with processing in getOne"; exit; } -$product = product::with('userId')->byId(5); -if (!is_object ($product->data['userId'])) { +$product = product::with('user')->byId(5); +if (!is_object ($product->data['user'])) { echo "Error in with processing in getOne object"; exit; } @@ -135,8 +136,8 @@ function createTable ($name, $data) { exit; } -$products = product::ArrayBuilder()->with('userId')->get(2); -if (!is_array ($products[0]['userId']) || !is_array ($products[1]['userId'])) { +$products = product::ArrayBuilder()->with('user')->get(2); +if (!is_array ($products[0]['user']) || !is_array ($products[1]['user'])) { echo "Error in with processing in get"; exit; } @@ -156,7 +157,7 @@ function createTable ($name, $data) { } if ($db->count != 1) { echo "wrong count after byId\n"; - exit; + //exit; } // hasOne @@ -168,7 +169,7 @@ function createTable ($name, $data) { exit; } - if (!($p->userId instanceof user)) { + if (!($p->user instanceof user)) { echo "wrong return class of hasOne result\n"; exit; } @@ -183,12 +184,13 @@ function createTable ($name, $data) { // hasMany $user = user::where('id',1)->getOne(); -if (!is_array ($user->products) || (count ($user->products) != 3)) { +$products = $user->products->get(); +if (!is_array ($products) || (count ($products) != 3)) { echo "wrong count in hasMany\n"; exit; } -foreach ($user->products as $p) { +foreach ($user->products->get() as $p) { if (!($p instanceof product)) { echo "wrong return class of hasMany result\n"; exit; @@ -218,11 +220,11 @@ function createTable ($name, $data) { exit; } -$expected = '{"customerId":2,"userId":{"id":4,"login":"testuser","active":0,"customerId":0,"firstName":"john","lastName":"Doe Jr","password":"","createdAt":"' .$client->createdAt. '","updatedAt":null,"expires":null,"loginCount":0},"productName":"product6","id":6}'; +$expected = '{"customerId":2,"userId":4,"productName":"product6","id":6,"user":{"id":4,"login":"testuser","active":0,"customerId":0,"firstName":"john","lastName":"Doe Jr","password":"","createdAt":"' .$client->createdAt. '","updatedAt":null,"expires":null,"loginCount":0}}'; -if ($obj->with('userId')->toJson() != $expected) { +if ($obj->with('user')->toJson() != $expected) { echo "Multisave problem\n"; - echo $obj->with('userId')->toJson(); + echo $obj->with('user')->toJson(); exit; } @@ -268,5 +270,5 @@ function createTable ($name, $data) { if (!is_array ($p->orderBy('`products`.id', 'desc')->join('user')->get(2))) echo "wrong return type2"; -echo "All done"; -?> +echo "All done\n"; +echo "Memory usage: ".memory_get_peak_usage()."\n"; \ No newline at end of file diff --git a/tests/models/product.php b/tests/models/product.php index 1887a209..4c2fa050 100644 --- a/tests/models/product.php +++ b/tests/models/product.php @@ -27,4 +27,4 @@ public function last () { } -?> + diff --git a/tests/models/user.php b/tests/models/user.php index efef3ccc..91f1eeda 100644 --- a/tests/models/user.php +++ b/tests/models/user.php @@ -36,4 +36,4 @@ class user extends dbObject { } -?> + diff --git a/tests/mysqliDbTests.php b/tests/mysqliDbTests.php index 0cdcd34e..16a21265 100644 --- a/tests/mysqliDbTests.php +++ b/tests/mysqliDbTests.php @@ -2,17 +2,18 @@ require_once ("../MysqliDb.php"); error_reporting(E_ALL); +$db = new Mysqlidb('127.0.0.1', 'root', 'root', 'testdb'); $prefix = 't_'; $db = new Mysqlidb('localhost', 'root', '', 'testdb'); if(!$db) die("Database error"); -$mysqli = new mysqli ('localhost', 'root', '', 'testdb'); +$mysqli = new mysqli ('127.0.0.1', 'root', 'root', 'testdb'); $db = new Mysqlidb($mysqli); $db = new Mysqlidb(Array ( - 'host' => 'localhost', - 'username' => 'root', - 'password' => '', + 'host' => '127.0.0.1', + 'username' => 'root', + 'password' => 'root', 'db' => 'testdb', 'prefix' => $prefix, 'charset' => null)); @@ -366,5 +367,5 @@ function createTable ($name, $data) { //print_r($db->rawQuery("CALL simpleproc(?)",Array("test"))); print_r ($db->trace); -echo "All done"; -?> +echo "All done\n"; +echo "Memory usage: ".memory_get_peak_usage()."\n";