public static function singleton() { if (!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c(); } return self::$instance; }
/** * Create table * * @param string $name * @param array $data */ function createTable($name, $data) { $PDODb = PDODb::getInstance(); $PDODb->rawQuery("DROP TABLE IF EXISTS {$name}"); $query = "CREATE TABLE {$name} (id INT(9) UNSIGNED PRIMARY KEY AUTO_INCREMENT"; foreach ($data as $key => $value) { $query .= ", {$key} {$value}"; } $query .= ")"; $PDODb->rawQuery($query); }
/** * @param string|array|object $type * @param string $host * @param string $username * @param string $password * @param string $dbname * @param int $port * @param string $charset */ public function __construct($type, $host = null, $username = null, $password = null, $dbname = null, $port = null, $charset = null) { if (is_array($type)) { // if params were passed as array $this->connectionParams = $type; } elseif (is_object($type)) { // if type is set as pdo object $this->pdo = $type; } else { foreach ($this->connectionParams as $key => $param) { if (isset(${$key}) && !is_null(${$key})) { $this->connectionParams[$key] = ${$key}; } } } if (isset($this->connectionParams['prefix'])) { $this->setPrefix($this->connectionParams['prefix']); } if (isset($this->connectionParams['isSubQuery'])) { $this->isSubQuery = true; return; } self::$instance = $this; }
public function testPagination() { $PDODb = PDODb::getInstance(); $PDODb->orderBy('id', 'ASC')->setPageLimit(2); $result = $PDODb->paginate('products', 1); $this->assertEquals(3, $PDODb->totalPages); $this->assertEquals(2, count($result)); }