From dd028bec3822d1d9c28c35d599d687e038c7705f Mon Sep 17 00:00:00 2001 From: wei <> Date: Thu, 4 Jan 2007 11:23:26 +0000 Subject: Add chat demo and tutorial. --- .../Data/ActiveRecord/Exceptions/messages.txt | 3 ++- framework/Data/ActiveRecord/TActiveRecord.php | 22 ++++++++++++++++++++++ .../Data/ActiveRecord/TActiveRecordGateway.php | 14 ++++++++++++++ .../Data/ActiveRecord/Vendor/TDbMetaDataCommon.php | 19 +++++++++++++++++-- 4 files changed, 55 insertions(+), 3 deletions(-) (limited to 'framework/Data/ActiveRecord') diff --git a/framework/Data/ActiveRecord/Exceptions/messages.txt b/framework/Data/ActiveRecord/Exceptions/messages.txt index f77b2275..92bdb30f 100644 --- a/framework/Data/ActiveRecord/Exceptions/messages.txt +++ b/framework/Data/ActiveRecord/Exceptions/messages.txt @@ -9,4 +9,5 @@ ar_no_primary_key_found = Table '{0}' does not contain any primary key fiel ar_primary_key_is_scalar = Primary key '{1}' in table '{0}' is NOT a composite key, invalid value '{2} used. ar_invalid_db_connection = Missing or invalid default database connection for ActiveRecord class '{0}', default connection is set by the DbConnection property of TActiveRecordManager. ar_mismatch_args_exception = ActiveRecord finder method '{0}' expects {1} parameters but found only {2} parameters instead. -ar_invalid_tablename_property = ActiveRecord tablename property '{0}::${1}' must be static and not null. \ No newline at end of file +ar_invalid_tablename_property = ActiveRecord tablename property '{0}::${1}' must be static and not null. +ar_value_must_not_be_null = Property '{0}::${2}' must not be null as defined by column '{2}' in table '{1}'. \ No newline at end of file diff --git a/framework/Data/ActiveRecord/TActiveRecord.php b/framework/Data/ActiveRecord/TActiveRecord.php index 0855fabf..68d63a23 100644 --- a/framework/Data/ActiveRecord/TActiveRecord.php +++ b/framework/Data/ActiveRecord/TActiveRecord.php @@ -210,6 +210,28 @@ abstract class TActiveRecord extends TComponent return $gateway->deleteRecordsByPk($this,(array)$keys); } + + /** + * Delete multiple records using a criteria. + * @param string|TActiveRecordCriteria SQL condition or criteria object. + * @param mixed parameter values. + * @return int number of records deleted. + */ + public function deleteAll($criteria, $parameters=array()) + { + if(is_string($criteria)) + { + if(!is_array($parameters) && func_num_args() > 1) + { + $parameters = func_get_args(); + array_shift($parameters); + } + $criteria=new TActiveRecordCriteria($criteria,$parameters); + } + $gateway = $this->getRecordManager()->getRecordGateway(); + return $gateway->deleteRecordsByCriteria($this, $criteria); + } + /** * Populate the record with data, registers the object as clean. * @param string new record name diff --git a/framework/Data/ActiveRecord/TActiveRecordGateway.php b/framework/Data/ActiveRecord/TActiveRecordGateway.php index e7ea5e46..1cb1c79f 100644 --- a/framework/Data/ActiveRecord/TActiveRecordGateway.php +++ b/framework/Data/ActiveRecord/TActiveRecordGateway.php @@ -246,6 +246,20 @@ class TActiveRecordGateway extends TComponent return $command->execute(); } + /** + * Delete multiple records by criteria. + * @param TActiveRecord active record finder instance. + * @param TActiveRecordCriteria search criteria + * @return int number of records. + */ + public function deleteRecordsByCriteria(TActiveRecord $record, $criteria) + { + $meta = $this->getMetaData($record); + $command = $meta->getDeleteByCriteriaCommand($record->getDBConnection(),$criteria); + $this->raiseCommandEvent(TActiveRecordStatementType::Delete,$command,$record,$criteria); + return $command->execute(); + } + /** * Raise the corresponding command event, insert, update, delete or select. * @param string command type diff --git a/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php b/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php index 74c97689..fffdb6fb 100644 --- a/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php +++ b/framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php @@ -161,9 +161,9 @@ abstract class TDbMetaDataCommon extends TDbMetaData { $conn->setActive(true); $numKeys = count($this->getPrimaryKeys()); - if($numKeys===0) - throw new TActiveRecordException('ar_no_primary_key_found',$this->getTableName()); $table = $this->getTableName(); + if($numKeys===0) + throw new TActiveRecordException('ar_no_primary_key_found',$table); if($numKeys===1) $criteria = $this->getDeleteInPkCriteria($conn,$keys); else @@ -173,6 +173,21 @@ abstract class TDbMetaDataCommon extends TDbMetaData $command->prepare(); return $command; } + + + /** + * SQL command to delete records by criteria + * @param TDbConnection database connection. + * @param TActiveRecordCriteria criteria object. + * @return TDbCommand delete command. + */ + public function getDeleteByCriteriaCommand($conn, $criteria) + { + $conditions = $criteria!==null?$this->getSqlFromCriteria($conn,$criteria) : ''; + $table = $this->getTableName(); + $sql = "DELETE FROM {$table} {$conditions}"; + return $this->createCriteriaBindedCommand($conn,$sql, $criteria); + } } ?> \ No newline at end of file -- cgit v1.2.3