| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfWidgetFormDoctrineSelect extends sfWidgetFormSelect |
|---|
| 22 |
{ |
|---|
| 23 |
|
|---|
| 24 |
* @see sfWidget |
|---|
| 25 |
*/ |
|---|
| 26 |
public function __construct($options = array(), $attributes = array()) |
|---|
| 27 |
{ |
|---|
| 28 |
$options['choices'] = new sfCallable(array($this, 'getChoices')); |
|---|
| 29 |
|
|---|
| 30 |
parent::__construct($options, $attributes); |
|---|
| 31 |
} |
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 |
* Constructor. |
|---|
| 35 |
* |
|---|
| 36 |
* Available options: |
|---|
| 37 |
* |
|---|
| 38 |
* * model: The model class (required) |
|---|
| 39 |
* * add_empty: Whether to add a first empty value or not (false by default) |
|---|
| 40 |
* If the option is not a Boolean, the value will be used as the text value |
|---|
| 41 |
* * method: The method to use to display object values (__toString by default) |
|---|
| 42 |
* * order_by: An array composed of two fields: |
|---|
| 43 |
* * The column to order by the results (must be in the PhpName format) |
|---|
| 44 |
* * asc or desc |
|---|
| 45 |
* * alias: The alias for the main component involved in the query |
|---|
| 46 |
* * query: A query to use when retrieving objects |
|---|
| 47 |
* * connection: The Doctrine connection to use (null by default) |
|---|
| 48 |
* * multiple: true if the select tag must allow multiple selections |
|---|
| 49 |
* |
|---|
| 50 |
* @see sfWidgetFormSelect |
|---|
| 51 |
*/ |
|---|
| 52 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 53 |
{ |
|---|
| 54 |
$this->addRequiredOption('model'); |
|---|
| 55 |
$this->addOption('add_empty', false); |
|---|
| 56 |
$this->addOption('method', '__toString'); |
|---|
| 57 |
$this->addOption('order_by', null); |
|---|
| 58 |
$this->addOption('alias', 'a'); |
|---|
| 59 |
$this->addOption('query', null); |
|---|
| 60 |
$this->addOption('connection', null); |
|---|
| 61 |
$this->addOption('multiple', false); |
|---|
| 62 |
|
|---|
| 63 |
parent::configure($options, $attributes); |
|---|
| 64 |
} |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
* Returns the choices associated to the model. |
|---|
| 68 |
* |
|---|
| 69 |
* @return array An array of choices |
|---|
| 70 |
*/ |
|---|
| 71 |
public function getChoices() |
|---|
| 72 |
{ |
|---|
| 73 |
$choices = array(); |
|---|
| 74 |
if (false !== $this->getOption('add_empty')) |
|---|
| 75 |
{ |
|---|
| 76 |
$choices[''] = true === $this->getOption('add_empty') ? '' : $this->getOption('add_empty'); |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
$a = $this->getOption('alias'); |
|---|
| 80 |
$q = null === $this->getOption('query') |
|---|
| 81 |
? Doctrine_Core::getTable($this->getOption('model'))->createQuery($a) |
|---|
| 82 |
: $this->getOption('query'); |
|---|
| 83 |
|
|---|
| 84 |
if ($order = $this->getOption('order_by')) |
|---|
| 85 |
{ |
|---|
| 86 |
$q->orderBy("$a." . $order[0] . ' ' . $order[1]); |
|---|
| 87 |
} |
|---|
| 88 |
|
|---|
| 89 |
$objects = $q->execute(); |
|---|
| 90 |
$method = $this->getOption('method'); |
|---|
| 91 |
foreach ($objects as $object) |
|---|
| 92 |
{ |
|---|
| 93 |
$choices[is_array($value = $object->getPrimaryKey()) ? current($value) : $value] = $object->$method(); |
|---|
| 94 |
} |
|---|
| 95 |
|
|---|
| 96 |
return $choices; |
|---|
| 97 |
} |
|---|
| 98 |
|
|---|
| 99 |
public function __clone() |
|---|
| 100 |
{ |
|---|
| 101 |
$this->setOption('choices', new sfCallable(array($this, 'getChoices'))); |
|---|
| 102 |
} |
|---|
| 103 |
} |
|---|