Ejemplo n.º 1
0
<?php

/*
* Test relationship functionality
*
* @package UnitTest
*/
use Buan\Model;
use Buan\ModelCriteria;
use Buan\ModelCollection;
use Buan\ModelManager;
use Buan\ModelRelation;
$mmBook = ModelManager::create('Book');
$mmLibrary = ModelManager::create('Library');
$mmPerson = ModelManager::create('Person');
/* ------------------------------------------------------- Loading and saving */
$test = $this->startTest('Basic loading and saving');
// Load some persistent models
$l1 = Model::create('Library');
$l1->id = 1;
$test->addResult($mmLibrary->load($l1) === TRUE);
$l2 = Model::create('Library');
$l2->id = 1;
$test->addResult($mmLibrary->load($l2) === TRUE);
// Ensure instance uniqueness
$test->addResult($l1 !== $l2);
// Check flags
$test->addResult($l1->isInDatabase());
$test->addResult(!$l1->hasChanged());
// Save (no changes)
$test->addResult($mmLibrary->save($l1) === TRUE);
Ejemplo n.º 2
0
// Start building some objects for testing persistent stuff
echo "Populating database ... ";
ob_flush();
ModelManager::sqlQuery('BEGIN TRANSACTION');
//ModelManager::sqlQuery('START TRANSACTION');	/* Version for MySql */
// Populate: Library with books
for ($i = 0; $i < 2; $i++) {
    $sql = 'INSERT INTO library (id, name) VALUES (' . ($i + 1) . ', "LIB(' . uniqid(rand()) . ')")';
    ModelManager::sqlQuery($sql);
}
for ($i = 0; $i < 500; $i++) {
    $sql = 'INSERT INTO book (id, library_id, title) VALUES (' . ($i + 1) . ', 1, "BOOK(' . uniqid(rand()) . ')")';
    ModelManager::sqlQuery($sql);
}
for ($j = $i; $j < $i + 100; $j++) {
    $sql = 'INSERT INTO book (id, library_id, title) VALUES (' . ($j + 1) . ', 2, "BOOK(' . uniqid(rand()) . ')")';
    ModelManager::sqlQuery($sql);
}
// Populate: People and accounts
// Populate: Library with books
for ($i = 0; $i < 5; $i++) {
    $sql = 'INSERT INTO person (id, name, age) VALUES (' . ($i + 1) . ', "NAME(' . uniqid(rand()) . ')", ' . rand(25, 50) . ')';
    ModelManager::sqlQuery($sql);
}
// Populate: People have books
$sql = 'INSERT INTO person_book (id, person_id, book_id) VALUES (1, 1, 1)';
ModelManager::sqlQuery($sql);
// Commit changes
ModelManager::sqlQuery('COMMIT');
echo "done\n\n";
ob_flush();
Ejemplo n.º 3
0
 /**
  * Returns an array of Models of the specified type, according to any given criteria.
  *
  * @param string Name of the Model type to be selected
  * @param \Buan\ModelCriteria Filter resluts on this criteria
  * @return \Buan\ModelCollection
  * @throws ModelException
  */
 public static function select($modelName, $criteria = null)
 {
     // Create an instance of the Model type and it's Manager class
     $model = Model::create($modelName);
     $manager = ModelManager::create($modelName);
     // Process
     $records = [];
     try {
         // Get the DB connection used by Models of this type
         $DB = Database::getConnectionByModel($model);
         // Build, prepare and execute query
         if ($criteria === null) {
             $c = new ModelCriteria();
         } else {
             $c = clone $criteria;
         }
         $c->selectField("`{$model->getDbTableName()}`.*");
         $c->selectTable($model->getDbTableName());
         $sql = $c->sql();
         $stmt = $DB->prepare($sql->query);
         foreach ($sql->bindings as $binding) {
             $stmt->bindValue($binding->parameter, $binding->value, $binding->dataType);
         }
         $stmt->execute();
         // Create a new collection stream from the result set and return
         return new ModelCollection($modelName, $stmt);
     } catch (PDOException $e) {
         throw new ModelException("PDO Exception: {$e->getMessage()}");
     } catch (Exception $e) {
         throw new ModelException($e->getMessage());
     }
 }
Ejemplo n.º 4
0
<?php

/*
* Test collection object.
*
* @package UnitTest
*/
use Buan\Database;
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);