From b71d71b9639471a4d637fd4dc1fdb5e3aa819612 Mon Sep 17 00:00:00 2001 From: William DURAND Date: Tue, 10 May 2011 18:52:18 +0200 Subject: [PATCH] Added query option to ModelType --- Form/ChoiceList/ModelChoiceList.php | 10 +++++++--- Form/Type/ModelType.php | 4 +++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Form/ChoiceList/ModelChoiceList.php b/Form/ChoiceList/ModelChoiceList.php index 359454a..32ab932 100644 --- a/Form/ChoiceList/ModelChoiceList.php +++ b/Form/ChoiceList/ModelChoiceList.php @@ -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(); diff --git a/Form/Type/ModelType.php b/Form/Type/ModelType.php index d7a127d..f279562 100644 --- a/Form/Type/ModelType.php +++ b/Form/Type/ModelType.php @@ -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'] ); }