/** * 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()); } }
/** * Delete an Active Record object from the database * @param [$id] The Object ID * @exception Exception if there's no active transaction opened */ public function delete($id = NULL) { $class = get_class($this); // discover the primary key name $pk = $this->getPrimaryKey(); // if the user has not passed the ID, take the object ID $id = $id ? $id : $this->{$pk}; // creates a DELETE instruction $sql = new TSqlDelete(); $sql->setEntity($this->getEntity()); // creates a select criteria $criteria = new TCriteria(); $criteria->add(new TFilter($pk, '=', $id)); // assign the criteria to the delete instruction $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()); } unset($this->data); if ($cache = $this->getCacheControl()) { $record_key = $class . '[' . $id . ']'; if ($cache::delValue($record_key)) { TTransaction::log($record_key . ' deleted from cache'); } } // return the result of the exec() method return $result; } else { // if there's no active transaction opened throw new Exception(AdiantiCoreTranslator::translate('No active transactions') . ': ' . __METHOD__ . ' ' . $this->getEntity()); } }