From 972e503fa83e24b8009a2bc0b89e48be49fe9e3a Mon Sep 17 00:00:00 2001 From: Felix Labrecque Date: Thu, 29 Nov 2012 10:30:50 -0500 Subject: [PATCH] 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. --- .../Form/ChoiceList/ModelChoiceList.php | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php b/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php index 54fe224d31a7..82d5e9d49d68 100644 --- a/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php +++ b/src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php @@ -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 @@ public function __construct($class, $labelPath = null, $choices = null, $queryOb $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 @@ public function getIndicesForChoices(array $models) // 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 @@ public function getIndicesForValues(array $values) // 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 @@ public function getIndicesForValues(array $values) */ protected function createIndex($model) { - if (1 === count($this->identifier)) { + if ($this->identifierAsIndex) { return current($this->getIdentifierValues($model)); } @@ -351,4 +365,15 @@ private function getIdentifierValues($model) 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; + } }