Exemplo n.º 1
0
 /**
  * Returns a count of all records of the specified type that match the given
  * criteria (if specified)
  *
  * @todo When PDOStatement->rowCount() is supported by all database drivers,
  * use it instead of the fetchAll() solution.
  *
  * @param string Name of the Model type to be selected
  * @param \Buan\ModelCriteria Filter by this criteria
  * @return int
  */
 public static function selectCount($modelName, $criteria = null)
 {
     // Create an instance of the Model type and it's Manager class
     $model = Model::create($modelName);
     // Get the DB connection used by Models of this type
     try {
         $DB = Database::getConnectionByModel($model);
     } catch (Exception $e) {
         SystemLog::add($e->getMessage(), SystemLog::WARNING);
         return 0;
     }
     // Build query criteria
     if ($criteria === null) {
         $c = new ModelCriteria();
     } else {
         $c = clone $criteria;
         //$c->ungroupBy();
         //if($c->hasSelectFields()) {
         //	$c->groupBy("NULL");
         //}
     }
     $c->selectTable($model->getDbTableName());
     // If no fields have been specified in the SELECT portion of the query,
     // then we'll use COUNT(*)
     //
     // The method of counting the rows is one of:
     //	FETCH_ALL	= The query is run as-is and rows are counted from the
     //				result of $stmt->fetchAll()
     //	COUNT_SQL	= The COUNT(*) method is used (for simple queries that
     //				do not already contain and FIELDS in the SELECT portion)
     $countMethod = 'FETCH_ALL';
     if (!$c->hasSelectFields()) {
         $c->selectField("COUNT(*) AS numRecords");
         $countMethod = 'COUNT_SQL';
     }
     // Prepare and execute query
     $sql = $c->sql();
     if (!($stmt = $DB->prepare($sql->query))) {
         // Log and return
         SystemLog::add('Failed to prepare PDO statement: ' . $sql, SystemLog::WARNING);
         return 0;
     } else {
         foreach ($sql->bindings as $binding) {
             $stmt->bindValue($binding->parameter, $binding->value, $binding->dataType);
         }
         if (!$stmt->execute()) {
             // Log and return
             SystemLog::add('Query failed: ' . $stmt->queryString, SystemLog::WARNING);
             return 0;
         }
     }
     // Get the count
     $rec = $stmt->fetchAll(PDO::FETCH_ASSOC);
     if (empty($rec) && $countMethod == 'COUNT_SQL') {
         SystemLog::add('ModelManager::selectCount() has not managed to retrieve any count.', SystemLog::WARNING);
         return 0;
     }
     return $countMethod == 'FETCH_ALL' ? count($rec) : (int) $rec[0]['numRecords'];
 }
Exemplo n.º 2
0
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();