Development

/branches/1.3/lib/plugins/sfPropelPlugin/lib/builder/SfObjectBuilder.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfPropelPlugin/lib/builder/SfObjectBuilder.php

Revision 23909, 8.6 kB (checked in by FabianLange, 4 years ago)

[1.2][1.3] updated SfObjectBuilder? so that Propel objects do not query the I18N table when they are new (fixes #7513, patch by joostdj)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 require_once 'propel/engine/builder/om/php5/PHP5ObjectBuilder.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  * @deprecated since symfony 1.3
20  */
21 class SfObjectBuilder extends PHP5ObjectBuilder
22 {
23   public function build()
24   {
25     $objectCode = parent::build();
26     if (!$this->getBuildProperty('builderAddComments'))
27     {
28       $objectCode = sfToolkit::stripComments($objectCode);
29     }
30
31     return $objectCode;
32   }
33
34   protected function addClassBody(&$script)
35   {
36     parent::addClassBody($script);
37
38     if ($this->getTable()->getAttribute('isI18N'))
39     {
40       if (count($this->getTable()->getPrimaryKey()) > 1)
41       {
42         throw new Exception('i18n support only works with a single primary key');
43       }
44
45       $this->addCultureAccessorMethod($script);
46       $this->addCultureMutatorMethod($script);
47
48       $this->addI18nMethods($script);
49     }
50
51     if ($this->getBuildProperty('builderAddBehaviors'))
52     {
53       $this->addCall($script);
54     }
55   }
56
57   protected function addCall(&$script)
58   {
59     $script .= "
60   public function __call(\$method, \$arguments)
61   {
62     if (!\$callable = sfMixer::getCallable('{$this->getClassname()}:'.\$method))
63     {
64       throw new sfException(sprintf('Call to undefined method {$this->getClassname()}::%s', \$method));
65     }
66
67     array_unshift(\$arguments, \$this);
68
69     return call_user_func_array(\$callable, \$arguments);
70   }
71 ";
72   }
73
74   protected function addAttributes(&$script)
75   {
76     parent::addAttributes($script);
77
78     if ($this->getTable()->getAttribute('isI18N'))
79     {
80       $script .= '
81   /**
82    * The value for the culture field.
83    * @var string
84    */
85   protected $culture;
86 ';
87     }
88   }
89
90   protected function addCultureAccessorMethod(&$script)
91   {
92     $script .= '
93
94   /**
95    * Returns the culture.
96    *
97    * @return string The culture
98    */
99   public function getCulture()
100   {
101     return $this->culture;
102   }
103 ';
104   }
105
106   protected function addCultureMutatorMethod(&$script)
107   {
108     $script .= '
109   /**
110    * Sets the culture.
111    *
112    * @param string $culture The culture to set
113    *
114    * @return void
115    */
116   public function setCulture($culture)
117   {
118     $this->culture = $culture;
119   }
120 ';
121   }
122
123   protected function addI18nMethods(&$script)
124   {
125     $table = $this->getTable();
126     $pks = $table->getPrimaryKey();
127     $pk = $pks[0]->getPhpName();
128
129     foreach ($table->getReferrers() as $fk)
130     {
131       $tblFK = $fk->getTable();
132       if ($tblFK->getName() == $table->getAttribute('i18nTable'))
133       {
134         $className = $tblFK->getPhpName();
135         $culture = '';
136         $culture_peername = '';
137         foreach ($tblFK->getColumns() as $col)
138         {
139           if (("true" === strtolower($col->getAttribute('isCulture'))))
140           {
141             $culture = $col->getPhpName();
142             $culture_peername = PeerBuilder::getColumnName($col, $className);
143           }
144         }
145
146         foreach ($tblFK->getColumns() as $col)
147         {
148           if ($col->isPrimaryKey()) continue;
149
150           $script .= '
151   public function get'.$col->getPhpName().'($culture = null)
152   {
153     return $this->getCurrent'.$className.'($culture)->get'.$col->getPhpName().'();
154   }
155
156   public function set'.$col->getPhpName().'($value, $culture = null)
157   {
158     $this->getCurrent'.$className.'($culture)->set'.$col->getPhpName().'($value);
159   }
160 ';
161         }
162
163 $script .= '
164   protected $current_i18n = array();
165
166   public function getCurrent'.$className.'($culture = null)
167   {
168     if (null === $culture)
169     {
170       $culture = null === $this->culture ? sfPropel::getDefaultCulture() : $this->culture;
171     }
172
173     if (!isset($this->current_i18n[$culture]))
174     {
175       $obj = $this->isNew() ? null : '.$className.'Peer::retrieveByPK($this->get'.$pk.'(), $culture);
176       if ($obj)
177       {
178         $this->set'.$className.'ForCulture($obj, $culture);
179       }
180       else
181       {
182         $this->set'.$className.'ForCulture(new '.$className.'(), $culture);
183         $this->current_i18n[$culture]->set'.$culture.'($culture);
184       }
185     }
186
187     return $this->current_i18n[$culture];
188   }
189
190   public function set'.$className.'ForCulture($object, $culture)
191   {
192     $this->current_i18n[$culture] = $object;
193     $this->add'.$className.'($object);
194   }
195 ';
196       }
197     }
198   }
199
200   protected function addDoSave(&$script)
201   {
202     $tmp = '';
203     parent::addDoSave($tmp);
204     // add autosave to i18n object even if the base object is not changed
205     $tmp = preg_replace_callback('#(\$this\->(.+?)\->isModified\(\))#', array($this, 'i18nDoSaveCallback'), $tmp);
206
207     $script .= $tmp;
208   }
209
210   private function i18nDoSaveCallback($matches)
211   {
212     $value = $matches[1];
213
214     // get the related class to see if it is a i18n one
215     $table = $this->getTable();
216     $column = null;
217     foreach ($table->getForeignKeys() as $fk)
218     {
219       if ($matches[2] == $this->getFKVarName($fk))
220       {
221         $column = $fk;
222         break;
223       }
224     }
225     $foreign_table = $this->getDatabase()->getTable($fk->getForeignTableName());
226     if ($foreign_table->getAttribute('isI18N'))
227     {
228       $foreign_tables_i18n_table = $this->getDatabase()->getTable($foreign_table->getAttribute('i18nTable'));
229       $value .= ' || ($this->'.$matches[2].'->getCulture() && $this->'.$matches[2].'->getCurrent'.$foreign_tables_i18n_table->getPhpName().'()->isModified())';
230     }
231
232     return $value;
233   }
234
235   protected function addDelete(&$script)
236   {
237     $tmp = '';
238     parent::addDelete($tmp);
239
240     if ($this->getBuildProperty('builderAddBehaviors'))
241     {
242       // add sfMixer call
243       $pre_mixer_script = "
244
245     foreach (sfMixer::getCallables('{$this->getClassname()}:delete:pre') as \$callable)
246     {
247       \$ret = call_user_func(\$callable, \$this, \$con);
248       if (\$ret)
249       {
250         return;
251       }
252     }
253
254 ";
255       $post_mixer_script = "
256
257     foreach (sfMixer::getCallables('{$this->getClassname()}:delete:post') as \$callable)
258     {
259       call_user_func(\$callable, \$this, \$con);
260     }
261
262 ";
263       $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
264       $tmp = preg_replace('/}\s*$/', $post_mixer_script.'  }', $tmp);
265     }
266
267     // update current script
268     $script .= $tmp;
269   }
270
271   protected function addSave(&$script)
272   {
273     $tmp = '';
274     parent::addSave($tmp);
275
276     // add support for created_(at|on) and updated_(at|on) columns
277     $date_script = '';
278     $updated = false;
279     $created = false;
280     foreach ($this->getTable()->getColumns() as $col)
281     {
282       $clo = strtolower($col->getName());
283
284       if (!$updated && in_array($clo, array('updated_at', 'updated_on')))
285       {
286         $updated = true;
287         $date_script .= "
288     if (\$this->isModified() && !\$this->isColumnModified(".$this->getColumnConstant($col)."))
289     {
290       \$this->set".$col->getPhpName()."(time());
291     }
292 ";
293       }
294       else if (!$created && in_array($clo, array('created_at', 'created_on')))
295       {
296         $created = true;
297         $date_script .= "
298     if (\$this->isNew() && !\$this->isColumnModified(".$this->getColumnConstant($col)."))
299     {
300       \$this->set".$col->getPhpName()."(time());
301     }
302 ";
303       }
304     }
305     $tmp = preg_replace('/{/', '{'.$date_script, $tmp, 1);
306
307     if ($this->getBuildProperty('builderAddBehaviors'))
308     {
309       // add sfMixer call
310       $pre_mixer_script = "
311
312     foreach (sfMixer::getCallables('{$this->getClassname()}:save:pre') as \$callable)
313     {
314       \$affectedRows = call_user_func(\$callable, \$this, \$con);
315       if (is_int(\$affectedRows))
316       {
317         return \$affectedRows;
318       }
319     }
320
321 ";
322       $post_mixer_script = <<<EOF
323
324     foreach (sfMixer::getCallables('{$this->getClassname()}:save:post') as \$callable)
325     {
326       call_user_func(\$callable, \$this, \$con, \$affectedRows);
327     }
328
329 EOF;
330       $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
331       $tmp = preg_replace('/(\$con\->commit\(\);)/', '$1'.$post_mixer_script, $tmp);
332     }
333
334     // update current script
335     $script .= $tmp;
336   }
337
338   protected function addClassClose(&$script)
339   {
340     parent::addClassClose($script);
341
342     $behaviors = $this->getTable()->getAttribute('behaviors');
343     if ($behaviors)
344     {
345       $behavior_file_name = 'Base'.$this->getTable()->getPhpName().'Behaviors';
346       $behavior_file_path = ClassTools::getFilePath($this->getStubObjectBuilder()->getPackage().'.om', $behavior_file_name);
347
348       $script .= sprintf("\ninclude_once '%s';\n", $behavior_file_path);
349     }
350   }
351
352   protected function addConstants(&$script)
353   {
354     $script .= "\n  const PEER = '".$this->getPeerClassName()."';\n";
355   }
356 }
357
Note: See TracBrowser for help on using the browser.