/
sfPropelRoute.class.php
126 lines (105 loc) · 3.21 KB
/
sfPropelRoute.class.php
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?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.
*/
/**
* sfPropelRoute represents a route that is bound to a Propel class.
*
* A Propel route can represent a single Propel object or a list of objects.
*
* @package symfony
* @subpackage routing
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
* @version SVN: $Id: sfPropelRoute.class.php 21924 2009-09-11 14:59:26Z fabien $
*/
class sfPropelRoute extends sfObjectRoute
{
protected
$criteria = null;
public function setListCriteria(Criteria $criteria)
{
if (!$this->isBound())
{
throw new LogicException('The route is not bound.');
}
$this->criteria = $criteria;
}
protected function getObjectForParameters($parameters)
{
$this->fixOptions();
if (!isset($this->options['method']))
{
$this->options['method'] = isset($this->options['method_for_criteria']) ? $this->options['method_for_criteria'] : 'doSelectOne';
$className = $this->options['model'];
$criteria = new Criteria();
$variables = $this->getRealVariables();
if (!count($variables))
{
return false;
}
foreach ($variables as $variable)
{
try
{
$constant = call_user_func(array($className, 'translateFieldName'), $variable, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_COLNAME);
$criteria->add($constant, $parameters[$variable]);
}
catch (Exception $e)
{
// don't add Criteria if the variable cannot be mapped to a column
}
}
$parameters = $criteria;
}
return parent::getObjectForParameters($parameters);
}
protected function getObjectsForParameters($parameters)
{
$this->fixOptions();
if (!isset($this->options['method']))
{
$this->options['method'] = isset($this->options['method_for_criteria']) ? $this->options['method_for_criteria'] : 'doSelect';
$parameters = new Criteria();
}
if (null !== $this->criteria)
{
$parameters = $this->criteria;
}
return parent::getObjectForParameters($parameters);
}
protected function doConvertObjectToArray($object)
{
$this->fixOptions();
if (isset($this->options['convert']) || method_exists($object, 'toParams'))
{
return parent::doConvertObjectToArray($object);
}
$className = $this->options['model'];
$parameters = array();
foreach ($this->getRealVariables() as $variable)
{
try
{
$method = 'get'.call_user_func(array($className, 'translateFieldName'), $variable, BasePeer::TYPE_FIELDNAME, BasePeer::TYPE_PHPNAME);
}
catch (Exception $e)
{
$method = 'get'.sfInflector::camelize($variable);
}
$parameters[$variable] = $object->$method();
}
return $parameters;
}
protected function fixOptions()
{
if (!isset($this->options['object_model']))
{
$this->options['object_model'] = $this->options['model'];
$this->options['model'] = constant($this->options['model'].'::PEER');
}
}
}