Skip to content

Commit

Permalink
Fix problem when 1 column identifier in propel is a string.
Browse files Browse the repository at this point in the history
The ModelChoiceList thinks it can be used as the index of the ChoiceList, when it can only be used if it is an integer.
  • Loading branch information
Felix Labrecque committed Nov 29, 2012
1 parent 50a62da commit 972e503
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/Symfony/Bridge/Propel1/Form/ChoiceList/ModelChoiceList.php
Expand Up @@ -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
Expand All @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}

Expand All @@ -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));
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit 972e503

Please sign in to comment.