Development

/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php

You must first sign up to be able to contribute.

root/branches/1.1/lib/plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php

Revision 17357, 12.2 kB (checked in by FabianLange, 5 years ago)

[1.0,1.1,1.2,1.3] doSelectWithI18n no longer accidentally modifies the Criteria passed to it (fixes #6243)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 require_once 'propel/engine/builder/om/php5/PHP5ComplexPeerBuilder.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 propel
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class SfPeerBuilder extends PHP5ComplexPeerBuilder
20 {
21   public function build()
22   {
23     $peerCode = parent::build();
24     if (!DataModelBuilder::getBuildProperty('builderAddComments'))
25     {
26       $peerCode sfToolkit::stripComments($peerCode);
27     }
28     if (!DataModelBuilder::getBuildProperty('builderAddIncludes'))
29     {
30       // remove all inline includes: peer class include inline the mapbuilder classes
31       $peerCode = preg_replace("/(include|require)_once\s*.*MapBuilder\.php.*\s*/", "", $peerCode);
32     }
33
34     // change Propel::import() calls to sfPropel::import()
35     $peerCode = str_replace('Propel::import(', 'sfPropel::import(', $peerCode);
36
37     return $peerCode;
38   }
39
40   protected function addIncludes(&$script)
41   {
42     if (!DataModelBuilder::getBuildProperty('builderAddIncludes'))
43     {
44       return;
45     }
46
47     parent::addIncludes($script);
48   }
49
50   protected function addSelectMethods(&$script)
51   {
52     parent::addSelectMethods($script);
53
54     if ($this->getTable()->getAttribute('isI18N'))
55     {
56       $this->addDoSelectWithI18n($script);
57       $this->addI18nMethods($script);
58     }
59
60     $this->addUniqueColumnNamesMethod($script);
61   }
62
63   protected function addI18nMethods(&$script)
64   {
65     $table = $this->getTable();
66     foreach ($table->getReferrers() as $fk)
67     {
68       $tblFK = $fk->getTable();
69       if ($tblFK->getName() == $table->getAttribute('i18nTable'))
70       {
71         $i18nClassName = $tblFK->getPhpName();
72         break;
73       }
74     }
75
76     $script .= "
77
78   /**
79    * Returns the i18n model class name.
80    *
81    * @return string The i18n model class name
82    */
83   public static function getI18nModel()
84   {
85     return '$i18nClassName';
86   }
87 ";
88   }
89
90   protected function addDoSelectWithI18n(&$script)
91   {
92     $table = $this->getTable();
93     $thisTableObjectBuilder = OMBuilder::getNewObjectBuilder($table);
94     $className = $table->getPhpName();
95     $pks = $table->getPrimaryKey();
96     $pk = PeerBuilder::getColumnName($pks[0], $className);
97
98     // get i18n table name and culture column name
99     foreach ($table->getReferrers() as $fk)
100     {
101       $tblFK = $fk->getTable();
102       if ($tblFK->getName() == $table->getAttribute('i18nTable'))
103       {
104         $i18nClassName = $tblFK->getPhpName();
105         // FIXME
106         $i18nPeerClassName = $i18nClassName.'Peer';
107
108         $i18nTable = $table->getDatabase()->getTable($tblFK->getName());
109         $i18nTableObjectBuilder = OMBuilder::getNewObjectBuilder($i18nTable);
110         $i18nTablePeerBuilder = OMBuilder::getNewPeerBuilder($i18nTable);
111         $i18nPks = $i18nTable->getPrimaryKey();
112         $i18nPk = PeerBuilder::getColumnName($i18nPks[0], $i18nClassName);
113
114         $culturePhpName = '';
115         $cultureColumnName = '';
116         foreach ($tblFK->getColumns() as $col)
117         {
118           if (("true" === strtolower($col->getAttribute('isCulture'))))
119           {
120             $culturePhpName = $col->getPhpName();
121             $cultureColumnName = PeerBuilder::getColumnName($col, $i18nClassName);
122           }
123         }
124       }
125     }
126
127     $script .= "
128
129   /**
130    * Selects a collection of $className objects pre-filled with their i18n objects.
131    *
132    * @return array Array of $className objects.
133    * @throws PropelException Any exceptions caught during processing will be
134    *     rethrown wrapped into a PropelException.
135    */
136   public static function doSelectWithI18n(Criteria \$c, \$culture = null, \$con = null)
137   {
138     // we're going to modify criteria, so copy it first
139     \$c = clone \$c;
140     if (\$culture === null)
141     {
142       \$culture = sfPropel::getDefaultCulture();
143     }
144 ";
145
146     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
147     {
148       $script .= "
149
150     foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoin:doSelectJoin') as \$callable)
151     {
152       call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con);
153     }
154
155 ";
156     }
157
158     $script .= "
159     // Set the correct dbName if it has not been overridden
160     if (\$c->getDbName() == Propel::getDefaultDB())
161     {
162       \$c->setDbName(self::DATABASE_NAME);
163     }
164
165     ".$this->getPeerClassname()."::addSelectColumns(\$c);
166     \$startcol = (".$this->getPeerClassname()."::NUM_COLUMNS - ".$this->getPeerClassname()."::NUM_LAZY_LOAD_COLUMNS) + 1;
167
168     ".$i18nPeerClassName."::addSelectColumns(\$c);
169
170     \$c->addJoin(".$pk.", ".$i18nPk.");
171     \$c->add(".$cultureColumnName.", \$culture);
172
173     \$rs = ".$this->basePeerClassname."::doSelect(\$c, \$con);
174     \$results = array();
175
176     while(\$rs->next()) {
177 ";
178             if ($table->getChildrenColumn()) {
179               $script .= "
180       \$omClass = ".$this->getPeerClassname()."::getOMClass(\$rs, 1);
181 ";
182             } else {
183               $script .= "
184       \$omClass = ".$this->getPeerClassname()."::getOMClass();
185 ";
186             }
187             $script .= "
188       \$cls = Propel::import(\$omClass);
189       \$obj1 = new \$cls();
190       \$obj1->hydrate(\$rs);
191       \$obj1->setCulture(\$culture);
192 ";
193 //            if ($i18nTable->getChildrenColumn()) {
194               $script .= "
195       \$omClass = ".$i18nTablePeerBuilder->getPeerClassname()."::getOMClass(\$rs, \$startcol);
196 ";
197 //            } else {
198 //              $script .= "
199 //      \$omClass = ".$i18nTablePeerBuilder->getPeerClassname()."::getOMClass();
200 //";
201 //            }
202
203             $script .= "
204       \$cls = Propel::import(\$omClass);
205       \$obj2 = new \$cls();
206       \$obj2->hydrate(\$rs, \$startcol);
207
208       \$obj1->set".$i18nClassName."ForCulture(\$obj2, \$culture);
209       \$obj2->set".$className."(\$obj1);
210
211       \$results[] = \$obj1;
212     }
213     return \$results;
214   }
215 ";
216   }
217
218   protected function addDoValidate(&$script)
219   {
220       $tmp = '';
221       parent::addDoValidate($tmp);
222
223       $script .= str_replace("return {$this->basePeerClassname}::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns);\n",
224         "\$res =  {$this->basePeerClassname}::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns);\n".
225         "    if (\$res !== true) {\n".
226         "        \$request = sfContext::getInstance()->getRequest();\n".
227         "        foreach (\$res as \$failed) {\n".
228         "            \$col = ".$this->getPeerClassname()."::translateFieldname(\$failed->getColumn(), BasePeer::TYPE_COLNAME, BasePeer::TYPE_PHPNAME);\n".
229         "            \$request->setError(\$col, \$failed->getMessage());\n".
230         "        }\n".
231         "    }\n\n".
232         "    return \$res;\n", $tmp);
233   }
234
235   protected function addDoSelectRS(&$script)
236   {
237     $tmp = '';
238     parent::addDoSelectRS($tmp);
239
240     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
241     {
242       $mixer_script = "
243
244     foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectRS:doSelectRS') as \$callable)
245     {
246       call_user_func(\$callable, '{$this->getClassname()}', \$criteria, \$con);
247     }
248
249 ";
250       $tmp = preg_replace('/public static function doSelect(RS|Join.*)\(Criteria \$(c|criteria), \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp);
251     }
252
253     $script .= $tmp;
254   }
255
256   protected function addDoSelectJoin(&$script)
257   {
258     $tmp = '';
259     parent::addDoSelectJoin($tmp);
260
261     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
262     {
263       $mixer_script = "
264
265     foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoin:doSelectJoin') as \$callable)
266     {
267       call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con);
268     }
269
270 ";
271       $tmp = preg_replace('/public static function doSelectJoin.*\(Criteria \$c, \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp);
272     }
273
274     $script .= $tmp;
275   }
276
277   protected function addDoSelectJoinAll(&$script)
278   {
279     $tmp = '';
280     parent::addDoSelectJoinAll($tmp);
281
282     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
283     {
284       $mixer_script = "
285
286     foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoinAll:doSelectJoinAll') as \$callable)
287     {
288       call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con);
289     }
290
291 ";
292       $tmp = preg_replace('/public static function doSelectJoinAll\(Criteria \$c, \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp);
293     }
294
295     $script .= $tmp;
296   }
297  
298   protected function addDoSelectJoinAllExcept(&$script)
299   {
300     $tmp = '';
301     parent::addDoSelectJoinAllExcept($tmp);
302
303     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
304     {
305       $mixer_script = "
306
307     foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoinAllExcept:doSelectJoinAllExcept') as \$callable)
308     {
309       call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con);
310     }
311
312 ";
313       $tmp = preg_replace('/public static function doSelectJoinAllExcept.*\(Criteria \$c, \$con = null\)\n\s*{/', '\0'.$mixer_script, $tmp);
314     }
315
316     $script .= $tmp;
317   }
318
319   protected function addDoUpdate(&$script)
320   {
321     $tmp = '';
322     parent::addDoUpdate($tmp);
323
324     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
325     {
326       // add sfMixer call
327       $pre_mixer_script = "
328
329     foreach (sfMixer::getCallables('{$this->getClassname()}:doUpdate:pre') as \$callable)
330     {
331       \$ret = call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con);
332       if (false !== \$ret)
333       {
334         return \$ret;
335       }
336     }
337
338 ";
339
340       $post_mixer_script = "
341
342     foreach (sfMixer::getCallables('{$this->getClassname()}:doUpdate:post') as \$callable)
343     {
344       call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con, \$ret);
345     }
346
347     return \$ret;
348 ";
349
350       $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
351       $tmp = preg_replace("/\t\treturn ([^}]+)/", "\t\t\$ret = $1".$post_mixer_script.'  ', $tmp, 1);
352     }
353
354     $script .= $tmp;
355   }
356
357   protected function addDoInsert(&$script)
358   {
359     $tmp = '';
360     parent::addDoInsert($tmp);
361
362     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
363     {
364       // add sfMixer call
365       $pre_mixer_script = "
366
367     foreach (sfMixer::getCallables('{$this->getClassname()}:doInsert:pre') as \$callable)
368     {
369       \$ret = call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con);
370       if (false !== \$ret)
371       {
372         return \$ret;
373       }
374     }
375
376 ";
377
378       $post_mixer_script = "
379     foreach (sfMixer::getCallables('{$this->getClassname()}:doInsert:post') as \$callable)
380     {
381       call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con, \$pk);
382     }
383
384     return";
385
386       $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
387       $tmp = preg_replace("/\t\treturn/", "\t\t".$post_mixer_script, $tmp, 1);
388     }
389
390     $script .= $tmp;
391   }
392
393   protected function addClassClose(&$script)
394   {
395     parent::addClassClose($script);
396
397     $behavior_file_name = 'Base'.$this->getTable()->getPhpName().'Behaviors';
398     $behavior_file_path = $this->getFilePath($this->getStubObjectBuilder()->getPackage().'.om.'.$behavior_file_name);
399     $absolute_behavior_file_path = sfConfig::get('sf_root_dir').'/'.$behavior_file_path;
400
401     if (file_exists($absolute_behavior_file_path))
402     {
403       unlink($absolute_behavior_file_path);
404     }
405
406     $behaviors = $this->getTable()->getAttribute('behaviors');
407     if ($behaviors)
408     {
409       file_put_contents($absolute_behavior_file_path, sprintf("<?php\nsfPropelBehavior::add('%s', %s);\n", $this->getTable()->getPhpName(), var_export(unserialize($behaviors), true)));
410
411       $behavior_include_script = <<<EOF
412
413
414 if (sfProjectConfiguration::getActive() instanceof sfApplicationConfiguration)
415 {
416   include_once '%s';
417 }
418
419 EOF;
420       $script .= sprintf($behavior_include_script, $behavior_file_path);
421     }
422   }
423
424   protected function addUniqueColumnNamesMethod(&$script)
425   {
426     $unices = array();
427     foreach ($this->getTable()->getUnices() as $unique)
428     {
429       $unices[] = sprintf("array('%s')", implode("', '", $unique->getColumns()));
430     }
431
432     $unices = array_unique($unices);
433
434     $unices = implode(', ', $unices);
435     $script .= <<<EOF
436
437
438   static public function getUniqueColumnNames()
439   {
440     return array($unices);
441   }
442 EOF;
443   }
444 }
445
Note: See TracBrowser for help on using the browser.