/** * 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()); } }
/** * Returns the LAST Object ID from database * @return An Integer containing the LAST Object ID from database * @exception Exception if there's no active transaction opened */ public function getLastID() { $pk = $this->getPrimaryKey(); // get the connection of the active transaction if ($conn = TTransaction::get()) { // instancia instrução de SELECT $sql = new TSqlSelect(); $sql->addColumn("max({$pk}) as {$pk}"); $sql->setEntity($this->getEntity()); // register the operation in the LOG file TTransaction::log($sql->getInstruction()); $result = $conn->Query($sql->getInstruction()); // retorna os dados do banco $row = $result->fetch(); return $row[0]; } else { // if there's no active transaction opened throw new Exception(AdiantiCoreTranslator::translate('No active transactions') . ': ' . __METHOD__ . ' ' . $this->getEntity()); } }