Development

/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/widget/sfWidgetFormPropelSelect.class.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/widget/sfWidgetFormPropelSelect.class.php

Revision 11616, 3.2 kB (checked in by nicolas, 6 years ago)

[1.1] fixed Criteria alteration problem in sfValidatorPropelChoice and sfValidatorPropelChoiceMany (closes #4397)

  • 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    *  * order_by:   An array composed of two fields:
41    *                  * The column to order by the results (must be in the PhpName format)
42    *                  * asc or desc
43    *  * criteria:   A criteria to use when retrieving objects
44    *  * connection: The Propel connection to use (null by default)
45    *  * multiple:   true if the select tag must allow multiple selections
46    *
47    * @see sfWidgetFormSelect
48    */
49   protected function configure($options = array(), $attributes = array())
50   {
51     $this->addRequiredOption('model');
52     $this->addOption('add_empty', false);
53     $this->addOption('method', '__toString');
54     $this->addOption('order_by', null);
55     $this->addOption('criteria', null);
56     $this->addOption('connection', null);
57     $this->addOption('multiple', false);
58
59     parent::configure($options, $attributes);
60   }
61
62   /**
63    * Returns the choices associated to the model.
64    *
65    * @return array An array of choices
66    */
67   public function getChoices()
68   {
69     $choices = array();
70     if (false !== $this->getOption('add_empty'))
71     {
72       $choices[''] = true === $this->getOption('add_empty') ? '' : $this->getOption('add_empty');
73     }
74
75     $class = $this->getOption('model').'Peer';
76
77     $criteria = is_null($this->getOption('criteria')) ? new Criteria() : clone $this->getOption('criteria');
78     if ($order = $this->getOption('order_by'))
79     {
80       $method = sprintf('add%sOrderByColumn', 0 === strpos(strtoupper($order[1]), 'ASC') ? 'Ascending' : 'Descending');
81       $criteria->$method(call_user_func(array($class, 'translateFieldName'), $order[0], BasePeer::TYPE_PHPNAME, BasePeer::TYPE_COLNAME));
82     }
83     $objects = call_user_func(array($class, 'doSelect'), $criteria, $this->getOption('connection'));
84
85     $method = $this->getOption('method');
86
87     if (!method_exists($this->getOption('model'), $method))
88     {
89       throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $method, __CLASS__));
90     }
91
92     foreach ($objects as $object)
93     {
94       $choices[$object->getPrimaryKey()] = $object->$method();
95     }
96
97     return $choices;
98   }
99 }
100
Note: See TracBrowser for help on using the browser.