Development

/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/widget/sfWidgetFormDoctrineSelect.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/widget/sfWidgetFormDoctrineSelect.class.php

Revision 23810, 3.1 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
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  * sfWidgetFormDoctrineSelect represents a select HTML tag 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$
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 }
Note: See TracBrowser for help on using the browser.