$m->isInDatabase(TRUE); $test->addResult($m->isInDatabase() === TRUE); $test->end(); // Get/set primary keys $test = $this->startTest('Get/set simple primary keys'); $m = Model::create('Person'); $test->addResult($m->getPrimaryKey() === 'id'); $test->addResult($m->getPrimaryKeyValue() === NULL); $m->id = 70; $test->addResult($m->id === 70); $test->addResult($m->getPrimaryKeyValue() === 70); $m->setPrimaryKeyValue(100); $test->addResult($m->id === 100); $test->addResult($m->getPrimaryKeyValue() === 100); $m->setPrimaryKeyValue(NULL); $test->addResult($m->id === NULL); $test->addResult($m->getPrimaryKeyValue() === NULL); $test->end(); // Get/set composite primary keys $test = $this->startTest('Get/set composite primary keys'); $m = Model::create('Address'); $test->addResult($m->getPrimaryKey() === 'postcode,houseno'); $test->addResult(is_array($m->getPrimaryKeyValue()) && in_array('postcode', array_keys($m->getPrimaryKeyValue())) && in_array('houseno', array_keys($m->getPrimaryKeyValue()))); $m->postcode = 'NE26'; $m->houseno = 33; $test->addResult(is_array($m->getPrimaryKeyValue()) && $m->postcode === 'NE26' && $m->houseno === 33); $m->setPrimaryKeyValue(array('postcode' => 'NE26', 'houseno' => 31)); $test->addResult($m->postcode === 'NE26' && $m->houseno === 31); $m->setPrimaryKeyValue('postcode', 'NE20'); $test->addResult($m->postcode === 'NE20' && $m->houseno === 31); $test->end();
$c1->addRelatives($c1sub); $test->addResult($c1->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c1sub)); $test->addResult($c1sub->findRelatives('Category', ModelRelation::REF_CHILD)->contains($c1)); $c2sub = Model::create('Category'); $c2sub->title = "Child 2 Sub"; $c2->addRelatives($c2sub); $test->addResult($c2->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c2sub)); $test->addResult($c2sub->findRelatives('Category', ModelRelation::REF_CHILD)->contains($c2)); $c1->disownRelatives($c1sub); $test->addResult(!$c1->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c1sub)); $test->addResult($c1sub->findRelatives('Category', ModelRelation::REF_CHILD)->isEmpty()); $parent->disownRelatives($c2); $test->addResult(!$parent->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c2)); $test->addResult($c2->findRelatives('Category', ModelRelation::REF_CHILD)->isEmpty()); $test->addResult($c2->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c2sub)); $test->end(); /* ------------------------------------------------------------ M:M recursive */ $test = $this->startTest('M:M recursive'); $bob = Model::create('Person'); $bob->name = 'Bob'; $joe = Model::create('Person'); $joe->name = 'Joe'; $bob->addRelatives($joe); $friend = $bob->findLinkingRelative($joe); $test->addResult($bob->findRelatives('Person', ModelRelation::REF_PARENT)->contains($joe)); $test->addResult($joe->findRelatives('Person', ModelRelation::REF_CHILD)->contains($bob)); $test->addResult($friend->modelName == 'Friend'); $joe->disownRelatives($bob); $test->addResult(count($bob->findRelatives('Person', ModelRelation::REF_PARENT)) === 0); $test->addResult(count($joe->findRelatives('Person', ModelRelation::REF_CHILD)) === 0); $test->end();
/** * 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']; }
$c1 = Model::create('Category'); $c1->title = 'Child 1'; $parent->addRelatives($c1); $test->addResult($parent->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c1)); $test->addResult($c1->findRelatives('Category', ModelRelation::REF_CHILD)->contains($parent)); $c2 = Model::create('Category'); $c2->title = 'Child 2'; $c2->addRelatives($parent, ModelRelation::REF_CHILD); $test->addResult($parent->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c2)); $test->addResult($c2->findRelatives('Category', ModelRelation::REF_CHILD)->contains($parent)); $c1sub = Model::create('Category'); $c1sub->title = "Child 1 Sub"; $c1->addRelatives($c1sub); $test->addResult($c1->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c1sub)); $test->addResult($c1sub->findRelatives('Category', ModelRelation::REF_CHILD)->contains($c1)); $c2sub = Model::create('Category'); $c2sub->title = "Child 2 Sub"; $c2->addRelatives($c2sub); $test->addResult($c2->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c2sub)); $test->addResult($c2sub->findRelatives('Category', ModelRelation::REF_CHILD)->contains($c2)); $c1->disownRelatives($c1sub); $test->addResult(!$c1->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c1sub)); $test->addResult($c1sub->findRelatives('Category', ModelRelation::REF_CHILD)->isEmpty()); $parent->disownRelatives($c2); $test->addResult(!$parent->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c2)); $test->addResult($c2->findRelatives('Category', ModelRelation::REF_CHILD)->isEmpty()); $test->addResult($c2->findRelatives('Category', ModelRelation::REF_PARENT)->contains($c2sub)); $test->end(); /* ------------------------------------------------------------ M:M recursive */ /* $test = $this->startTest('M:M recursive');
* 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); $stmt->execute(); $c1 = new ModelCollection('Book', $stmt); $test->addResult($c1[6]->modelName == 'Book');