Ejemplo n.º 1
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.º 2
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.º 3
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);