Development

/branches/1.2/lib/plugins/sfPropelPlugin/lib/builder/SfPeerBuilder.php

You must first sign up to be able to contribute.

root/branches/1.2/lib/plugins/sfPropelPlugin/lib/builder/SfPeerBuilder.php

Revision 28393, 25.4 kB (checked in by fabien, 5 years ago)

[1.2, 1.3] removed unneeded code (which breaks the separation of concerns by the way - closes #8375)

  • 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/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 propel
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19 class SfPeerBuilder extends PHP5PeerBuilder
20 {
21   public function build()
22   {
23     $peerCode = parent::build();
24     if (!DataModelBuilder::getBuildProperty('builderAddComments'))
25     {
26       $peerCode sfToolkit::stripComments($peerCode);
27     }
28
29     if (!DataModelBuilder::getBuildProperty('builderAddIncludes'))
30     {
31       // remove all inline includes: peer class include inline the mapbuilder classes
32       $peerCode = preg_replace("/(include|require)_once\s*.*Base.*Peer\.php.*\s*/", "", $peerCode);
33       $peerCode = preg_replace("/(include|require)_once\s*.*MapBuilder\.php.*\s*/", "", $peerCode);
34     }
35
36     // change Propel::import() calls to sfPropel::import()
37     $peerCode = str_replace('Propel::import(', 'sfPropel::import(', $peerCode);
38
39     return $peerCode;
40   }
41
42   protected function addIncludes(&$script)
43   {
44     if (!DataModelBuilder::getBuildProperty('builderAddIncludes'))
45     {
46       return;
47     }
48
49     parent::addIncludes($script);
50   }
51
52   protected function addSelectMethods(&$script)
53   {
54     parent::addSelectMethods($script);
55
56     if ($this->getTable()->getAttribute('isI18N'))
57     {
58       $this->addDoSelectWithI18n($script);
59       $this->addI18nMethods($script);
60     }
61
62     $this->addUniqueColumnNamesMethod($script);
63   }
64
65   protected function addI18nMethods(&$script)
66   {
67     $table = $this->getTable();
68     foreach ($table->getReferrers() as $fk)
69     {
70       $tblFK = $fk->getTable();
71       if ($tblFK->getName() == $table->getAttribute('i18nTable'))
72       {
73         $i18nClassName = $tblFK->getPhpName();
74         break;
75       }
76     }
77
78     $script .= "
79
80   /**
81    * Returns the i18n model class name.
82    *
83    * @return string The i18n model class name
84    */
85   public static function getI18nModel()
86   {
87     return '$i18nClassName';
88   }
89 ";
90   }
91
92   protected function addDoSelectWithI18n(&$script)
93   {
94     $table = $this->getTable();
95     $thisTableObjectBuilder = OMBuilder::getNewObjectBuilder($table);
96     $className = $table->getPhpName();
97     $pks = $table->getPrimaryKey();
98     $pk = PeerBuilder::getColumnName($pks[0], $className);
99
100     // get i18n table name and culture column name
101     foreach ($table->getReferrers() as $fk)
102     {
103       $tblFK = $fk->getTable();
104       if ($tblFK->getName() == $table->getAttribute('i18nTable'))
105       {
106         $i18nClassName = $tblFK->getPhpName();
107
108         // FIXME
109         $i18nPeerClassName = $i18nClassName.'Peer';
110
111         $i18nTable = $table->getDatabase()->getTable($tblFK->getName());
112         $i18nTableObjectBuilder = OMBuilder::getNewObjectBuilder($i18nTable);
113         $i18nTablePeerBuilder = OMBuilder::getNewPeerBuilder($i18nTable);
114         $i18nPks = $i18nTable->getPrimaryKey();
115         $i18nPk = PeerBuilder::getColumnName($i18nPks[0], $i18nClassName);
116
117         $culturePhpName = '';
118         $cultureColumnName = '';
119         foreach ($tblFK->getColumns() as $col)
120         {
121           if (('true' == trim(strtolower($col->getAttribute('isCulture')))))
122           {
123             $culturePhpName = $col->getPhpName();
124             $cultureColumnName = PeerBuilder::getColumnName($col, $i18nClassName);
125           }
126         }
127       }
128     }
129
130     $script .= "
131
132   /**
133    * Selects a collection of $className objects pre-filled with their i18n objects.
134    *
135    * @return array Array of $className objects.
136    * @throws PropelException Any exceptions caught during processing will be
137    *     rethrown wrapped into a PropelException.
138    */
139   public static function doSelectWithI18n(Criteria \$c, \$culture = null, PropelPDO \$con = null)
140   {
141     // we're going to modify criteria, so copy it first
142     \$c = clone \$c;
143     if (\$culture === null)
144     {
145       \$culture = sfPropel::getDefaultCulture();
146     }
147 ";
148
149     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
150     {
151       $script .= "
152
153     foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoin:doSelectJoin') as \$callable)
154     {
155       call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con);
156     }
157
158 ";
159     }
160
161     $script .= "
162     // Set the correct dbName if it has not been overridden
163     if (\$c->getDbName() == Propel::getDefaultDB())
164     {
165       \$c->setDbName(self::DATABASE_NAME);
166     }
167
168     ".$this->getPeerClassname()."::addSelectColumns(\$c);
169     \$startcol = (".$this->getPeerClassname()."::NUM_COLUMNS - ".$this->getPeerClassname()."::NUM_LAZY_LOAD_COLUMNS);
170
171     ".$i18nPeerClassName."::addSelectColumns(\$c);
172
173     \$c->addJoin(".$pk.", ".$i18nPk.");
174     \$c->add(".$cultureColumnName.", \$culture);
175
176     \$stmt = ".$this->basePeerClassname."::doSelect(\$c, \$con);
177     \$results = array();
178
179     while(\$row = \$stmt->fetch(PDO::FETCH_NUM)) {
180 ";
181             if ($table->getChildrenColumn()) {
182               $script .= "
183       \$omClass = ".$this->getPeerClassname()."::getOMClass(\$row, \$startcol);
184 ";
185             } else {
186               $script .= "
187       \$omClass = ".$this->getPeerClassname()."::getOMClass();
188 ";
189             }
190             $script .= "
191       \$cls = Propel::importClass(\$omClass);
192       \$obj1 = new \$cls();
193       \$obj1->hydrate(\$row);
194       \$obj1->setCulture(\$culture);
195 ";
196             if ($i18nTable->getChildrenColumn()) {
197               $script .= "
198       \$omClass = ".$i18nTablePeerBuilder->getPeerClassname()."::getOMClass(\$row, \$startcol);
199 ";
200             } else {
201               $script .= "
202       \$omClass = ".$i18nTablePeerBuilder->getPeerClassname()."::getOMClass();
203 ";
204             }
205
206             $script .= "
207       \$cls = Propel::importClass(\$omClass);
208       \$obj2 = new \$cls();
209       \$obj2->hydrate(\$row, \$startcol);
210
211       \$obj1->set".$i18nClassName."ForCulture(\$obj2, \$culture);
212       \$obj2->set".$className."(\$obj1);
213
214       \$results[] = \$obj1;
215     }
216     return \$results;
217   }
218 ";
219   }
220
221   protected function addDoValidate(&$script)
222   {
223       $tmp = '';
224       parent::addDoValidate($tmp);
225
226       /**
227        * @todo setup 1.1 global validation errors for propel model validation
228        */
229       $script .= str_replace("return {$this->basePeerClassname}::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns);\n",
230         "\$res =  {$this->basePeerClassname}::doValidate(".$this->getPeerClassname()."::DATABASE_NAME, ".$this->getPeerClassname()."::TABLE_NAME, \$columns);\n".
231         "    if (\$res !== true) {\n".
232         "        foreach (\$res as \$failed) {\n".
233         "            \$col = ".$this->getPeerClassname()."::translateFieldname(\$failed->getColumn(), BasePeer::TYPE_COLNAME, BasePeer::TYPE_PHPNAME);\n".
234         "        }\n".
235         "    }\n\n".
236         "    return \$res;\n", $tmp);
237   }
238
239   protected function addDoSelectStmt(&$script)
240   {
241     $tmp = '';
242     parent::addDoSelectStmt($tmp);
243
244     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
245     {
246       $mixer_script = "
247
248     foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectStmt:doSelectStmt') as \$callable)
249     {
250       call_user_func(\$callable, '{$this->getClassname()}', \$criteria, \$con);
251     }
252
253 ";
254
255      $tmp = preg_replace('/{/', '{'.$mixer_script, $tmp, 1);
256     }
257
258     $script .= $tmp;
259   }
260
261   protected function addDoSelectJoin(&$script)
262   {
263     $tmp = '';
264     parent::addDoSelectJoin($tmp);
265
266     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
267     {
268       $mixer_script = "
269
270     foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoin:doSelectJoin') as \$callable)
271     {
272       call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con);
273     }
274
275 ";
276       $tmp = preg_replace('/{/', '{'.$mixer_script, $tmp, 1);
277     }
278
279     $script .= $tmp;
280   }
281
282   protected function addDoSelectJoinAll(&$script)
283   {
284     $tmp = '';
285     parent::addDoSelectJoinAll($tmp);
286
287     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
288     {
289       $mixer_script = "
290
291     foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoinAll:doSelectJoinAll') as \$callable)
292     {
293       call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con);
294     }
295
296 ";
297       $tmp = preg_replace('/{/', '{'.$mixer_script, $tmp, 1);
298     }
299
300     $script .= $tmp;
301   }
302
303   protected function addDoSelectJoinAllExcept(&$script)
304   {
305     $tmp = '';
306     parent::addDoSelectJoinAllExcept($tmp);
307
308     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
309     {
310       $mixer_script = "
311
312     foreach (sfMixer::getCallables('{$this->getClassname()}:doSelectJoinAllExcept:doSelectJoinAllExcept') as \$callable)
313     {
314       call_user_func(\$callable, '{$this->getClassname()}', \$c, \$con);
315     }
316
317 ";
318       $tmp = preg_replace('/{/', '{'.$mixer_script, $tmp, 1);
319     }
320
321     $script .= $tmp;
322   }
323
324   protected function addDoUpdate(&$script)
325   {
326     $tmp = '';
327     parent::addDoUpdate($tmp);
328
329     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
330     {
331       // add sfMixer call
332       $pre_mixer_script = "
333
334     foreach (sfMixer::getCallables('{$this->getClassname()}:doUpdate:pre') as \$callable)
335     {
336       \$ret = call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con);
337       if (false !== \$ret)
338       {
339         return \$ret;
340       }
341     }
342
343 ";
344
345       $post_mixer_script = "
346
347     foreach (sfMixer::getCallables('{$this->getClassname()}:doUpdate:post') as \$callable)
348     {
349       call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con, \$ret);
350     }
351
352     return \$ret;
353 ";
354
355       $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
356       $tmp = preg_replace("/\t\treturn ([^}]+)/", "\t\t\$ret = $1".$post_mixer_script.'  ', $tmp, 1);
357     }
358
359     $script .= $tmp;
360   }
361
362   protected function addDoInsert(&$script)
363   {
364     $tmp = '';
365     parent::addDoInsert($tmp);
366
367     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
368     {
369       // add sfMixer call
370       $pre_mixer_script = "
371
372     foreach (sfMixer::getCallables('{$this->getClassname()}:doInsert:pre') as \$callable)
373     {
374       \$ret = call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con);
375       if (false !== \$ret)
376       {
377         return \$ret;
378       }
379     }
380
381 ";
382
383       $post_mixer_script = "
384     foreach (sfMixer::getCallables('{$this->getClassname()}:doInsert:post') as \$callable)
385     {
386       call_user_func(\$callable, '{$this->getClassname()}', \$values, \$con, \$pk);
387     }
388
389     return";
390
391       $tmp = preg_replace('/{/', '{'.$pre_mixer_script, $tmp, 1);
392       $tmp = preg_replace("/\t\treturn/", "\t\t".$post_mixer_script, $tmp, 1);
393     }
394
395     $script .= $tmp;
396   }
397
398   protected function addClassClose(&$script)
399   {
400     parent::addClassClose($script);
401
402     $behavior_file_name = 'Base'.$this->getTable()->getPhpName().'Behaviors';
403     $behavior_file_path = ClassTools::getFilePath($this->getStubObjectBuilder()->getPackage().'.om', $behavior_file_name);
404
405     $absolute_behavior_file_path = sfConfig::get('sf_root_dir').'/'.$behavior_file_path;
406
407     if (file_exists($absolute_behavior_file_path))
408     {
409       unlink($absolute_behavior_file_path);
410     }
411
412     $behaviors = $this->getTable()->getAttribute('behaviors');
413     if ($behaviors)
414     {
415       file_put_contents($absolute_behavior_file_path, sprintf("<?php\nsfPropelBehavior::add('%s', %s);\n", $this->getTable()->getPhpName(), var_export(unserialize($behaviors), true)));
416
417       $behavior_include_script = <<<EOF
418
419
420 if (sfProjectConfiguration::getActive() instanceof sfApplicationConfiguration)
421 {
422   include_once '%s';
423 }
424
425 EOF;
426       $script .= sprintf($behavior_include_script, $behavior_file_path);
427     }
428   }
429
430   protected function addUniqueColumnNamesMethod(&$script)
431   {
432     $unices = array();
433     foreach ($this->getTable()->getUnices() as $unique)
434     {
435       $unices[] = sprintf("array('%s')", implode("', '", $unique->getColumns()));
436     }
437
438     $unices = array_unique($unices);
439
440     $unices = implode(', ', $unices);
441     $script .= <<<EOF
442
443
444   static public function getUniqueColumnNames()
445   {
446     return array($unices);
447   }
448 EOF;
449   }
450
451     /**
452      * Adds the doCountJoin*() methods.
453      * @param      string &$script The script will be modified in this method.
454      */
455     protected function addDoCountJoin(&$script)
456     {
457         $table = $this->getTable();
458         $className = $this->getObjectClassname();
459         $countFK = count($table->getForeignKeys());
460         $join_behavior = $this->getJoinBehavior();
461
462         if ($countFK >= 1) {
463
464             foreach ($table->getForeignKeys() as $fk) {
465
466                 $joinTable = $table->getDatabase()->getTable($fk->getForeignTableName());
467
468                 if (!$joinTable->isForReferenceOnly()) {
469
470                     if ( $fk->getForeignTableName() != $table->getName() ) {
471
472                         $thisTableObjectBuilder = $this->getNewObjectBuilder($table);
473                         $joinedTableObjectBuilder = $this->getNewObjectBuilder($joinTable);
474                         $joinedTablePeerBuilder = $this->getNewPeerBuilder($joinTable);
475
476                         $joinClassName = $joinedTableObjectBuilder->getObjectClassname();
477
478                         $script .= "
479
480     /**
481      * Returns the number of rows matching criteria, joining the related ".$thisTableObjectBuilder->getFKPhpNameAffix($fk, $plural = false)." table
482      *
483      * @param      Criteria \$c
484      * @param      boolean \$distinct Whether to select only distinct columns; deprecated: use Criteria->setDistinct() instead.
485      * @param      PropelPDO \$con
486      * @param      String    \$join_behavior the type of joins to use, defaults to $join_behavior
487      * @return     int Number of matching rows.
488      */
489     public static function doCountJoin".$thisTableObjectBuilder->getFKPhpNameAffix($fk, $plural = false)."(Criteria \$criteria, \$distinct = false, PropelPDO \$con = null, \$join_behavior = $join_behavior)
490     {
491         // we're going to modify criteria, so copy it first
492         \$criteria = clone \$criteria;
493
494         // We need to set the primary table name, since in the case that there are no WHERE columns
495         // it will be impossible for the BasePeer::createSelectSql() method to determine which
496         // tables go into the FROM clause.
497         \$criteria->setPrimaryTableName(".$this->getPeerClassname()."::TABLE_NAME);
498
499         if (\$distinct && !in_array(Criteria::DISTINCT, \$criteria->getSelectModifiers())) {
500             \$criteria->setDistinct();
501         }
502
503         if (!\$criteria->hasSelectClause()) {
504             ".$this->getPeerClassname()."::addSelectColumns(\$criteria);
505         }
506
507         \$criteria->clearOrderByColumns(); // ORDER BY won't ever affect the count
508
509         // Set the correct dbName
510         \$criteria->setDbName(self::DATABASE_NAME);
511
512         if (\$con === null) {
513             \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_READ);
514         }
515 ";
516
517                         $lfMap = $fk->getLocalForeignMapping();
518                         $left = array();
519                         $right = array();
520                         foreach ($fk->getLocalColumns() as $columnName ) {
521                             array_push($left, $this->getColumnConstant($table->getColumn($columnName) ) );
522                             array_push($right, $joinedTablePeerBuilder->getColumnConstant($joinTable->getColumn( $lfMap[$columnName] ) ) );
523                         }
524                         $script .= "
525         \$criteria->addJoin(array(";
526                         foreach ($left as $lCol) {
527                             $script .= $lCol;
528                             if ($lCol != $left[count($left)]) {
529                                 $script .= ",";
530                             }
531                         }
532                         $script .= "), array(";
533                         foreach ($right as $rCol) {
534                             $script .= $rCol;
535                             if ($rCol != $right[count($right)]) {
536                                 $script .= ",";
537                             }
538                         }
539                         $script .= "), \$join_behavior);
540 ";
541
542     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
543     {
544       $script .= "
545
546     foreach (sfMixer::getCallables('{$this->getClassname()}:doCount:doCount') as \$callable)
547     {
548       call_user_func(\$callable, '{$this->getClassname()}', \$criteria, \$con);
549     }
550
551 ";
552     }
553
554       $script .= "
555         \$stmt = ".$this->basePeerClassname."::doCount(\$criteria, \$con);
556
557         if (\$row = \$stmt->fetch(PDO::FETCH_NUM)) {
558             \$count = (int) \$row[0];
559         } else {
560             \$count = 0; // no rows returned; we infer that means 0 matches.
561         }
562         \$stmt->closeCursor();
563         return \$count;
564     }
565 ";
566                     }
567                 }
568             }
569         }
570
571     }
572
573     /**
574      * Adds the doCountJoinAll() method.
575      * @param      string &$script The script will be modified in this method.
576      */
577     protected function addDoCountJoinAll(&$script)
578     {
579         $table = $this->getTable();
580         $className = $this->getObjectClassname();
581         $join_behavior = $this->getJoinBehavior();
582
583         $script .= "
584
585     /**
586      * Returns the number of rows matching criteria, joining all related tables
587      *
588      * @param      Criteria \$c
589      * @param      boolean \$distinct Whether to select only distinct columns; deprecated: use Criteria->setDistinct() instead.
590      * @param      PropelPDO \$con
591      * @param      String    \$join_behavior the type of joins to use, defaults to $join_behavior
592      * @return     int Number of matching rows.
593      */
594     public static function doCountJoinAll(Criteria \$criteria, \$distinct = false, PropelPDO \$con = null, \$join_behavior = $join_behavior)
595     {
596         // we're going to modify criteria, so copy it first
597         \$criteria = clone \$criteria;
598
599         // We need to set the primary table name, since in the case that there are no WHERE columns
600         // it will be impossible for the BasePeer::createSelectSql() method to determine which
601         // tables go into the FROM clause.
602         \$criteria->setPrimaryTableName(".$this->getPeerClassname()."::TABLE_NAME);
603
604         if (\$distinct && !in_array(Criteria::DISTINCT, \$criteria->getSelectModifiers())) {
605             \$criteria->setDistinct();
606         }
607
608         if (!\$criteria->hasSelectClause()) {
609             ".$this->getPeerClassname()."::addSelectColumns(\$criteria);
610         }
611
612         \$criteria->clearOrderByColumns(); // ORDER BY won't ever affect the count
613
614         // Set the correct dbName
615         \$criteria->setDbName(self::DATABASE_NAME);
616
617         if (\$con === null) {
618             \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_READ);
619         }
620 ";
621
622         foreach ($table->getForeignKeys() as $fk) {
623             // want to cover this case, but the code is not there yet.
624             if ( $fk->getForeignTableName() != $table->getName() ) {
625                 $joinTable = $table->getDatabase()->getTable($fk->getForeignTableName());
626                 $joinedTablePeerBuilder = $this->getNewPeerBuilder($joinTable);
627
628                 $joinClassName = $joinedTablePeerBuilder->getObjectClassname();
629
630                 $lfMap = $fk->getLocalForeignMapping();
631                 $left = array();
632                 $right = array();
633                 foreach ($fk->getLocalColumns() as $columnName ) {
634                     array_push($left, $this->getColumnConstant($table->getColumn($columnName) ) );
635                     array_push($right, $joinedTablePeerBuilder->getColumnConstant($joinTable->getColumn( $lfMap[$columnName] ) ) );
636                 }
637                 $script .= "
638         \$criteria->addJoin(array(";
639                 foreach ($left as $lCol) {
640                     $script .= $lCol;
641                     if ($lCol != $left[count($left)]) {
642                         $script .= ",";
643                     }
644                 }
645                 $script .= "), array(";
646                 foreach ($right as $rCol) {
647                     $script .= $rCol;
648                     if ($rCol != $right[count($right)]) {
649                         $script .= ",";
650                     }
651                 }
652                 $script .= "), \$join_behavior);";
653             } // if fk->getForeignTableName != table->getName
654         } // foreach [sub] foreign keys
655
656     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
657     {
658       $script .= "
659
660     foreach (sfMixer::getCallables('{$this->getClassname()}:doCount:doCount') as \$callable)
661     {
662       call_user_func(\$callable, '{$this->getClassname()}', \$criteria, \$con);
663     }
664
665 ";
666     }
667
668
669         $script .= "
670         \$stmt = ".$this->basePeerClassname."::doCount(\$criteria, \$con);
671
672         if (\$row = \$stmt->fetch(PDO::FETCH_NUM)) {
673             \$count = (int) \$row[0];
674         } else {
675             \$count = 0; // no rows returned; we infer that means 0 matches.
676         }
677         \$stmt->closeCursor();
678         return \$count;
679     }";
680     } // end addDoCountJoinAll()
681
682     /**
683      * Adds the doCountJoinAllExcept*() methods.
684      * @param      string &$script The script will be modified in this method.
685      */
686     protected function addDoCountJoinAllExcept(&$script)
687     {
688         $table = $this->getTable();
689         $join_behavior = $this->getJoinBehavior();
690
691         $fkeys = $table->getForeignKeys();  // this sep assignment is necessary otherwise sub-loops over
692         // getForeignKeys() will cause this to only execute one time.
693         foreach ($fkeys as $fk ) {
694
695             $tblFK = $table->getDatabase()->getTable($fk->getForeignTableName());
696
697             $excludedTable = $table->getDatabase()->getTable($fk->getForeignTableName());
698
699             $thisTableObjectBuilder = $this->getNewObjectBuilder($table);
700             $excludedTableObjectBuilder = $this->getNewObjectBuilder($excludedTable);
701             $excludedTablePeerBuilder = $this->getNewPeerBuilder($excludedTable);
702
703             $excludedClassName = $excludedTableObjectBuilder->getObjectClassname();
704
705             $script .= "
706
707     /**
708      * Returns the number of rows matching criteria, joining the related ".$thisTableObjectBuilder->getFKPhpNameAffix($fk, $plural = false)." table
709      *
710      * @param      Criteria \$c
711      * @param      boolean \$distinct Whether to select only distinct columns; deprecated: use Criteria->setDistinct() instead.
712      * @param      PropelPDO \$con
713      * @param      String    \$join_behavior the type of joins to use, defaults to $join_behavior
714      * @return     int Number of matching rows.
715      */
716     public static function doCountJoinAllExcept".$thisTableObjectBuilder->getFKPhpNameAffix($fk, $plural = false)."(Criteria \$criteria, \$distinct = false, PropelPDO \$con = null, \$join_behavior = $join_behavior)
717     {
718         // we're going to modify criteria, so copy it first
719         \$criteria = clone \$criteria;
720
721         if (\$distinct && !in_array(Criteria::DISTINCT, \$criteria->getSelectModifiers())) {
722             \$criteria->setDistinct();
723         }
724
725         if (!\$criteria->hasSelectClause()) {
726             ".$this->getPeerClassname()."::addSelectColumns(\$criteria);
727         }
728
729         \$criteria->clearOrderByColumns(); // ORDER BY won't ever affect the count
730
731         // Set the correct dbName
732         \$criteria->setDbName(self::DATABASE_NAME);
733
734         if (\$con === null) {
735             \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_READ);
736         }
737     ";
738
739             foreach ($table->getForeignKeys() as $subfk) {
740                 // want to cover this case, but the code is not there yet.
741                 if ( $subfk->getForeignTableName() != $table->getName() ) {
742                     $joinTable = $table->getDatabase()->getTable($subfk->getForeignTableName());
743                     $joinTablePeerBuilder = $this->getNewPeerBuilder($joinTable);
744                     $joinClassName = $joinTablePeerBuilder->getObjectClassname();
745
746                     if ($joinClassName != $excludedClassName)
747                     {
748                         $lfMap = $subfk->getLocalForeignMapping();
749                         $left = array();
750                         $right = array();
751                         foreach ($subfk->getLocalColumns() as $columnName ) {
752                             array_push($left, $this->getColumnConstant($table->getColumn($columnName) ) );
753                             array_push($right, $joinTablePeerBuilder->getColumnConstant($joinTable->getColumn( $lfMap[$columnName] ) ) );
754                         }
755                         $script .= "
756                 \$criteria->addJoin(array(";
757                         foreach ($left as $lCol) {
758                             $script .= $lCol;
759                             if ($lCol != $left[count($left)]) {
760                                 $script .= ",";
761                             }
762                         }
763                         $script .= "), array(";
764                         foreach ($right as $rCol) {
765                             $script .= $rCol;
766                             if ($rCol != $right[count($right)]) {
767                                 $script .= ",";
768                             }
769                         }
770                         $script .= "), \$join_behavior);";
771                     }
772                 }
773             } // foreach fkeys
774
775
776     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
777     {
778       $script .= "
779
780     foreach (sfMixer::getCallables('{$this->getClassname()}:doCount:doCount') as \$callable)
781     {
782       call_user_func(\$callable, '{$this->getClassname()}', \$criteria, \$con);
783     }
784
785 ";
786     }
787
788
789             $script .= "
790         \$stmt = ".$this->basePeerClassname."::doCount(\$criteria, \$con);
791
792         if (\$row = \$stmt->fetch(PDO::FETCH_NUM)) {
793             \$count = (int) \$row[0];
794         } else {
795             \$count = 0; // no rows returned; we infer that means 0 matches.
796         }
797         \$stmt->closeCursor();
798         return \$count;
799     }
800 ";
801         } // foreach fk
802
803     } // addDoCountJoinAllExcept
804
805     /**
806      * Adds the doCount() method.
807      * @param      string &$script The script will be modified in this method.
808      */
809     protected function addDoCount(&$script)
810     {
811         $script .= "
812     /**
813      * Returns the number of rows matching criteria.
814      *
815      * @param      Criteria \$criteria
816      * @param      boolean \$distinct Whether to select only distinct columns; deprecated: use Criteria->setDistinct() instead.
817      * @param      PropelPDO \$con
818      * @return     int Number of matching rows.
819      */
820     public static function doCount(Criteria \$criteria, \$distinct = false, PropelPDO \$con = null)
821     {
822         // we may modify criteria, so copy it first
823         \$criteria = clone \$criteria;
824
825         // We need to set the primary table name, since in the case that there are no WHERE columns
826         // it will be impossible for the BasePeer::createSelectSql() method to determine which
827         // tables go into the FROM clause.
828         \$criteria->setPrimaryTableName(".$this->getPeerClassname()."::TABLE_NAME);
829
830         if (\$distinct && !in_array(Criteria::DISTINCT, \$criteria->getSelectModifiers())) {
831             \$criteria->setDistinct();
832         }
833
834         if (!\$criteria->hasSelectClause()) {
835             ".$this->getPeerClassname()."::addSelectColumns(\$criteria);
836         }
837
838         \$criteria->clearOrderByColumns(); // ORDER BY won't ever affect the count
839         \$criteria->setDbName(self::DATABASE_NAME); // Set the correct dbName
840
841         if (\$con === null) {
842             \$con = Propel::getConnection(".$this->getPeerClassname()."::DATABASE_NAME, Propel::CONNECTION_READ);
843         }
844 ";
845     if (DataModelBuilder::getBuildProperty('builderAddBehaviors'))
846     {
847       $script .= "
848
849     foreach (sfMixer::getCallables('{$this->getClassname()}:doCount:doCount') as \$callable)
850     {
851       call_user_func(\$callable, '{$this->getClassname()}', \$criteria, \$con);
852     }
853
854 ";
855     }
856       $script .= "
857         // BasePeer returns a PDOStatement
858         \$stmt = ".$this->basePeerClassname."::doCount(\$criteria, \$con);
859
860         if (\$row = \$stmt->fetch(PDO::FETCH_NUM)) {
861             \$count = (int) \$row[0];
862         } else {
863             \$count = 0; // no rows returned; we infer that means 0 matches.
864         }
865         \$stmt->closeCursor();
866         return \$count;
867     }";
868     }
869
870
871 }
872
Note: See TracBrowser for help on using the browser.