Added query option to ModelType

This commit is contained in:
William DURAND 2011-05-10 18:52:18 +02:00
parent 0fbe363de6
commit b71d71b963
2 changed files with 10 additions and 4 deletions

View file

@ -42,8 +42,12 @@ class ModelChoiceList extends ArrayChoiceList
* @var \Symfony\Component\Form\Util\PropertyPath
*/
private $propertyPath = null;
/**
* Query
*/
private $query = null;
public function __construct($class, $property = null, $choices = array())
public function __construct($class, $property = null, $choices = array(), $queryObject = null)
{
$this->class = $class;
@ -52,6 +56,7 @@ class ModelChoiceList extends ArrayChoiceList
$this->table = $query->getTableMap();
$this->identifier = $this->table->getPrimaryKeys();
$this->query = $queryObject ?: $query;
// The property option defines, which property (path) is used for
// displaying models as strings
@ -88,8 +93,7 @@ class ModelChoiceList extends ArrayChoiceList
if ($this->choices) {
$models = $this->choices;
} else {
$queryClass = $this->class . 'Query';
$models = $queryClass::create()->find();
$models = $this->query->find();
}
$this->choices = array();

View file

@ -30,6 +30,7 @@ class ModelType extends AbstractType
'expanded' => false,
'class' => null,
'property' => null,
'query' => null,
'choices' => array(),
'preferred_choices' => array(),
'multiple' => false,
@ -42,7 +43,8 @@ class ModelType extends AbstractType
$defaultOptions['choice_list'] = new ModelChoiceList(
$options['class'],
$options['property'],
$options['choices']
$options['choices'],
$options['query']
);
}