1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
<?php
/*
* This file is part of the symfony package.
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfWidgetFormPropelJQueryAutocompleter represents an autocompleter input widget rendered by JQuery
* optimized for foreign key lookup.
*
* @package symfony
* @subpackage widget
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfWidgetFormPropelJQueryAutocompleter.class.php 12130 2008-10-10 14:51:07Z fabien $
*/
class sfWidgetFormPropelJQueryAutocompleter extends sfWidgetFormJQueryAutocompleter
{
/**
* @see sfWidget
*/
public function __construct($options = array(), $attributes = array())
{
$options['value_callback'] = array($this, 'toString');
parent::__construct($options, $attributes);
}
/**
* Configures the current widget.
*
* @param array $options An array of options
* @param array $attributes An array of default HTML attributes
*
* @see sfWidgetFormJQueryAutocompleter
*/
protected function configure($options = array(), $attributes = array())
{
$this->addRequiredOption('model');
$this->addOption('method', '__toString');
parent::configure($options, $attributes);
}
/**
* Returns the text representation of a foreign key.
*
* @param string $value The primary key
*/
protected function toString($value)
{
$class = constant($this->getOption('model').'::PEER');
$criteria = new Criteria();
$object = call_user_func(array($class, 'retrieveByPK'), $value);
$method = $this->getOption('method');
if (!method_exists($this->getOption('model'), $method))
{
throw new RuntimeException(sprintf('Class "%s" must implement a "%s" method to be rendered in a "%s" widget', $this->getOption('model'), $method, __CLASS__));
}
return !is_null($object) ? $object->$method() : '';
}
}
|