Development

/branches/1.2/lib/plugins/sfPropelPlugin/lib/widget/sfWidgetFormPropelSelect.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfPropelPlugin/lib/widget/sfWidgetFormPropelSelect.class.php

Revision 12803, 3.8 kB (checked in by fabien, 5 years ago)

[1.2] added key_method to sfWidgetFormPropelSelect and sfWidgetFormPropelChoice (closes #4863)

  • Property svn:mime-type set to text/x-php
  • 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  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * sfWidgetFormPropelSelect represents a select HTML tag for a model.
13  *
14  * @package    symfony
15  * @subpackage widget
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class sfWidgetFormPropelSelect extends sfWidgetFormSelect
20 {
21   /**
22    * @see sfWidget
23    */
24   public function __construct($options = array(), $attributes = array())
25   {
26     $options['choices'] = new sfCallable(array($this, 'getChoices'));
27
28     parent::__construct($options, $attributes);
29   }
30
31   /**
32    * Constructor.
33    *
34    * Available options:
35    *
36    *  * model:       The model class (required)
37    *  * add_empty:   Whether to add a first empty value or not (false by default)
38    *                 If the option is not a Boolean, the value will be used as the text value
39    *  * method:      The method to use to display object values (__toString by default)
40    *  * key_method:  The method to use to display the object keys (getPrimaryKey by default)
41    *  * order_by:    An array composed of two fields:
42    *                   * The column to order by the results (must be in the PhpName format)
43    *                   * asc or desc
44    *  * criteria:    A criteria to use when retrieving objects
45    *  * connection:  The Propel connection to use (null by default)
46    *  * multiple:    true if the select tag must allow multiple selections
47    *  * peer_method: The peer method to use to fetch objects
48    *
49    * @see sfWidgetFormSelect
50    */
51   protected function configure($options = array(), $attributes = array())
52   {
53     $this->addRequiredOption('model');
54     $this->addOption('add_empty', false);
55     $this->addOption('method', '__toString');
56     $this->addOption('key_method', 'getPrimaryKey');
57     $this->addOption('order_by', null);
58     $this->addOption('criteria', null);
59     $this->addOption('connection', null);
60     $this->addOption('multiple', false);
61     $this->addOption('peer_method', 'doSelect');
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     $class = constant($this->getOption('model').'::PEER');
80
81     $criteria = is_null($this->getOption('criteria')) ? new Criteria() : clone $this->getOption('criteria');
82     if ($order = $this->getOption('order_by'))
83     {
84       $method = sprintf('add%sOrderByColumn', 0 === strpos(strtoupper($order[1]), 'ASC') ? 'Ascending' : 'Descending');
85       $criteria->$method(call_user_func(array($class, 'translateFieldName'), $order[0], BasePeer::TYPE_PHPNAME, BasePeer::TYPE_COLNAME));
86     }
87     $objects = call_user_func(array($class, $this->getOption('peer_method')), $criteria, $this->getOption('connection'));
88
89     $methodKey = $this->getOption('key_method');
90     if (!method_exists($this->getOption('model'), $methodKey))
91     {
92       throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $methodKey, __CLASS__));
93     }
94
95     $methodValue = $this->getOption('method');
96     if (!method_exists($this->getOption('model'), $methodValue))
97     {
98       throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $methodValue, __CLASS__));
99     }
100
101     foreach ($objects as $object)
102     {
103       $choices[$object->$methodKey()] = $object->$methodValue();
104     }
105
106     return $choices;
107   }
108 }
109
Note: See TracBrowser for help on using the browser.