From 5885bd12735637430e093c896499c3f26832fceb Mon Sep 17 00:00:00 2001 From: wei <> Date: Wed, 21 Mar 2007 03:27:12 +0000 Subject: minor AR updates, remove obsolete TApplication::$_pageService --- HISTORY | 2 + .../protected/pages/Database/Scaffold.page | 80 +- .../ActiveRecord/Scaffold/TScaffoldEditView.php | 4 +- framework/Data/ActiveRecord/TActiveRecord.php | 1092 ++++++++++---------- .../Data/ActiveRecord/TActiveRecordManager.php | 14 +- framework/TApplication.php | 4 - 6 files changed, 626 insertions(+), 570 deletions(-) diff --git a/HISTORY b/HISTORY index 9975aff9..e4ee9543 100644 --- a/HISTORY +++ b/HISTORY @@ -7,6 +7,7 @@ BUG: Ticket#549 - set/get Timestamp on TDatePicker shound handle "null" values ( BUG: Ticket#550 - explicitly include TCallbackClientSide in TCallbackOptions (Qiang) BUG: Ticket#553 - I18N quickstart sample does not work (Qiang) BUG: Ticket#555 - TNumberFormat throws exception (Qiang) +BUG: Ticket#573 - NumberFormat Bug (Wei) BUG: TXmlElement did not encode attribute and text values when being saved as a string (Qiang) BUG: SelectedIndices not return expected result for ActiveListBox (Wei) ENH: Ticket#503 - Localization and parameter tags can now appear as a substring in a property initial value (Qiang) @@ -53,6 +54,7 @@ BUG: Ticket#481 - Unable to cancel navigation when handling OnSideBarButtonClick BUG: Ticket#505 - cultureInfo::getEnglishName does not return an arrary (Wei) BUG: Ticket#508 - CultureInfo class: PHP Notice because of missing static declaration (Wei) BUG: Ticket#558 - TRadionButtonList generates a empty onclick attribute (Qiang) +BUG: Ticket#573 - NumberFormat Bug (Wei) BUG: typo in THttpResponse.writeFile() about sending headers (Qiang) Version 3.0.6 December 4, 2006 diff --git a/demos/quickstart/protected/pages/Database/Scaffold.page b/demos/quickstart/protected/pages/Database/Scaffold.page index 055cac26..b2d52b6d 100644 --- a/demos/quickstart/protected/pages/Database/Scaffold.page +++ b/demos/quickstart/protected/pages/Database/Scaffold.page @@ -17,6 +17,7 @@ the following builtin functionality:
The above code will list the current records in the users table. +Each record can be edited by clicking on the "edit" button and deleted by +clicking on the "delete" button. A new record can be added by clicking on the +"Add new record" button, enter some data (notice the automatic validation of required fields and data types), and click the "save" button. +Specifying search terms in the search textbox to find particular records. Finally, the +record list can be sorted for each column by changing the sorting column and order. +
+ +The TScaffoldView is a template control composed of other scaffold controls. +The following properties gives access to these composite controls.
++ All these composite controls can be customized as we shall see below. +
+ +A list of Active Records can be displayed using the TScaffoldListView +with the following useful properties.
+Other views... list view
+Custom rendering of the each Active Record can be achieved by specifying +the ItemTemplate and/or AlternatingItemTemplate property of the List +repeater. +The TScaffoldListView will listen for two command events named "delete" and +"edit". A "delete" command will delete a the record for the row where the "delete" command originates. +An "edit" command will push the record data to be edited by a +TScaffoldEditView with ID specified by the EditViewID property. +The following example lists the usernames only with bold formatting. +
edit view...
+The SearchCondition property and +SearchParameters property (takes array values) can be +specified to customize the records to be shown. The SearchCondition +will be used as the Condition property of TActiveRecordCriteria +and the SearchParameters property corresponds to +Parameters property of TActiveRecordCriteria.
+ +Combining list + edit views
+custom list view...
+To be completed...
- -Address book example...
- -
- * class UserRecord extends TActiveRecord
- * {
- * const TABLE='users'; //optional table name.
- *
- * public $username; //corresponds to the fieldname in the table
- * public $email;
- *
- * //returns active record finder instance
- * public static function finder($className=__CLASS__)
- * {
- * return parent::finder($className);
- * }
- * }
- *
- * //create a connection and give it to the ActiveRecord manager.
- * $dsn = 'pgsql:host=localhost;dbname=test';
- * $conn = new TDbConnection($dsn, 'dbuser','dbpass');
- * TActiveRecordManager::getInstance()->setDbConnection($conn);
- *
- * //load the user record with username (primary key) 'admin'.
- * $user = UserRecord::finder()->findByPk('admin');
- * $user->email = 'admin@example.org';
- * $user->save(); //update the 'admin' record.
- *
- *
- * @author Wei Zhuo
- * $finder->deleteByPk($primaryKey); //delete 1 record
- * $finder->deleteByPk($key1,$key2,...); //delete multiple records
- * $finder->deleteByPk(array($key1,$key2,...)); //delete multiple records
- *
- *
- * For composite primary keys (determined from the table definitions):
- *
- * $finder->deleteByPk(array($key1,$key2)); //delete 1 record
- *
- * //delete multiple records
- * $finder->deleteByPk(array($key1,$key2), array($key3,$key4),...);
- *
- * //delete multiple records
- * $finder->deleteByPk(array( array($key1,$key2), array($key3,$key4), .. ));
- *
- *
- * @param mixed primary key values.
- * @return int number of records deleted.
- */
- public function deleteByPk($keys)
- {
- if(func_num_args() > 1)
- $keys = func_get_args();
- $gateway = $this->getRecordManager()->getRecordGateway();
- 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
- * @param array name value pair record data
- * @return TActiveRecord object record, null if data is empty.
- */
- protected function populateObject($type, $data)
- {
- if(empty($data)) return null;
- $registry = $this->getRecordManager()->getObjectStateRegistry();
-
- //try the cache (the cache object must be clean)
- if(!is_null($obj = $registry->getCachedInstance($data)))
- return $obj;
-
- //create and populate the object
- $obj = Prado::createComponent($type);
- foreach($data as $name => $value)
- $obj->{$name} = $value;
-
- $gateway = $this->getRecordManager()->getRecordGateway();
- $obj->_readOnly = $gateway->getMetaData($this)->getIsView();
-
- //cache it
- return $registry->addCachedInstance($data,$obj);
- }
-
- /**
- * Find one single record that matches the criteria.
- *
- * Usage:
- *
- * $finder->find('username = :name AND password = :pass',
- * array(':name'=>$name, ':pass'=>$pass));
- * $finder->find('username = ? AND password = ?', array($name, $pass));
- * $finder->find('username = ? AND password = ?', $name, $pass);
- * //$criteria is of TActiveRecordCriteria
- * $finder->find($criteria); //the 2nd parameter for find() is ignored.
- *
- *
- * @param string|TActiveRecordCriteria SQL condition or criteria object.
- * @param mixed parameter values.
- * @return TActiveRecord matching record object.
- */
- public function find($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();
- $data = $gateway->findRecordsByCriteria($this,$criteria);
- return $this->populateObject(get_class($this), $data);
- }
-
- /**
- * Same as find() but returns an array of objects.
- *
- * @param string|TActiveRecordCriteria SQL condition or criteria object.
- * @param mixed parameter values.
- * @return array matching record objects
- */
- public function findAll($criteria=null,$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();
- $results = array();
- $class = get_class($this);
- foreach($gateway->findRecordsByCriteria($this,$criteria,true) as $data)
- $results[] = $this->populateObject($class,$data);
- return $results;
- }
-
- /**
- * Find one record using only the primary key or composite primary keys. Usage:
- *
- *
- * $finder->findByPk($primaryKey);
- * $finder->findByPk($key1, $key2, ...);
- * $finder->findByPk(array($key1,$key2,...));
- *
- *
- * @param mixed primary keys
- * @return TActiveRecord
- */
- public function findByPk($keys)
- {
- if(func_num_args() > 1)
- $keys = func_get_args();
- $gateway = $this->getRecordManager()->getRecordGateway();
- $data = $gateway->findRecordByPK($this,$keys);
- return $this->populateObject(get_class($this), $data);
- }
-
- /**
- * Find multiple records matching a list of primary or composite keys.
- *
- * For scalar primary keys:
- *
- * $finder->findAllByPk($key1, $key2, ...);
- * $finder->findAllByPk(array($key1, $key2, ...));
- *
- *
- * For composite keys:
- *
- * $finder->findAllByPk(array($key1, $key2), array($key3, $key4), ...);
- * $finder->findAllByPk(array(array($key1, $key2), array($key3, $key4), ...));
- *
- * @param mixed primary keys
- * @return array matching ActiveRecords
- */
- public function findAllByPks($keys)
- {
- if(func_num_args() > 1)
- $keys = func_get_args();
- $gateway = $this->getRecordManager()->getRecordGateway();
- $results = array();
- $class = get_class($this);
- foreach($gateway->findRecordsByPks($this,(array)$keys) as $data)
- $results[] = $this->populateObject($class,$data);
- return $results;
- }
-
- /**
- * Find records using full SQL, returns corresponding record object.
- * The names of the column retrieved must be defined in your Active Record
- * class.
- * @param string select SQL
- * @param array $parameters
- * @return array matching active records.
- */
- public function findBySql($sql,$parameters=array())
- {
- if(!is_array($parameters) && func_num_args() > 1)
- {
- $parameters = func_get_args();
- array_shift($parameters);
- }
- $gateway = $this->getRecordManager()->getRecordGateway();
- $data = $gateway->findRecordsBySql($this,$sql,$parameters);
- $results = array();
- $class = get_class($this);
- foreach($gateway->findRecordsBySql($this,$sql,$parameters) as $data)
- $results[] = $this->populateObject($class,$data);
- return $results;
- }
-
- /**
- * Find the number of records.
- * @param string|TActiveRecordCriteria SQL condition or criteria object.
- * @param mixed parameter values.
- * @return int number of records.
- */
- public function count($criteria=null,$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->countRecords($this,$criteria);
- }
-
- /**
- * 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.
- * 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:
- *
- * $finder->findByName($name)
- * $finder->find('Name = ?', $name);
- *
- *
- * $finder->findByUsernameAndPassword($name,$pass); // OR may be used
- * $finder->findBy_Username_And_Password($name,$pass); // _OR_ may be used
- * $finder->find('Username = ? AND Password = ?', $name, $pass);
- *
- *
- * $finder->findAllByAge($age);
- * $finder->findAll('Age = ?', $age);
- *
- *
- * $finder->deleteAll('Name = ?', $name);
- * $finder->deleteByName($name);
- *
- * @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 = $this->extractMatchingConditions($method, $condition);
- $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));
- }
- return new TActiveRecordCriteria(implode(' ',$fields),$args);
- }
-
- /**
- * Calculates the AND/OR condition from dynamic method substrings using
- * table meta data, allows for any AND-OR combinations.
- * @param string dynamic method name
- * @param string dynamic method search criteria
- * @return array search condition substrings
- */
- private function extractMatchingConditions($method, $condition)
- {
- $meta = $this->getRecordManager()->getRecordGateway()->getMetaData($this);
- $search = implode('|', $meta->getColumnNames());
- $regexp = '/('.$search.')(and|_and_|or|_or_)?/i';
- $matches = array();
- if(!preg_match_all($regexp, strtolower($condition), $matches,PREG_SET_ORDER))
- {
- throw new TActiveRecordException('ar_mismatch_column_names',
- $method, implode(', ', $meta->getColumnNames()), $meta->getTableName());
- }
- $fields = array();
- foreach($matches as $match)
- {
- $column = $meta->getColumn($match[1]);
- $sql = $column->getName() . ' = ? ';
- if(count($match) > 2)
- $sql .= strtoupper(str_replace('_', '', $match[2]));
- $fields[] = $sql;
- }
- return $fields;
- }
+/**
+ * TActiveRecord and TActiveRecordEventParameter class file.
+ *
+ * @author Wei Zhuo
+ * class UserRecord extends TActiveRecord
+ * {
+ * const TABLE='users'; //optional table name.
+ *
+ * public $username; //corresponds to the fieldname in the table
+ * public $email;
+ *
+ * //returns active record finder instance
+ * public static function finder($className=__CLASS__)
+ * {
+ * return parent::finder($className);
+ * }
+ * }
+ *
+ * //create a connection and give it to the ActiveRecord manager.
+ * $dsn = 'pgsql:host=localhost;dbname=test';
+ * $conn = new TDbConnection($dsn, 'dbuser','dbpass');
+ * TActiveRecordManager::getInstance()->setDbConnection($conn);
+ *
+ * //load the user record with username (primary key) 'admin'.
+ * $user = UserRecord::finder()->findByPk('admin');
+ * $user->email = 'admin@example.org';
+ * $user->save(); //update the 'admin' record.
+ *
+ *
+ * @author Wei Zhuo
+ * $finder->deleteByPk($primaryKey); //delete 1 record
+ * $finder->deleteByPk($key1,$key2,...); //delete multiple records
+ * $finder->deleteByPk(array($key1,$key2,...)); //delete multiple records
+ *
+ *
+ * For composite primary keys (determined from the table definitions):
+ *
+ * $finder->deleteByPk(array($key1,$key2)); //delete 1 record
+ *
+ * //delete multiple records
+ * $finder->deleteByPk(array($key1,$key2), array($key3,$key4),...);
+ *
+ * //delete multiple records
+ * $finder->deleteByPk(array( array($key1,$key2), array($key3,$key4), .. ));
+ *
+ *
+ * @param mixed primary key values.
+ * @return int number of records deleted.
+ */
+ public function deleteByPk($keys)
+ {
+ if(func_num_args() > 1)
+ $keys = func_get_args();
+ $gateway = $this->getRecordManager()->getRecordGateway();
+ 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
+ * @param array name value pair record data
+ * @return TActiveRecord object record, null if data is empty.
+ */
+ protected function populateObject($type, $data)
+ {
+ if(empty($data)) return null;
+ $registry = $this->getRecordManager()->getObjectStateRegistry();
+
+ //try the cache (the cache object must be clean)
+ if(!is_null($obj = $registry->getCachedInstance($data)))
+ return $obj;
+
+ //create and populate the object
+ $obj = Prado::createComponent($type);
+ foreach($data as $name => $value)
+ $obj->{$name} = $value;
+
+ $gateway = $this->getRecordManager()->getRecordGateway();
+ $obj->_readOnly = $gateway->getMetaData($this)->getIsView();
+
+ //cache it
+ return $registry->addCachedInstance($data,$obj);
+ }
+
+ /**
+ * Find one single record that matches the criteria.
+ *
+ * Usage:
+ *
+ * $finder->find('username = :name AND password = :pass',
+ * array(':name'=>$name, ':pass'=>$pass));
+ * $finder->find('username = ? AND password = ?', array($name, $pass));
+ * $finder->find('username = ? AND password = ?', $name, $pass);
+ * //$criteria is of TActiveRecordCriteria
+ * $finder->find($criteria); //the 2nd parameter for find() is ignored.
+ *
+ *
+ * @param string|TActiveRecordCriteria SQL condition or criteria object.
+ * @param mixed parameter values.
+ * @return TActiveRecord matching record object.
+ */
+ public function find($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();
+ $data = $gateway->findRecordsByCriteria($this,$criteria);
+ return $this->populateObject(get_class($this), $data);
+ }
+
+ /**
+ * Same as find() but returns an array of objects.
+ *
+ * @param string|TActiveRecordCriteria SQL condition or criteria object.
+ * @param mixed parameter values.
+ * @return array matching record objects
+ */
+ public function findAll($criteria=null,$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();
+ $results = array();
+ $class = get_class($this);
+ foreach($gateway->findRecordsByCriteria($this,$criteria,true) as $data)
+ $results[] = $this->populateObject($class,$data);
+ return $results;
+ }
+
+ /**
+ * Find one record using only the primary key or composite primary keys. Usage:
+ *
+ *
+ * $finder->findByPk($primaryKey);
+ * $finder->findByPk($key1, $key2, ...);
+ * $finder->findByPk(array($key1,$key2,...));
+ *
+ *
+ * @param mixed primary keys
+ * @return TActiveRecord
+ */
+ public function findByPk($keys)
+ {
+ if(func_num_args() > 1)
+ $keys = func_get_args();
+ $gateway = $this->getRecordManager()->getRecordGateway();
+ $data = $gateway->findRecordByPK($this,$keys);
+ return $this->populateObject(get_class($this), $data);
+ }
+
+ /**
+ * Find multiple records matching a list of primary or composite keys.
+ *
+ * For scalar primary keys:
+ *
+ * $finder->findAllByPk($key1, $key2, ...);
+ * $finder->findAllByPk(array($key1, $key2, ...));
+ *
+ *
+ * For composite keys:
+ *
+ * $finder->findAllByPk(array($key1, $key2), array($key3, $key4), ...);
+ * $finder->findAllByPk(array(array($key1, $key2), array($key3, $key4), ...));
+ *
+ * @param mixed primary keys
+ * @return array matching ActiveRecords
+ */
+ public function findAllByPks($keys)
+ {
+ if(func_num_args() > 1)
+ $keys = func_get_args();
+ $gateway = $this->getRecordManager()->getRecordGateway();
+ $results = array();
+ $class = get_class($this);
+ foreach($gateway->findRecordsByPks($this,(array)$keys) as $data)
+ $results[] = $this->populateObject($class,$data);
+ return $results;
+ }
+
+ /**
+ * Find records using full SQL, returns corresponding record object.
+ * The names of the column retrieved must be defined in your Active Record
+ * class.
+ * @param string select SQL
+ * @param array $parameters
+ * @return array matching active records.
+ */
+ public function findBySql($sql,$parameters=array())
+ {
+ if(!is_array($parameters) && func_num_args() > 1)
+ {
+ $parameters = func_get_args();
+ array_shift($parameters);
+ }
+ $gateway = $this->getRecordManager()->getRecordGateway();
+ $data = $gateway->findRecordsBySql($this,$sql,$parameters);
+ $results = array();
+ $class = get_class($this);
+ foreach($gateway->findRecordsBySql($this,$sql,$parameters) as $data)
+ $results[] = $this->populateObject($class,$data);
+ return $results;
+ }
+
+ /**
+ * Find the number of records.
+ * @param string|TActiveRecordCriteria SQL condition or criteria object.
+ * @param mixed parameter values.
+ * @return int number of records.
+ */
+ public function count($criteria=null,$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->countRecords($this,$criteria);
+ }
+
+ /**
+ * 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.
+ * 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:
+ *
+ * $finder->findByName($name)
+ * $finder->find('Name = ?', $name);
+ *
+ *
+ * $finder->findByUsernameAndPassword($name,$pass); // OR may be used
+ * $finder->findBy_Username_And_Password($name,$pass); // _OR_ may be used
+ * $finder->find('Username = ? AND Password = ?', $name, $pass);
+ *
+ *
+ * $finder->findAllByAge($age);
+ * $finder->findAll('Age = ?', $age);
+ *
+ *
+ * $finder->deleteAll('Name = ?', $name);
+ * $finder->deleteByName($name);
+ *
+ * @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 = $this->extractMatchingConditions($method, $condition);
+ $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));
+ }
+ return new TActiveRecordCriteria(implode(' ',$fields),$args);
+ }
+
+ /**
+ * Calculates the AND/OR condition from dynamic method substrings using
+ * table meta data, allows for any AND-OR combinations.
+ * @param string dynamic method name
+ * @param string dynamic method search criteria
+ * @return array search condition substrings
+ */
+ private function extractMatchingConditions($method, $condition)
+ {
+ $meta = $this->getRecordManager()->getRecordGateway()->getMetaData($this);
+ $search = implode('|', $meta->getColumnNames());
+ $regexp = '/('.$search.')(and|_and_|or|_or_)?/i';
+ $matches = array();
+ if(!preg_match_all($regexp, strtolower($condition), $matches,PREG_SET_ORDER))
+ {
+ throw new TActiveRecordException('ar_mismatch_column_names',
+ $method, implode(', ', $meta->getColumnNames()), $meta->getTableName());
+ }
+ $fields = array();
+ foreach($matches as $match)
+ {
+ $column = $meta->getColumn($match[1]);
+ $sql = $column->getName() . ' = ? ';
+ if(count($match) > 2)
+ $sql .= strtoupper(str_replace('_', '', $match[2]));
+ $fields[] = $sql;
+ }
+ return $fields;
+ }
}
-?>
\ No newline at end of file
+
+/**
+ * TActiveRecordEventParameter class.
+ *
+ * @author Wei Zhuo