Development

/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/widget/sfWidgetFormDoctrineChoice.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfDoctrinePlugin/lib/widget/sfWidgetFormDoctrineChoice.class.php

Revision 19982, 3.9 kB (checked in by Jonathan.Wage, 5 years ago)

[1.2, 1.3] sfDoctrinePlugin: adding missing phpdoc blocks to sfWidgetFormDoctrineChoice::configure() (closes #6637)

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  * 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 11540 2008-09-14 15:23:55Z fabien $
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 }
Note: See TracBrowser for help on using the browser.