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.php30
2 files changed, 29 insertions, 4 deletions
diff --git a/framework/Data/ActiveRecord/Exceptions/messages.txt b/framework/Data/ActiveRecord/Exceptions/messages.txt
index 774c0275..53d8f4e3 100644
--- a/framework/Data/ActiveRecord/Exceptions/messages.txt
+++ b/framework/Data/ActiveRecord/Exceptions/messages.txt
@@ -12,4 +12,5 @@ ar_mismatch_args_exception = ActiveRecord finder method '{0}' expects {1} pa
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}'.
ar_missing_pk_values = Missing primary key values in forming IN(key1, key2, ...) for table '{0}'.
-ar_pk_value_count_mismatch = Composite key value count mismatch in forming IN( (key1, key2, ..), (key3, key4, ..)) for table '{0}'. \ No newline at end of file
+ar_pk_value_count_mismatch = Composite key value count mismatch in forming IN( (key1, key2, ..), (key3, key4, ..)) for table '{0}'.
+ar_must_copy_from_array_or_object = $data in {0}::copyFrom($data) must be an object or an array. \ No newline at end of file
diff --git a/framework/Data/ActiveRecord/TActiveRecord.php b/framework/Data/ActiveRecord/TActiveRecord.php
index 9272e1b9..9bc74c0f 100644
--- a/framework/Data/ActiveRecord/TActiveRecord.php
+++ b/framework/Data/ActiveRecord/TActiveRecord.php
@@ -424,7 +424,9 @@ abstract class TActiveRecord extends TComponent
* Dynamic find method using parts of method name as search criteria.
* Method name starting with "findBy" only returns 1 record.
* Method name starting with "findAllBy" returns 0 or more records.
- * The condition is taken as part of the method name after "findBy" or "findAllBy".
+ * Method name starting with "deleteBy" deletes records by the trail criteria.
+ * The condition is taken as part of the method name after "findBy", "findAllBy"
+ * or "deleteBy".
*
* The following are equivalent:
* <code>
@@ -440,25 +442,47 @@ abstract class TActiveRecord extends TComponent
* $finder->findAllByAge($age);
* $finder->findAll('Age = ?', $age);
* </code>
+ * <code>
+ * $finder->deleteAll('Name = ?', $name);
+ * $finder->deleteByName($name);
+ * </code>
* @return mixed single record if method name starts with "findBy", 0 or more records
* if method name starts with "findAllBy"
*/
public function __call($method,$args)
{
+ $delete =false;
if($findOne = substr(strtolower($method),0,6)==='findby')
$condition = $method[6]==='_' ? substr($method,7) : substr($method,6);
else if(substr(strtolower($method),0,9)==='findallby')
$condition = $method[9]==='_' ? substr($method,10) : substr($method,9);
+ else if($delete = substr(strtolower($method),0,8)==='deleteby')
+ $condition = $method[8]==='_' ? substr($method,9) : substr($method,8);
else
return null;//throw new TActiveRecordException('ar_invalid_finder_method',$method);
+
+ $criteria = $this->createCriteriaFromString($method, $condition, $args);
+ if($delete)
+ return $this->deleteAll($criteria);
+ else
+ return $findOne ? $this->find($criteria) : $this->findAll($criteria);
+ }
+
+ /**
+ * @param string __call method name
+ * @param string criteria conditions
+ * @param array method arguments
+ * @return TActiveRecordCriteria criteria created from the method name and its arguments.
+ */
+ private function createCriteriaFromString($method, $condition, $args)
+ {
$fields = array();
foreach(preg_split('/and|_and_/i',$condition) as $field)
$fields[] = $field.' = ?';
$args=count($args) === 1 && is_array($args[0]) ? $args[0] : $args;
if(count($fields)>count($args))
throw new TActiveRecordException('ar_mismatch_args_exception',$method,count($fields),count($args));
- $criteria = new TActiveRecordCriteria(implode(' AND ',$fields),$args);
- return $findOne ? $this->find($criteria) : $this->findAll($criteria);
+ return new TActiveRecordCriteria(implode(' AND ',$fields),$args);
}
}
?> \ No newline at end of file