1 | <?php |
---|
2 | |
---|
3 | require_once 'propel/engine/builder/om/php5/PHP5PeerBuilder.php'; |
---|
4 | |
---|
5 | /* |
---|
6 | * This file is part of the symfony package. |
---|
7 | * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com> |
---|
8 | * |
---|
9 | * For the full copyright and license information, please view the LICENSE |
---|
10 | * file that was distributed with this source code. |
---|
11 | */ |
---|
12 | |
---|
13 | /** |
---|
14 | * @package symfony |
---|
15 | * @subpackage addon |
---|
16 | * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
---|
17 | * @version SVN: $Id: SfPeerBuilder.php 3663 2007-03-23 13:43:19Z fabien $ |
---|
18 | */ |
---|
19 | class SfPeerBuilder extends PHP5PeerBuilder |
---|
20 | { |
---|
21 | public function build() |
---|
22 | { |
---|
23 | if (!DataModelBuilder::getBuildProperty('builderAddComments')) |
---|
24 | { |
---|
25 | return sfToolkit::stripComments(parent::build()); |
---|
26 | } |
---|
27 | |
---|
28 | return parent::build(); |
---|
29 | } |
---|
30 | |
---|
31 | protected function addIncludes(&$script) |
---|
32 | { |
---|
33 | if (!DataModelBuilder::getBuildProperty('builderAddIncludes')) |
---|
34 | { |
---|
35 | return; |
---|
36 | } |
---|
37 | |
---|
38 | parent::addIncludes($script); |
---|
39 | } |
---|
40 | |
---|
41 | protected function addSelectMethods(&$script) |
---|
42 | { |
---|
43 | parent::addSelectMethods($script); |
---|
44 | |
---|
45 | if ($this->getTable()->getAttribute('isI18N')) |
---|
46 | { |
---|
47 | $this->addDoSelectWithI18n($script); |
---|
48 | } |
---|
49 | } |
---|
50 | |
---|
51 | protected function addDoSelectWithI18n(&$script) |
---|
52 | { |
---|
53 | $table = $this->getTable(); |
---|
54 | $thisTableObjectBuilder = OMBuilder::getNewObjectBuilder($table); |
---|
55 | $className = $table->getPhpName(); |
---|
56 | $pks = $table->getPrimaryKey(); |
---|
57 | $pk = PeerBuilder::getColumnName($pks[0], $className); |
---|
58 | |
---|
59 | // get i18n table name and culture column name |
---|
60 | foreach ($table->getReferrers() as $fk) |
---|
61 | { |
---|
62 | $tblFK = $fk->getTable(); |
---|
63 | if ($tblFK->getName() == $table->getAttribute('i18nTable')) |
---|
64 | { |
---|
65 | $i18nClassName = $tblFK->getPhpName(); |
---|
66 | // FIXME |
---|
67 | $i18nPeerClassName = $i18nClassName.'Peer'; |
---|
68 | |
---|
69 | $i18nTable = $table->getDatabase()->getTable($tblFK->getName()); |
---|
70 | $i18nTableObjectBuilder = OMBuilder::getNewObjectBuilder($i18nTable); |
---|
71 | $i18nTablePeerBuilder = OMBuilder::getNewPeerBuilder($i18nTable); |
---|
72 | $i18nPks = $i18nTable->getPrimaryKey(); |
---|
73 | $i18nPk = PeerBuilder::getColumnName($i18nPks[0], $i18nClassName); |
---|
74 | |
---|
75 | $culturePhpName = ''; |
---|
76 | $cultureColumnName = ''; |
---|
77 | foreach ($tblFK->getColumns() as $col) |
---|
78 | { |
---|
79 | if (("true" === strtolower($col->getAttribute('isCulture')))) |
---|
80 | { |
---|
81 | $culturePhpName = $col->getPhpName(); |
---|
82 | $cultureColumnName = PeerBuilder::getColumnName($col, $i18nClassName); |
---|
83 | } |
---|
84 | } |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | $script .= " |
---|
89 | |
---|
90 | /** |
---|
91 | * Selects a collection of $className objects pre-filled with their i18n objects. |
---|
92 | * |
---|
93 | * @return array Array of $className objects. |
---|
94 | * @throws PropelException Any exceptions caught during processing will be |
---|
95 | * rethrown wrapped into a PropelException. |
---|
96 | */ |
---|
97 | public static function doSelectWithI18n(Criteria \$c, \$culture = null, \$con = null) |
---|
98 | { |
---|
99 | if (\$culture === null) |
---|
100 | { |
---|
101 | \$culture = sfContext::getInstance()->getUser()->getCulture(); |
---|
102 | } |
---|
103 | |
---|
104 | // Set the correct dbName if it has not been overridden |
---|
105 | if (\$c->getDbName() == Propel::getDefaultDB()) |
---|
106 | { |
---|
107 | \$c->setDbName(self::DATABASE_NAME); |
---|
108 | } |
---|
109 | |
---|
110 | ".$this->getPeerClassname()."::addSelectColumns(\$c); |
---|
111 | \$startcol = (".$this->getPeerClassname()."::NUM_COLUMNS - ".$this->getPeerClassname()."::NUM_LAZY_LOAD_COLUMNS) + 1; |
---|
112 | |
---|
113 | ".$i18nPeerClassName."::addSelectColumns(\$c); |
---|
114 | |
---|
115 | \$c->addJoin(".$pk.", ".$i18nPk."); |
---|
116 | \$c->add(".$cultureColumnName.", \$culture); |
---|
117 | |
---|
118 | \$rs = ".$this->basePeerClassname."::doSelect(\$c, \$con); |
---|
119 | \$results = array(); |
---|
120 | |
---|
121 | while(\$rs->next()) { |
---|
122 | "; |
---|
123 | if ($table->getChildrenColumn()) { |
---|
124 | $script .= " |
---|
125 | \$omClass = ".$this->getPeerClassname()."::getOMClass(\$rs, 1); |
---|
126 | "; |
---|
127 | } else { |
---|
128 | $script .= " |
---|
129 | \$omClass = ".$this->getPeerClassname()."::getOMClass(); |
---|
130 | "; |
---|
131 | } |
---|
132 | $script .= " |
---|
133 | \$cls = Propel::import(\$omClass); |
---|
134 | \$obj1 = new \$cls(); |
---|
135 | \$obj1->hydrate(\$rs); |
---|
136 | \$obj1->setCulture(\$culture); |
---|
137 | "; |
---|
138 | // if ($i18nTable->getChildrenColumn()) { |
---|
139 | $script .= " |
---|
140 | \$omClass = ".$i18nTablePeerBuilder->getPeerClassname()."::getOMClass(\$rs, \$startcol); |
---|
141 | "; |
---|
142 | // } else { |
---|
143 | // $script .= " |
---|
144 | // \$omClass = ".$i18nTablePeerBuilder->getPeerClassname()."::getOMClass(); |
---|
145 | //"; |
---|
146 | // } |
---|
147 | |
---|
148 | $script .= " |
---|
149 | \$cls = Propel::import(\$omClass); |
---|
150 | \$obj2 = new \$cls(); |
---|
151 | \$obj2->hydrate(\$rs, \$startcol); |
---|
152 | |
---|
153 | \$obj1->set".$i18nClassName."ForCulture(\$obj2, \$culture); |
---|
154 | \$obj2->set".$className."(\$obj1); |
---|
155 | |
---|
156 | \$results[] = \$obj1; |
---|
157 | } |
---|
158 | return \$results; |
---|
159 | } |
---|
160 | "; |
---|
161 | } |
---|
162 | |
---|
163 | protected function addDoValidate(&$script) |
---|
164 | { |
---|
165 | $tmp = ''; |
---|
166 | parent::addDoValidate($tmp); |
---|
167 | |
---|
168 | $script .= str_replace("return {$this->basePeerClassname}::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns);\n", |
---|
169 | "\$res = {$this->basePeerClassname}::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns);\n". |
---|
170 | " if (\$res !== true) {\n". |
---|
171 | " \$request = sfContext::getInstance()->getRequest();\n". |
---|
172 | " foreach (\$res as \$failed) {\n". |
---|
173 | " \$col = ".$this->getPeerClassname()."::translateFieldname(\$failed->getColumn(), BasePeer::TYPE_COLNAME, BasePeer::TYPE_PHPNAME);\n". |
---|
174 | " \$request->setError(\$col, \$failed->getMessage());\n". |
---|
175 | " }\n". |
---|
176 | " }\n\n". |
---|
177 | " return \$res;\n", $tmp); |
---|
178 | } |
---|
179 | |
---|
180 | protected function addDoSelectStmt(&$script) |
---|
181 | { |
---|
182 | $tmp = ''; |
---|
183 | parent::addDoSelectStmt($tmp); |
---|
184 | |
---|
185 | if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
---|
186 | { |
---|
187 | $mixer_script = " |
---|
188 | |
---|
189 | foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectStmt:doSelectStmt') as \$callable) |
---|
190 | { |
---|
191 | call_user_func(\$callable, '{$this->getClassname()}', \$criteria, \$con); |
---|
192 | } |
---|
193 | |
---|
194 | "; |
---|
195 | $tmp = preg_replace('/{/', '{'.$mixer_script, $tmp, 1); |
---|
196 | } |
---|
197 | |
---|
198 | $script .= $tmp; |
---|
199 | } |
---|
200 | |
---|
201 | protected function addDoCountJoin(&$script) |
---|
202 | { |
---|
203 | $tmp = ''; |
---|
204 | parent::addDoCountJoin($tmp); |
---|
205 | |
---|
206 | if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
---|
207 | { |
---|
208 | $mixer_script = " |
---|
209 | |
---|
210 | foreach (sfMixer::getCallables('{$this->getClassname()}:doCountJoin:doCountJoin') as \$callable) |
---|
211 | { |
---|
212 | call_user_func(\$callable, '{$this->getClassname()}', \$criteria, \$con); |
---|
213 | } |
---|
214 | |
---|
215 | "; |
---|
216 | $tmp = preg_replace('/public static function doCountJoin.*\(Criteria \$criteria, \$distinct = false, PropelPDO \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp); |
---|
217 | } |
---|
218 | |
---|
219 | $script .= $tmp; |
---|
220 | } |
---|
221 | |
---|
222 | protected function addDoSelectJoin(&$script) |
---|
223 | { |
---|
224 | $tmp = ''; |
---|
225 | parent::addDoSelectJoin($tmp); |
---|
226 | |
---|
227 | if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
---|
228 | { |
---|
229 | $mixer_script = " |
---|
230 | |
---|
231 | foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoin:doSelectJoin') as \$callable) |
---|
232 | { |
---|
233 | call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con); |
---|
234 | } |
---|
235 | |
---|
236 | "; |
---|
237 | $tmp = preg_replace('/public static function doSelectJoin.*\(Criteria \$c, \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp); |
---|
238 | } |
---|
239 | |
---|
240 | $script .= $tmp; |
---|
241 | } |
---|
242 | |
---|
243 | protected function addDoSelectJoinAll(&$script) |
---|
244 | { |
---|
245 | $tmp = ''; |
---|
246 | parent::addDoSelectJoinAll($tmp); |
---|
247 | |
---|
248 | if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
---|
249 | { |
---|
250 | $mixer_script = " |
---|
251 | |
---|
252 | foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoinAll:doSelectJoinAll') as \$callable) |
---|
253 | { |
---|
254 | call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con); |
---|
255 | } |
---|
256 | |
---|
257 | "; |
---|
258 | $tmp = preg_replace('/{/', '{'.$mixer_script, $tmp, 1); |
---|
259 | } |
---|
260 | |
---|
261 | $script .= $tmp; |
---|
262 | } |
---|
263 | |
---|
264 | protected function addDoSelectJoinAllExcept(&$script) |
---|
265 | { |
---|
266 | $tmp = ''; |
---|
267 | parent::addDoSelectJoinAllExcept($tmp); |
---|
268 | |
---|
269 | if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
---|
270 | { |
---|
271 | $mixer_script = " |
---|
272 | |
---|
273 | foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoinAllExcept:doSelectJoinAllExcept') as \$callable) |
---|
274 | { |
---|
275 | call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con); |
---|
276 | } |
---|
277 | |
---|
278 | "; |
---|
279 | $tmp = preg_replace('/public static function doSelectJoinAllExcept.*\(Criteria \$c, \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp); |
---|
280 | } |
---|
281 | |
---|
282 | $script .= $tmp; |
---|
283 | } |
---|
284 | |
---|
285 | protected function addDoUpdate(&$script) |
---|
286 | { |
---|
287 | $tmp = ''; |
---|
288 | parent::addDoUpdate($tmp); |
---|
289 | |
---|
290 | if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
---|
291 | { |
---|
292 | // add sfMixer call |
---|
293 | $pre_mixer_script = " |
---|
294 | |
---|
295 | foreach (sfMixer::getCallables('{$this->getClassname()}:doUpdate:pre') as \$callable) |
---|
296 | { |
---|
297 | \$ret = call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con); |
---|
298 | if (false !== \$ret) |
---|
299 | { |
---|
300 | return \$ret; |
---|
301 | } |
---|
302 | } |
---|
303 | |
---|
304 | "; |
---|
305 | |
---|
306 | $post_mixer_script = " |
---|
307 | |
---|
308 | foreach (sfMixer::getCallables('{$this->getClassname()}:doUpdate:post') as \$callable) |
---|
309 | { |
---|
310 | call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con, \$ret); |
---|
311 | } |
---|
312 | |
---|
313 | return \$ret; |
---|
314 | "; |
---|
315 | |
---|
316 | $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1); |
---|
317 | $tmp = preg_replace("/\t\treturn ([^}]+)/", "\t\t\$ret = $1".$post_mixer_script.' ', $tmp, 1); |
---|
318 | } |
---|
319 | |
---|
320 | $script .= $tmp; |
---|
321 | } |
---|
322 | |
---|
323 | protected function addDoInsert(&$script) |
---|
324 | { |
---|
325 | $tmp = ''; |
---|
326 | parent::addDoInsert($tmp); |
---|
327 | |
---|
328 | if (DataModelBuilder::getBuildProperty('builderAddBehaviors')) |
---|
329 | { |
---|
330 | // add sfMixer call |
---|
331 | $pre_mixer_script = " |
---|
332 | |
---|
333 | foreach (sfMixer::getCallables('{$this->getClassname()}:doInsert:pre') as \$callable) |
---|
334 | { |
---|
335 | \$ret = call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con); |
---|
336 | if (false !== \$ret) |
---|
337 | { |
---|
338 | return \$ret; |
---|
339 | } |
---|
340 | } |
---|
341 | |
---|
342 | "; |
---|
343 | |
---|
344 | $post_mixer_script = " |
---|
345 | foreach (sfMixer::getCallables('{$this->getClassname()}:doInsert:post') as \$callable) |
---|
346 | { |
---|
347 | call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con, \$pk); |
---|
348 | } |
---|
349 | |
---|
350 | return"; |
---|
351 | |
---|
352 | $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1); |
---|
353 | $tmp = preg_replace("/\t\treturn/", "\t\t".$post_mixer_script, $tmp, 1); |
---|
354 | } |
---|
355 | |
---|
356 | $script .= $tmp; |
---|
357 | } |
---|
358 | } |
---|