| 1 |
<?php |
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
class sfWidgetFormDoctrineChoice extends sfWidgetFormChoice |
|---|
| 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 |
* * key_method: The method to use to display the object keys (getPrimaryKey by default) |
|---|
| 43 |
* * order_by: An array composed of two fields: |
|---|
| 44 |
* * The column to order by the results (must be in the PhpName format) |
|---|
| 45 |
* * asc or desc |
|---|
| 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 |
* * table_method: The method to use to display the object keys (getPrimaryKey by default) |
|---|
| 50 |
* |
|---|
| 51 |
* @see sfWidgetFormSelect |
|---|
| 52 |
*/ |
|---|
| 53 |
protected function configure($options = array(), $attributes = array()) |
|---|
| 54 |
{ |
|---|
| 55 |
$this->addRequiredOption('model'); |
|---|
| 56 |
$this->addOption('add_empty', false); |
|---|
| 57 |
$this->addOption('method', '__toString'); |
|---|
| 58 |
$this->addOption('key_method', 'getPrimaryKey'); |
|---|
| 59 |
$this->addOption('order_by', null); |
|---|
| 60 |
$this->addOption('query', null); |
|---|
| 61 |
$this->addOption('connection', null); |
|---|
| 62 |
$this->addOption('multiple', false); |
|---|
| 63 |
$this->addOption('table_method', null); |
|---|
| 64 |
|
|---|
| 65 |
parent::configure($options, $attributes); |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
* Returns the choices associated to the model. |
|---|
| 70 |
* |
|---|
| 71 |
* @return array An array of choices |
|---|
| 72 |
*/ |
|---|
| 73 |
public function getChoices() |
|---|
| 74 |
{ |
|---|
| 75 |
$choices = array(); |
|---|
| 76 |
if (false !== $this->getOption('add_empty')) |
|---|
| 77 |
{ |
|---|
| 78 |
$choices[''] = true === $this->getOption('add_empty') ? '' : $this->getOption('add_empty'); |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
if (is_null($this->getOption('table_method'))) |
|---|
| 82 |
{ |
|---|
| 83 |
$query = is_null($this->getOption('query')) ? Doctrine::getTable($this->getOption('model'))->createQuery() : $this->getOption('query'); |
|---|
| 84 |
if ($order = $this->getOption('order_by')) |
|---|
| 85 |
{ |
|---|
| 86 |
$query->addOrderBy($order[0] . ' ' . $order[1]); |
|---|
| 87 |
} |
|---|
| 88 |
$objects = $query->execute(); |
|---|
| 89 |
} else { |
|---|
| 90 |
$tableMethod = $this->getOption('table_method'); |
|---|
| 91 |
$results = Doctrine::getTable($this->getOption('model'))->$tableMethod(); |
|---|
| 92 |
if ($results instanceof Doctrine_Query) |
|---|
| 93 |
{ |
|---|
| 94 |
$objects = $results->execute(); |
|---|
| 95 |
} else if ($results instanceof Doctrine_Collection) { |
|---|
| 96 |
$objects = $results; |
|---|
| 97 |
} else if ($results instanceof Doctrine_Record) { |
|---|
| 98 |
$objects = new Doctrine_Collection($this->getOption('model')); |
|---|
| 99 |
$objects[] = $results; |
|---|
| 100 |
} else { |
|---|
| 101 |
$objects = array(); |
|---|
| 102 |
} |
|---|
| 103 |
} |
|---|
| 104 |
|
|---|
| 105 |
$method = $this->getOption('method'); |
|---|
| 106 |
$keyMethod = $this->getOption('key_method'); |
|---|
| 107 |
|
|---|
| 108 |
foreach ($objects as $object) |
|---|
| 109 |
{ |
|---|
| 110 |
try |
|---|
| 111 |
{ |
|---|
| 112 |
$choices[$object->$keyMethod()] = $object->$method(); |
|---|
| 113 |
} catch (Exception $e) { |
|---|
| 114 |
throw $e; |
|---|
| 115 |
} |
|---|
| 116 |
} |
|---|
| 117 |
|
|---|
| 118 |
return $choices; |
|---|
| 119 |
} |
|---|
| 120 |
} |
|---|