| 1 |
lars |
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: sfPropelRoute.class.php 21924 2009-09-11 14:59:26Z fabien $
|
|
|
20 |
*/
|
|
|
21 |
class sfPropelRoute extends sfObjectRoute
|
|
|
22 |
{
|
|
|
23 |
protected
|
|
|
24 |
$criteria = null;
|
|
|
25 |
|
|
|
26 |
public function setListCriteria(Criteria $criteria)
|
|
|
27 |
{
|
|
|
28 |
if (!$this->isBound())
|
|
|
29 |
{
|
|
|
30 |
throw new LogicException('The route is not bound.');
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
$this->criteria = $criteria;
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
protected function getObjectForParameters($parameters)
|
|
|
37 |
{
|
|
|
38 |
$this->fixOptions();
|
|
|
39 |
|
|
|
40 |
if (!isset($this->options['method']))
|
|
|
41 |
{
|
|
|
42 |
$this->options['method'] = isset($this->options['method_for_criteria']) ? $this->options['method_for_criteria'] : 'doSelectOne';
|
|
|
43 |
|
|
|
44 |
$className = $this->options['model'];
|
|
|
45 |
$criteria = new Criteria();
|
|
|
46 |
$variables = $this->getRealVariables();
|
|
|
47 |
if (!count($variables))
|
|
|
48 |
{
|
|
|
49 |
return false;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
foreach ($variables as $variable)
|
|
|
53 |
{
|
|
|
54 |
try
|
|
|
55 |
{
|
|
|
56 |
$constant = call_user_func(array($className, 'translateFieldName'), $variable, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
|
|
|
57 |
$criteria->add($constant, $parameters[$variable]);
|
|
|
58 |
}
|
|
|
59 |
catch (Exception $e)
|
|
|
60 |
{
|
|
|
61 |
// don't add Criteria if the variable cannot be mapped to a column
|
|
|
62 |
}
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
$parameters = $criteria;
|
|
|
66 |
}
|
|
|
67 |
|
|
|
68 |
return parent::getObjectForParameters($parameters);
|
|
|
69 |
}
|
|
|
70 |
|
|
|
71 |
protected function getObjectsForParameters($parameters)
|
|
|
72 |
{
|
|
|
73 |
$this->fixOptions();
|
|
|
74 |
|
|
|
75 |
if (!isset($this->options['method']))
|
|
|
76 |
{
|
|
|
77 |
$this->options['method'] = isset($this->options['method_for_criteria']) ? $this->options['method_for_criteria'] : 'doSelect';
|
|
|
78 |
$parameters = new Criteria();
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
if (null !== $this->criteria)
|
|
|
82 |
{
|
|
|
83 |
$parameters = $this->criteria;
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
return parent::getObjectForParameters($parameters);
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
protected function doConvertObjectToArray($object)
|
|
|
90 |
{
|
|
|
91 |
$this->fixOptions();
|
|
|
92 |
|
|
|
93 |
if (isset($this->options['convert']) || method_exists($object, 'toParams'))
|
|
|
94 |
{
|
|
|
95 |
return parent::doConvertObjectToArray($object);
|
|
|
96 |
}
|
|
|
97 |
|
|
|
98 |
$className = $this->options['model'];
|
|
|
99 |
|
|
|
100 |
$parameters = array();
|
|
|
101 |
foreach ($this->getRealVariables() as $variable)
|
|
|
102 |
{
|
|
|
103 |
try
|
|
|
104 |
{
|
|
|
105 |
$method = 'get'.call_user_func(array($className, 'translateFieldName'), $variable, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
|
|
|
106 |
}
|
|
|
107 |
catch (Exception $e)
|
|
|
108 |
{
|
|
|
109 |
$method = 'get'.sfInflector::camelize($variable);
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
$parameters[$variable] = $object->$method();
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
return $parameters;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
protected function fixOptions()
|
|
|
119 |
{
|
|
|
120 |
if (!isset($this->options['object_model']))
|
|
|
121 |
{
|
|
|
122 |
$this->options['object_model'] = $this->options['model'];
|
|
|
123 |
$this->options['model'] = constant($this->options['model'].'::PEER');
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
}
|