| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* This file is part of the symfony package.
|
|
|
5 |
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
6 |
* (c) Jonathan H. Wage <jonwage@gmail.com>
|
|
|
7 |
*
|
|
|
8 |
* For the full copyright and license information, please view the LICENSE
|
|
|
9 |
* file that was distributed with this source code.
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
/**
|
|
|
13 |
* sfWidgetFormDoctrineChoice represents a choice widget for a model.
|
|
|
14 |
*
|
|
|
15 |
* @package symfony
|
|
|
16 |
* @subpackage doctrine
|
|
|
17 |
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
18 |
* @author Jonathan H. Wage <jonwage@gmail.com>
|
|
|
19 |
* @version SVN: $Id: sfWidgetFormDoctrineChoice.class.php 29679 2010-05-30 14:46:03Z Kris.Wallsmith $
|
|
|
20 |
*/
|
|
|
21 |
class sfWidgetFormDoctrineChoice extends sfWidgetFormChoice
|
|
|
22 |
{
|
|
|
23 |
/**
|
|
|
24 |
* @see sfWidget
|
|
|
25 |
*/
|
|
|
26 |
public function __construct($options = array(), $attributes = array())
|
|
|
27 |
{
|
|
|
28 |
$options['choices'] = array();
|
|
|
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 |
* * multiple: true if the select tag must allow multiple selections
|
|
|
48 |
* * table_method: A method to return either a query, collection or single object
|
|
|
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('key_method', 'getPrimaryKey');
|
|
|
58 |
$this->addOption('order_by', null);
|
|
|
59 |
$this->addOption('query', null);
|
|
|
60 |
$this->addOption('multiple', false);
|
|
|
61 |
$this->addOption('table_method', null);
|
|
|
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->translate($this->getOption('add_empty'));
|
|
|
77 |
}
|
|
|
78 |
|
|
|
79 |
if (null === $this->getOption('table_method'))
|
|
|
80 |
{
|
|
|
81 |
$query = null === $this->getOption('query') ? Doctrine_Core::getTable($this->getOption('model'))->createQuery() : $this->getOption('query');
|
|
|
82 |
if ($order = $this->getOption('order_by'))
|
|
|
83 |
{
|
|
|
84 |
$query->addOrderBy($order[0] . ' ' . $order[1]);
|
|
|
85 |
}
|
|
|
86 |
$objects = $query->execute();
|
|
|
87 |
}
|
|
|
88 |
else
|
|
|
89 |
{
|
|
|
90 |
$tableMethod = $this->getOption('table_method');
|
|
|
91 |
$results = Doctrine_Core::getTable($this->getOption('model'))->$tableMethod();
|
|
|
92 |
|
|
|
93 |
if ($results instanceof Doctrine_Query)
|
|
|
94 |
{
|
|
|
95 |
$objects = $results->execute();
|
|
|
96 |
}
|
|
|
97 |
else if ($results instanceof Doctrine_Collection)
|
|
|
98 |
{
|
|
|
99 |
$objects = $results;
|
|
|
100 |
}
|
|
|
101 |
else if ($results instanceof Doctrine_Record)
|
|
|
102 |
{
|
|
|
103 |
$objects = new Doctrine_Collection($this->getOption('model'));
|
|
|
104 |
$objects[] = $results;
|
|
|
105 |
}
|
|
|
106 |
else
|
|
|
107 |
{
|
|
|
108 |
$objects = array();
|
|
|
109 |
}
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
$method = $this->getOption('method');
|
|
|
113 |
$keyMethod = $this->getOption('key_method');
|
|
|
114 |
|
|
|
115 |
foreach ($objects as $object)
|
|
|
116 |
{
|
|
|
117 |
$choices[$object->$keyMethod()] = $object->$method();
|
|
|
118 |
}
|
|
|
119 |
|
|
|
120 |
return $choices;
|
|
|
121 |
}
|
|
|
122 |
}
|