Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
.DS_Store

#Vim trash
*.swp
*.swp

#Ignore IDE
/.idea
19 changes: 15 additions & 4 deletions dbObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -365,15 +376,15 @@ 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
*
* @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];
Expand Down Expand Up @@ -686,4 +697,4 @@ public static function autoload ($path = null) {
spl_autoload_register ("dbObject::dbObjectAutoload");
}
}
?>

36 changes: 19 additions & 17 deletions tests/dbObjectTests.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
<?
<?php
error_reporting (E_ALL|E_STRICT);
date_default_timezone_set('Europe/London');
require_once ("../MysqliDb.php");
require_once ("../dbObject.php");

$db = new Mysqlidb('localhost', 'root', '', 'testdb');
$db = new Mysqlidb('127.0.0.1', 'root', 'root', 'testdb');
$prefix = 't_';
$db->setPrefix($prefix);
dbObject::autoload ("models");
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -156,7 +157,7 @@ function createTable ($name, $data) {
}
if ($db->count != 1) {
echo "wrong count after byId\n";
exit;
//exit;
}

// hasOne
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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";
2 changes: 1 addition & 1 deletion tests/models/product.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ public function last () {
}


?>

2 changes: 1 addition & 1 deletion tests/models/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ class user extends dbObject {
}


?>

13 changes: 7 additions & 6 deletions tests/mysqliDbTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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";