Development

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

You must first sign up to be able to contribute.

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

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