Development

/trunk/lib/plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php

You must first sign up to be able to contribute.

root/trunk/lib/plugins/sfPropelPlugin/lib/propel/builder/SfPeerBuilder.php

Revision 8020, 11.4 kB (checked in by fabien, 2 months ago)

merged 1.1 branch changes (to r8006)

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