/** * Return the amount of objects that satisfy a given criteria * @param $criteria An TCriteria object, specifiyng the filters * @return An Integer containing the amount of objects that satisfy the criteria */ public function count(TCriteria $criteria = NULL) { if (!$criteria) { $criteria = isset($this->criteria) ? $this->criteria : new TCriteria(); } // creates a SELECT statement $sql = new TSqlSelect(); $sql->addColumn('count(*)'); $sql->setEntity($this->getEntity()); // assign the criteria to the SELECT statement $sql->setCriteria($criteria); // get the connection of the active transaction if ($conn = TTransaction::get()) { // register the operation in the LOG file TTransaction::log($sql->getInstruction()); $dbinfo = TTransaction::getDatabaseInfo(); // get dbinfo if (isset($dbinfo['prep']) and $dbinfo['prep'] == '1') { $result = $conn->prepare($sql->getInstruction(TRUE), array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $result->execute($criteria->getPreparedVars()); } else { // executes the SELECT statement $result = $conn->query($sql->getInstruction()); } if ($result) { $row = $result->fetch(); } // returns the result return $row[0]; } else { // if there's no active transaction opened throw new Exception(AdiantiCoreTranslator::translate('No active transactions') . ': ' . __METHOD__ . ' ' . $this->getEntity()); } }
/** * Load an Active Record Object from the database * @param $id The object ID * @return The Active Record Object * @exception Exception if there's no active transaction opened */ public function load($id) { $class = get_class($this); // get the Active Record class name $pk = $this->getPrimaryKey(); // discover the primary key name if ($cache = $this->getCacheControl()) { $record_key = $class . '[' . $id . ']'; if ($fetched_object_array = $cache::getValue($record_key)) { $fetched_object = clone $this; $fetched_object->fromArray($fetched_object_array); TTransaction::log($record_key . ' loaded from cache'); return $fetched_object; } } // creates a SELECT instruction $sql = new TSqlSelect(); $sql->setEntity($this->getEntity()); $sql->addColumn('*'); // creates a select criteria based on the ID $criteria = new TCriteria(); $criteria->add(new TFilter($pk, '=', $id)); // define the select criteria $sql->setCriteria($criteria); // get the connection of the active transaction if ($conn = TTransaction::get()) { // register the operation in the LOG file TTransaction::log($sql->getInstruction()); $dbinfo = TTransaction::getDatabaseInfo(); // get dbinfo if (isset($dbinfo['prep']) and $dbinfo['prep'] == '1') { $result = $conn->prepare($sql->getInstruction(TRUE), array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); $result->execute($criteria->getPreparedVars()); } else { // execute the query $result = $conn->query($sql->getInstruction()); } // if there's a result if ($result) { // returns the data as an object of this class $object = $result->fetchObject(get_class($this)); if ($object) { if ($cache = $this->getCacheControl()) { $record_key = $class . '[' . $id . ']'; if ($cache::setValue($record_key, $object->toArray())) { TTransaction::log($record_key . ' stored in cache'); } } } } return $object; } else { // if there's no active transaction opened throw new Exception(AdiantiCoreTranslator::translate('No active transactions') . ': ' . __METHOD__ . ' ' . $this->getEntity()); } }