summaryrefslogtreecommitdiff
path: root/framework/Data/ActiveRecord
diff options
context:
space:
mode:
Diffstat (limited to 'framework/Data/ActiveRecord')
-rw-r--r--framework/Data/ActiveRecord/Exceptions/messages.txt3
-rw-r--r--framework/Data/ActiveRecord/TActiveRecord.php22
-rw-r--r--framework/Data/ActiveRecord/TActiveRecordGateway.php14
-rw-r--r--framework/Data/ActiveRecord/Vendor/TDbMetaDataCommon.php19
4 files changed, 55 insertions, 3 deletions
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
@@ -247,6 +247,20 @@ class TActiveRecordGateway extends TComponent
}
/**
+ * 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
* @param TDbCommand sql command to be executed.
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