<?php error_reporting(E_ALL | E_STRICT); require_once "../MysqliDb.php"; require_once "../dbObject.php"; $db = new Mysqlidb('localhost', 'root', '', 'testdb'); dbObject::autoload("models"); $tables = array('users' => array('login' => 'char(10) not null', 'active' => 'bool default 0', 'customerId' => 'int(10) not null', 'firstName' => 'char(10) not null', 'lastName' => 'char(10)', 'password' => 'text not null', 'createdAt' => 'datetime', 'updatedAt' => 'datetime', 'expires' => 'datetime', 'loginCount' => 'int(10) default 0'), 'products' => array('customerId' => 'int(10) not null', 'userId' => 'int(10) not null', 'productName' => 'char(50)')); $data = array('user' => array(array('login' => 'user1', 'customerId' => 10, 'firstName' => 'John', 'lastName' => 'Doe', 'password' => $db->func('SHA1(?)', array("secretpassword+salt")), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc()), array('login' => 'user2', 'customerId' => 10, 'firstName' => 'Mike', 'lastName' => NULL, 'password' => $db->func('SHA1(?)', array("secretpassword2+salt")), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc(2)), array('login' => 'user3', 'active' => true, 'customerId' => 11, 'firstName' => 'Pete', 'lastName' => 'D', 'password' => $db->func('SHA1(?)', array("secretpassword2+salt")), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc(3))), 'product' => array(array('customerId' => 1, 'userId' => 1, 'productName' => 'product1'), array('customerId' => 1, 'userId' => 1, 'productName' => 'product2'), array('customerId' => 1, 'userId' => 1, 'productName' => 'product3'), array('customerId' => 1, 'userId' => 2, 'productName' => 'product4'), array('customerId' => 1, 'userId' => 2, 'productName' => 'product5'))); function createTable($name, $data) { global $db; //$q = "CREATE TABLE $name (id INT(9) UNSIGNED PRIMARY KEY NOT NULL"; $q = "CREATE TABLE {$name} (id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT"; foreach ($data as $k => $v) { $q .= ", {$k} {$v}"; } $q .= ")"; $db->rawQuery($q); } // rawQuery test foreach ($tables as $name => $fields) { $db->rawQuery("DROP TABLE " . $name); createTable($name, $fields); } foreach ($data as $name => $datas) { foreach ($data[$name] as $userData) { $obj = new $name($userData); $id = $obj->save(); if ($obj->errors) { echo "errors:";
'products' => Array ( 'customerId' => 'int(10) not null', 'userId' => 'int(10) not null', 'productName' => 'char(50)' ) ); $data = Array ( 'user' => Array ( Array ('login' => 'user1', 'customerId' => 10, 'firstName' => 'John', 'lastName' => 'Doe', 'password' => $db->func('SHA1(?)',Array ("secretpassword+salt")), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc() ), Array ('login' => 'user2', 'customerId' => 10, 'firstName' => 'Mike', 'lastName' => NULL, 'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")), 'expires' => $db->now('+1Y'), 'loginCount' => $db->inc(2) ), Array ('login' => 'user3', 'active' => true, 'customerId' => 11, 'firstName' => 'Pete', 'lastName' => 'D', 'password' => $db->func('SHA1(?)',Array ("secretpassword2+salt")),