Development

/branches/1.2/lib/plugins/sfPropelPlugin/lib/routing/sfPropelRoute.class.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfPropelPlugin/lib/routing/sfPropelRoute.class.php

Revision 13027, 3.5 kB (checked in by fabien, 5 years ago)

[1.2] added a method_for_criteria option

  • 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  * sfPropelRoute represents a route that is bound to a Propel class.
13  *
14  * A Propel route can represent a single Propel object or a list of objects.
15  *
16  * @package    symfony
17  * @subpackage routing
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21 class sfPropelRoute extends sfObjectRoute
22 {
23   protected
24     $criteria = null;
25
26   /**
27    * Constructor.
28    *
29    * @param string $pattern       The pattern to match
30    * @param array  $defaults      An array of default parameter values
31    * @param array  $requirements  An array of requirements for parameters (regexes)
32    * @param array  $options       An array of options
33    *
34    * @see sfObjectRoute
35    */
36   public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array())
37   {
38     parent::__construct($pattern, $defaults, $requirements, $options);
39
40     $this->options['object_model'] = $this->options['model'];
41     $this->options['model'] = constant($this->options['model'].'::PEER');
42   }
43
44   public function setListCriteria(Criteria $criteria)
45   {
46     if (!$this->isBound())
47     {
48       throw new LogicException('The route is not bound.');
49     }
50
51     $this->criteria = $criteria;
52   }
53
54   protected function getObjectForParameters($parameters)
55   {
56     if (!isset($this->options['method']))
57     {
58       $this->options['method'] = isset($this->options['method_for_criteria']) ? $this->options['method_for_criteria'] : 'doSelectOne';
59
60       $className = $this->options['model'];
61       $criteria = new Criteria();
62       $variables = $this->getRealVariables();
63       if (!count($variables))
64       {
65         return false;
66       }
67
68       foreach ($variables as $variable)
69       {
70         try
71         {
72           $constant = call_user_func(array($className, 'translateFieldName'), $variable, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
73           $criteria->add($constant, $parameters[$variable]);
74         }
75         catch (Exception $e)
76         {
77           // don't add Criteria if the variable cannot be mapped to a column
78         }
79       }
80
81       $parameters = $criteria;
82     }
83
84     return parent::getObjectForParameters($parameters);
85   }
86
87   protected function getObjectsForParameters($parameters)
88   {
89     if (!isset($this->options['method']))
90     {
91       $this->options['method'] = isset($this->options['method_for_criteria']) ? $this->options['method_for_criteria'] : 'doSelect';
92       $parameters = new Criteria();
93     }
94
95     if (!is_null($this->criteria))
96     {
97       $parameters = $this->criteria;
98     }
99
100     return parent::getObjectForParameters($parameters);
101   }
102
103   protected function doConvertObjectToArray($object)
104   {
105     if (isset($this->options['convert']) || method_exists($object, 'toParams'))
106     {
107       return parent::doConvertObjectToArray($object);
108     }
109
110     $className = $this->options['model'];
111
112     $parameters = array();
113     foreach ($this->getRealVariables() as $variable)
114     {
115       try
116       {
117         $method = 'get'.call_user_func(array($className, 'translateFieldName'), $variable, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
118       }
119       catch (Exception $e)
120       {
121         $method = 'get'.sfInflector::camelize($variable);
122       }
123
124       $parameters[$variable] = $object->$method();
125     }
126
127     return $parameters;
128   }
129 }
130
Note: See TracBrowser for help on using the browser.