*/ Config::set('app.command.urlPrefix', 'do'); Config::set('app.command.parameter', 'do'); Config::set('app.command.default', ''); Config::set('app.dir.controllers', dirname(dirname(__FILE__)) . '/controllers'); Config::set('app.dir.ignored', array('.', '..', '.svn', 'cvs')); Config::set('app.docRoot', dirname(dirname(__FILE__))); Config::set('app.domain', 'localhost'); Config::set('app.password', 'password'); Config::set('app.dir.databases', dirname(dirname(__FILE__)) . '/db'); AutoLoader::addClassPath(dirname(dirname(__FILE__)) . '/models'); AutoLoader::addClassPath(dirname(dirname(__FILE__)) . '/models/managers'); /* # Database */ Database::addConnection(array('name' => 'default', 'driver' => 'sqlite', 'dsn' => 'sqlite:' . Config::get('app.dir.databases') . '/model.s3db')); /* Alternative for testing whilst building test on dev machine Database::addConnection(array( 'name'=>'default', 'driver'=>'mysql', 'dsn'=>'mysql:host=localhost;dbname=buan_test', 'username'=>'root', 'password'=>'' ));*/ /* # Model relationships */ ModelRelation::define('Person(1):PersonAddress(M):Address(1)'); ModelRelation::define('Person(1):Account(1)'); ModelRelation::define('Person(1):PersonAlias(M,2)'); ModelRelation::define('Person(1):PersonBook(M):Book(1)');
<?php /** * @package UnitTest */ use Buan\Autoloader; use Buan\Config; use Buan\Database; use Buan\Model; use Buan\ModelManager; // Class paths AutoLoader::addClassPath(dirname(dirname(__FILE__)) . '/lib'); // Remove any previously created databases $dbInfo = Database::getConnectionInfo('default'); $dbFile = preg_replace("/^sqlite:(.*?)\$/i", "\$1", $dbInfo['dsn']); if (file_exists($dbFile)) { unlink($dbFile); } // Create test database $schema = new SqlDumpIterator(file_get_contents(dirname(__FILE__) . '/db-schema.sql')); foreach ($schema as $query) { try { $stmt = ModelManager::sqlQuery($query); } catch (Exception $e) { die($e->getMessage()); } } // Start building some objects for testing persistent stuff echo "Populating database ... "; ob_flush(); ModelManager::sqlQuery('BEGIN TRANSACTION');
/** * This method allows you to execute any arbitrary SQL statement and the * results are returned as a PDOStatement, or FALSE if the query failed. * * If you want to use numeric parameters (ie. SELECT * FROM x WHERE y=?) * then pass $params as a normal 0-indexed array. * However, if you want to use named parameters * (ie. SELECT * FROM x WHERE y=:myparam), then send $params as a hash * key=>value pairs of ":param"=>"value". * * Really, you could just as easily use the PDO functions directly in your * code. This will give you more flexibilty with setting attributes, etc. * Just try to keep all database code within your Model or ModelManager * classes. * * @param string|\Buan\ModelCriteria The query to execute * @param array Parameters to bind to the query * @param string The DB connection through which the query will be executed * @return \PDOStatement * @throws \PDOException */ public static function sqlQuery($sql, $params = [], $connection = null) { // Get the database connection if (is_null($connection)) { try { $connection = Database::getConnection('default'); } catch (Exception $e) { SystemLog::add($e->getMessage(), SystemLog::WARNING); return false; } } // Execute the query try { if ($sql instanceof ModelCriteria) { $sql = $sql->sql(); $stmt = $connection->prepare($sql->query); foreach ($sql->bindings as $binding) { $stmt->bindValue($binding->parameter, $binding->value, $binding->dataType); } $stmt->execute(); } else { if (count($params) > 0) { $stmt = $connection->prepare($sql); $stmt->execute($params); } else { $stmt = $connection->query($sql); } } return $stmt; } catch (PDOException $e) { $dbg = debug_backtrace(); $msg = $e->getMessage() . " (source: {$dbg[0]['file']} line {$dbg[0]['line']})"; throw new PDOException($msg); return false; } }
use Buan\Model; use Buan\ModelCollection; use Buan\ModelCriteria; use Buan\ModelManager; $mmLib = ModelManager::create('Library'); // Models, arrays $test = $this->startTest('Create collection from simple Models, arrays'); $lib1 = Model::create('Library'); $c1 = new ModelCollection($lib1); $test->addResult($c1->contains($lib1)); $lib2 = Model::create('Library'); $c2 = new ModelCollection(array($lib1, $lib2)); $test->addResult($c2->contains($lib1)); $test->addResult($c2->contains($lib2)); $test->end(); // Models, arrays $test = $this->startTest('Create collection from PDO result'); $DB = Database::getConnectionByModel(); $c = new ModelCriteria(); $c->selectField("`book`.*"); $c->selectTable('book'); $sql = $c->sql(); $stmt = $DB->prepare($sql->query); $stmt->execute(); $c1 = new ModelCollection('Book', $stmt); $test->addResult($c1[6]->modelName == 'Book'); $test->addResult(!$c1->isEmpty()); $test->end(); // Merge collections $test = $this->startTest('Merge collections'); $test->end();