diff options
Diffstat (limited to 'framework/Data/ActiveRecord')
5 files changed, 134 insertions, 11 deletions
diff --git a/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php b/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php index a352cb07..a3daf35c 100644 --- a/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php +++ b/framework/Data/ActiveRecord/Relations/TActiveRecordRelation.php @@ -84,7 +84,7 @@ abstract class TActiveRecordRelation $obj->collectForeignObjects($results);
}
else if($results instanceof TActiveRecordRelation)
- array_push($stack,$this); //call it later
+ $stack[] = $this; //call it later
return $results;
}
@@ -109,14 +109,17 @@ abstract class TActiveRecordRelation protected function findForeignKeys($from, $matchesRecord, $loose=false)
{
$gateway = $matchesRecord->getRecordGateway();
- $matchingTableName = $gateway->getRecordTableInfo($matchesRecord)->getTableName();
+ $recordTableInfo = $gateway->getRecordTableInfo($matchesRecord);
+ $matchingTableName = strtolower($recordTableInfo->getTableName());
+ $matchingFullTableName = strtolower($recordTableInfo->getTableFullName());
$tableInfo=$from;
if($from instanceof TActiveRecord)
$tableInfo = $gateway->getRecordTableInfo($from);
//find first non-empty FK
foreach($tableInfo->getForeignKeys() as $fkeys)
{
- if(strtolower($fkeys['table'])===strtolower($matchingTableName))
+ $fkTable = strtolower($fkeys['table']);
+ if($fkTable===$matchingTableName || $fkTable===$matchingFullTableName)
{
$hasFkField = !$loose && $this->getContext()->hasFkField();
$key = $hasFkField ? $this->getFkFields($fkeys['keys']) : $fkeys['keys'];
diff --git a/framework/Data/ActiveRecord/TActiveRecord.php b/framework/Data/ActiveRecord/TActiveRecord.php index fa134a9f..af171bbd 100644 --- a/framework/Data/ActiveRecord/TActiveRecord.php +++ b/framework/Data/ActiveRecord/TActiveRecord.php @@ -1,6 +1,6 @@ <?php /** - * TActiveRecord and TActiveRecordEventParameter class file. + * TActiveRecord, TActiveRecordEventParameter, TActiveRecordInvalidFinderResult class file. * * @author Wei Zhuo <weizhuo[at]gmail[dot]com> * @link http://www.pradosoft.com/ @@ -189,6 +189,15 @@ abstract class TActiveRecord extends TComponent */ protected $_connection; // use protected so that serialization is fine + + /** + * Defaults to 'null' + * + * @var TActiveRecordInvalidFinderResult + * @since 3.1.5 + */ + protected $_invalidFinderResult = null; // use protected so that serialization is fine + /** * Prevent __call() method creating __sleep() when serializing. */ @@ -527,9 +536,8 @@ abstract class TActiveRecord extends TComponent protected function populateObjects($reader) { $result=array(); - $class = get_class($this); foreach($reader as $data) - $result[] = self::createRecord($class, $data); + $result[] = $this->populateObject($data); return $result; } @@ -830,7 +838,12 @@ abstract class TActiveRecord extends TComponent else if($delete=strncasecmp($method,'deleteallby',11)===0) $condition = $method[11]==='_' ? substr($method,12) : substr($method,11); else - return null;//throw new TActiveRecordException('ar_invalid_finder_method',$method); + { + if($this->getInvalidFinderResult() == TActiveRecordInvalidFinderResult::Exception) + throw new TActiveRecordException('ar_invalid_finder_method',$method); + else + return null; + } $criteria = $this->getRecordGateway()->getCommand($this)->createCriteriaFromString($method, $condition, $args); if($delete) @@ -840,6 +853,34 @@ abstract class TActiveRecord extends TComponent } /** + * @return TActiveRecordInvalidFinderResult Defaults to '{@link TActiveRecordInvalidFinderResult::Null Null}'. + * @see TActiveRecordManager::getInvalidFinderResult + * @since 3.1.5 + */ + public function getInvalidFinderResult() + { + if($this->_invalidFinderResult !== null) + return $this->_invalidFinderResult; + + return self::getRecordManager()->getInvalidFinderResult(); + } + + /** + * Define the way an active record finder react if an invalid magic-finder invoked + * + * @param TActiveRecordInvalidFinderResult|null + * @see TActiveRecordManager::setInvalidFinderResult + * @since 3.1.5 + */ + public function setInvalidFinderResult($value) + { + if($value === null) + $this->_invalidFinderResult = null; + else + $this->_invalidFinderResult = TPropertyValue::ensureEnum($value, 'TActiveRecordInvalidFinderResult'); + } + + /** * Create a new TSqlCriteria object from a string $criteria. The $args * are additional parameters and are used in place of the $parameters * if $parameters is not an array and $args is an arrary. @@ -1022,3 +1063,25 @@ class TActiveRecordChangeEventParameter extends TEventParameter } } +/** + * TActiveRecordInvalidFinderResult class. + * TActiveRecordInvalidFinderResult defines the enumerable type for possible results + * if an invalid {@link TActiveRecord::__call magic-finder} invoked. + * + * The following enumerable values are defined: + * - Null: return null (default) + * - Exception: throws a TActiveRecordException + * + * @author Yves Berkholz <godzilla80@gmx.net> + * @version $Id$ + * @package System.Data.ActiveRecord + * @see TActiveRecordManager::setInvalidFinderResult + * @see TActiveRecordConfig::setInvalidFinderResult + * @see TActiveRecord::setInvalidFinderResult + * @since 3.1.5 + */ +class TActiveRecordInvalidFinderResult extends TEnumerable +{ + const Null = 'Null'; + const Exception = 'Exception'; +} diff --git a/framework/Data/ActiveRecord/TActiveRecordConfig.php b/framework/Data/ActiveRecord/TActiveRecordConfig.php index 63f05aef..51278fc9 100644 --- a/framework/Data/ActiveRecord/TActiveRecordConfig.php +++ b/framework/Data/ActiveRecord/TActiveRecordConfig.php @@ -11,6 +11,7 @@ */
Prado::using('System.Data.TDataSourceConfig');
+Prado::using('System.Data.ActiveRecord.TActiveRecordManager');
/**
* TActiveRecordConfig module configuration class.
@@ -74,17 +75,25 @@ class TActiveRecordConfig extends TDataSourceConfig private $_enableCache=false;
/**
+ * Defaults to '{@link TActiveRecordInvalidFinderResult::Null Null}'
+ *
+ * @var TActiveRecordInvalidFinderResult
+ * @since 3.1.5
+ */
+ private $_invalidFinderResult = TActiveRecordInvalidFinderResult::Null;
+
+ /**
* Initialize the active record manager.
* @param TXmlDocument xml configuration.
*/
public function init($xml)
{
parent::init($xml);
- Prado::using('System.Data.ActiveRecord.TActiveRecordManager');
$manager = TActiveRecordManager::getInstance();
if($this->getEnableCache())
$manager->setCache($this->getApplication()->getCache());
$manager->setDbConnection($this->getDbConnection());
+ $manager->setInvalidFinderResult($this->getInvalidFinderResult());
}
/**
@@ -103,5 +112,26 @@ class TActiveRecordConfig extends TDataSourceConfig {
return $this->_enableCache;
}
+
+ /**
+ * @return TActiveRecordInvalidFinderResult Defaults to '{@link TActiveRecordInvalidFinderResult::Null Null}'.
+ * @see setInvalidFinderResult
+ * @since 3.1.5
+ */
+ public function getInvalidFinderResult()
+ {
+ return $this->_invalidFinderResult;
+ }
+
+ /**
+ * Define the way an active record finder react if an invalid magic-finder invoked
+ *
+ * @param TActiveRecordInvalidFinderResult
+ * @see getInvalidFinderResult
+ * @since 3.1.5
+ */
+ public function setInvalidFinderResult($value)
+ {
+ $this->_invalidFinderResult = TPropertyValue::ensureEnum($value, 'TActiveRecordInvalidFinderResult');
+ }
}
- diff --git a/framework/Data/ActiveRecord/TActiveRecordGateway.php b/framework/Data/ActiveRecord/TActiveRecordGateway.php index 6cce9eb9..e588b976 100644 --- a/framework/Data/ActiveRecord/TActiveRecordGateway.php +++ b/framework/Data/ActiveRecord/TActiveRecordGateway.php @@ -349,7 +349,7 @@ class TActiveRecordGateway extends TComponent $tableInfo->getTableFullName(), $name); } if($column->getIsPrimaryKey()) - $primary[] = $value; + $primary[$name] = $value; else $values[$name] = $value; } diff --git a/framework/Data/ActiveRecord/TActiveRecordManager.php b/framework/Data/ActiveRecord/TActiveRecordManager.php index 9912e7ff..cba746ec 100644 --- a/framework/Data/ActiveRecord/TActiveRecordManager.php +++ b/framework/Data/ActiveRecord/TActiveRecordManager.php @@ -44,6 +44,14 @@ class TActiveRecordManager extends TComponent private $_cache;
/**
+ * Defaults to '{@link TActiveRecordInvalidFinderResult::Null Null}'
+ *
+ * @var TActiveRecordInvalidFinderResult
+ * @since 3.1.5
+ */
+ private $_invalidFinderResult = TActiveRecordInvalidFinderResult::Null;
+
+ /**
* @return ICache application cache.
*/
public function getCache()
@@ -106,6 +114,25 @@ class TActiveRecordManager extends TComponent {
return new TActiveRecordGateway($this);
}
-}
+ /**
+ * @return TActiveRecordInvalidFinderResult Defaults to '{@link TActiveRecordInvalidFinderResult::Null Null}'.
+ * @since 3.1.5
+ * @see setInvalidFinderResult
+ */
+ public function getInvalidFinderResult()
+ {
+ return $this->_invalidFinderResult;
+ }
+ /**
+ * Define the way an active record finder react if an invalid magic-finder invoked
+ * @param TActiveRecordInvalidFinderResult
+ * @since 3.1.5
+ * @see getInvalidFinderResult
+ */
+ public function setInvalidFinderResult($value)
+ {
+ $this->_invalidFinderResult = TPropertyValue::ensureEnum($value, 'TActiveRecordInvalidFinderResult');
+ }
+}
|