Fix problem when 1 column identifier in propel is a string.

The ModelChoiceList thinks it can be used as the index of the ChoiceList, when it can only be used if it is an integer.
This commit is contained in:
Felix Labrecque 2012-11-29 10:30:50 -05:00
parent 50a62da114
commit 972e503fa8

View File

@ -45,6 +45,13 @@ class ModelChoiceList extends ObjectChoiceList
*/
private $loaded = false;
/**
* Whether to use the identifier for index generation
*
* @var Boolean
*/
private $identifierAsIndex = false;
/**
* @param string $class
* @param string $labelPath
@ -69,6 +76,13 @@ class ModelChoiceList extends ObjectChoiceList
$choices = array();
}
if (1 === count($this->identifier)) {
// TODO this should be current($this->identifier)->isInteger() when propel ColumnMap contains the isInteger function
if ($this->isInteger(current($this->identifier))) {
$this->identifierAsIndex = true;
}
}
parent::__construct($choices, $labelPath, array(), $groupPath);
}
@ -224,7 +238,7 @@ class ModelChoiceList extends ObjectChoiceList
// know that the IDs are used as indices
// Attention: This optimization does not check choices for existence
if (1 === count($this->identifier)) {
if ($this->identifierAsIndex) {
$indices = array();
foreach ($models as $model) {
@ -259,7 +273,7 @@ class ModelChoiceList extends ObjectChoiceList
// know that the IDs are used as indices and values
// Attention: This optimization does not check values for existence
if (1 === count($this->identifier)) {
if ($this->identifierAsIndex) {
return $this->fixIndices($values);
}
@ -283,7 +297,7 @@ class ModelChoiceList extends ObjectChoiceList
*/
protected function createIndex($model)
{
if (1 === count($this->identifier)) {
if ($this->identifierAsIndex) {
return current($this->getIdentifierValues($model));
}
@ -351,4 +365,15 @@ class ModelChoiceList extends ObjectChoiceList
return $model->getPrimaryKeys();
}
/**
* Whether this column in an integer
* TODO we could add this function to propel ColumnMap class instead
*
* @return boolean
*/
private function isInteger($col)
{
return $col->getType() === \PDO::PARAM_INT;
}
}