示例#1
0
 /**
  * Delete a collection of Active Records from database
  * @param $criteria  An TCriteria object, specifiyng the filters
  * @return           The affected rows
  */
 public function delete(TCriteria $criteria = NULL)
 {
     if (!$criteria) {
         $criteria = isset($this->criteria) ? $this->criteria : new TCriteria();
     }
     $class = $this->class;
     // get the connection of the active transaction
     if ($conn = TTransaction::get()) {
         $dbinfo = TTransaction::getDatabaseInfo();
         // get dbinfo
         // first, clear cache
         $record = new $class();
         if ($cache = $record->getCacheControl()) {
             $pk = $record->getPrimaryKey();
             // creates a SELECT statement
             $sql = new TSqlSelect();
             $sql->addColumn($pk);
             $sql->setEntity($this->getEntity());
             // assign the criteria to the SELECT statement
             $sql->setCriteria($criteria);
             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 {
                 $result = $conn->query($sql->getInstruction());
             }
             if ($result) {
                 // iterate the results as objects
                 while ($row = $result->fetchObject()) {
                     $record_key = $class . '[' . $row->{$pk} . ']';
                     if ($cache::delValue($record_key)) {
                         TTransaction::log($record_key . ' deleted from cache');
                     }
                 }
             }
         }
         // creates a DELETE statement
         $sql = new TSqlDelete();
         $sql->setEntity($this->getEntity());
         // assign the criteria to the DELETE statement
         $sql->setCriteria($criteria);
         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 DELETE statement
             $result = $conn->exec($sql->getInstruction());
         }
         // register the operation in the LOG file
         TTransaction::log($sql->getInstruction());
         return $result;
     } else {
         // if there's no active transaction opened
         throw new Exception(AdiantiCoreTranslator::translate('No active transactions') . ': ' . __METHOD__ . ' ' . $this->getEntity());
     }
 }
示例#2
0
 /**
  * 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());
     }
 }