Development

#3883: sfPropelFinder.php

You must first sign up to be able to contribute.

Ticket #3883: sfPropelFinder.php

File sfPropelFinder.php, 25.9 kB (added by Richtermeister, 1 year ago)

sfPropelFinder class that supports phpName attribute on tables

Line 
1 <?php
2
3 /*
4  * This file is part of the sfPropelFinder package.
5  *
6  * (c) 2007 François Zaninotto <francois.zaninotto@symfony-project.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11  
12 class sfPropelFinder
13 {
14   protected $peerClass = null;
15   protected $databaseMap = null;
16   protected $criteria = null;
17   protected $relations = null;
18   protected $latestQuery = '';
19   protected $criterions = array();
20   protected $withClasses = array();
21   protected $withColumns = array();
22
23   public function __construct($class = '')
24   {
25     $this->criteria = new Criteria();
26     if($class)
27     {
28       $this->setPeerClass($class.'Peer');
29     }
30     if($this->peerClass)
31     {
32       $this->initialize();
33     }
34   }
35  
36   public function initialize()
37   {
38     $mapBuilder = call_user_func(array($this->peerClass, 'getMapBuilder'));
39     $mapBuilder->doBuild();
40     $this->databaseMap = $mapBuilder->getDatabaseMap();
41   }
42  
43   public function getPeerClass()
44   {
45     return $this->peerClass;
46   }
47
48   public function setPeerClass($peerClass)
49   {
50     $this->relations[]= $peerClass;
51     $this->peerClass = $peerClass;
52     
53     return $this;
54   }
55  
56   public function getCriteria()
57   {
58     return $this->buildCriteria();
59   }
60  
61   public function setCriteria($criteria)
62   {
63     $this->criteria = $criteria;
64     $this->criterions = array();
65     
66     return $this;
67   }
68
69   public function reinitCriteria()
70   {
71     return $this->setCriteria(new Criteria());
72   }
73  
74   public function getLatestQuery()
75   {
76     $con = Propel::getConnection();
77     if(method_exists($con, 'getLastExecutedQuery'))
78     {
79       return $this->latestQuery;
80     }
81     else
82     {
83       throw new Exception('getLatestQuery() only works when debug mode is enabled');
84     }
85   }
86  
87   public function updateLatestQuery()
88   {
89     $con = Propel::getConnection();
90     if(method_exists($con, 'getLastExecutedQuery'))
91     {
92       $this->latestQuery = $con->getLastExecutedQuery();
93     }
94   }
95  
96   public function addWithClass($class)
97   {
98     $this->withClasses []= $class;
99     
100     return $this;
101   }
102  
103   public function getWithClasses()
104   {
105     return $this->withClasses;
106   }
107  
108   public function reinitWithClasses()
109   {
110     $this->withClasses = array();
111     
112     return $this;
113   }
114
115   public function getWithColumns()
116   {
117     return $this->withColumns;
118   }
119  
120   public function reinitWithColumns()
121   {
122     $this->withColumns = array();
123     
124     return $this;
125   }
126  
127   // Finder Initializers
128  
129   /**
130    * Mixed initializer
131    * Accepts either a string (Propel class) or an array of Propel objects
132    *
133    * @param mixed $from The data to initialize the finder with
134    * @return sfPropelFinder a finder object
135    * @throws Exception If the data is neither a classname nor an array
136    */
137   public static function from($from)
138   {
139     if (is_string($from))
140     {
141       return self::fromClass($from);
142     }
143     if (is_array($from))
144     {
145       return self::fromCollection($from);
146     }
147     throw new Exception('from() only accepts a Propel object classname or an array of Propel objects');
148   }
149
150   /**
151    * Class initializer
152    *
153    * @param string $from Propel classname on which the search will be done
154    * @return sfPropelFinder a finder object
155    */
156   public static function fromClass($class)
157   {
158     $me = __CLASS__;
159     $finder = new $me($class);
160     
161     return $finder;
162   }
163  
164   /**
165    * Collection initializer
166    *
167    * @param array $from Array of Propel objects of the same class
168    * @param string $class Optional classname of the desired objects
169    * @param string $class Optional column name of the primary key
170    *
171    * @return sfPropelFinder a finder object
172    * @throws Exception If the array is empty, contains not Propel objects or composite objects
173    */
174   public static function fromCollection($collection, $class = '', $pkName = '')
175   {
176     $pks = array();
177     foreach($collection as $object)
178     {
179       if($class != get_class($object))
180       {
181         if($class)
182         {
183           throw new Exception('A finder can only be initialized from an array of objects of a single class');
184         }
185         if($object instanceof BaseObject)
186         {
187           $class = get_class($object);
188         }
189         else
190         {
191           throw new Exception('A finder can only be initialized from an array of Propel objects');
192         }
193       }
194       $pks []= $object->getPrimaryKey();
195     }
196     if(!$class)
197     {
198       throw new Exception('A finder cannot be initialized with an empty array');
199     }
200     
201     $tempObject = new $class();
202     foreach ($tempObject->getPeer()->getTableMap()->getColumns() as $column)
203     {
204       if($column->isPrimaryKey())
205       {
206         if($pkName)
207         {
208           throw new Exception('A finder cannot be initialized from an array of objects with several foreign keys');
209         }
210         else
211         {
212           $pkName = $column->getFullyQualifiedName();
213         }
214       }
215     }
216     
217     return self::fromArray($pks, $class, $pkName);
218   }
219  
220   /**
221    * Array initializer
222    *
223    * @param array $array Array of Primary keys
224    * @param string $class Propel classname on which the search will be done
225    *
226    * @return sfPropelFinder a finder object
227    */
228   public static function fromArray($array, $class, $pkName)
229   {
230     $finder = self::fromClass($class);
231     $finder->add($pkName, $array, Criteria::IN);
232     
233     return $finder;
234   }
235  
236   // Finder Executers
237  
238   public function count($con = null, $reinitCriteria = true)
239   {
240     $ret = call_user_func(array($this->getPeerClass(), 'doCount'), $this->getCriteria(), $con);
241     $this->updateLatestQuery();
242     if($reinitCriteria)
243     {
244       $this->reinitCriteria();
245     }
246     
247     return $ret;
248   }
249  
250   public function find($limit = null, $con = null, $reinitCriteria = false)
251   {
252     if($limit)
253     {
254       $this->criteria->setLimit($limit);
255     }
256     $ret = $this->doFind($this->getCriteria(), $con);
257     $this->updateLatestQuery();
258     if($reinitCriteria)
259     {
260       $this->reinitCriteria();
261     }
262     
263     return $ret;
264   }
265  
266   public function findOne($con = null, $reinitCriteria = true)
267   {
268     $this->criteria->setLimit(1);
269     $ret = $this->doFind($this->getCriteria(), $con);
270     $this->updateLatestQuery();
271     if($reinitCriteria)
272     {
273       $this->reinitCriteria();
274     }
275     if ($ret)
276     {
277       return $ret[0];
278     }
279     return null;
280   }
281  
282   public function findLast($column = null, $con = null, $reinitCriteria = true)
283   {
284     if($column)
285     {
286       $this->orderBy($column, 'desc');
287     }
288     else
289     {
290       $this->guessOrder('desc');
291     }
292     
293     return $this->findOne($con, $reinitCriteria);
294   }
295  
296   public function findFirst($column = null, $con = null, $reinitCriteria = true)
297   {
298     if($column)
299     {
300       $this->orderBy($column, 'asc');
301     }
302     else
303     {
304       $this->guessOrder('asc');
305     }
306     
307     return $this->findOne($con, $reinitCriteria);
308   }
309
310   protected function guessOrder($direction = 'desc')
311   {
312     $columnNames = array();
313     foreach ($this->getColumnsForPeerClass($this->getPeerClass()) as $c)
314     {
315       $columnNames []= $c->getPhpName();
316     }
317     foreach(sfConfig::get('app_sfPropelFinder_sort_column_guesses', array('UpdatedAt', 'UpdatedOn', 'CreatedAt', 'CreatedOn', 'Id')) as $testColumnName)
318     {
319       if(in_array($testColumnName, $columnNames))
320       {
321         $this->orderBy($testColumnName, $direction);
322         return;
323       }
324     }
325     
326     throw new Exception('Unable to figure out the column to use to order rows');
327   }
328  
329   public function findBy($columnName, $value, $limit = null, $con = null, $reinitCriteria = false)
330   {
331     $column = $this->getColName($columnName);
332     $this->addCondition('and', $column, $value, Criteria::EQUAL);
333     
334     return $this->find($limit, $con, $reinitCriteria);
335   }
336
337   public function findOneBy($columnName, $value, $con = null, $reinitCriteria = false)
338   {
339     $column = $this->getColName($columnName);
340     $this->addCondition('and', $column, $value, Criteria::EQUAL);
341     
342     return $this->findOne($con, $reinitCriteria);
343   }
344  
345   public function findPk($pk, $con = null)
346   {
347     if(is_array($pk))
348     {
349       $ret = call_user_func(array($this->getPeerClass(), 'retrieveByPks'), $pk, $con);
350     }
351     else
352     {
353       $ret = call_user_func(array($this->getPeerClass(), 'retrieveByPk'), $pk, $con);
354     }
355     $this->updateLatestQuery();
356     
357     return $ret;
358   }
359  
360   public function doFind($criteria, $con = null)
361   {
362     if($this->getWithClasses() || $this->getWithColumns())
363     {
364       $c = $this->prepareCompositeCriteria($criteria);
365       $resultSet = call_user_func(array($this->getPeerClass(), 'doSelectRS'), $c, $con);
366       
367       // Hydrate the objects based on the resultset
368       $omClass = call_user_func(array($this->getPeerClass(), 'getOMClass'));
369       $cls = Propel::import($omClass);
370       $objects = array();
371       $withObjs = array();
372       while ($resultSet->next())
373       {
374         // First come the columns of the main class
375         $obj = new $cls();
376         $startCol = $obj->hydrate($resultSet, 1);
377         // Then the related classes added by way of 'with'
378         $objectsInJoin = array($obj);
379         foreach ($this->getWithClasses() as $className)
380         {
381           $withObj = new $className();
382           $startCol = $withObj->hydrate($resultSet, $startCol);
383           
384           // initialize our object directory
385           if (!isset($withObjs[$className]))
386           {
387             $withObjs[$className] = array();
388           }
389           
390           // check if object is not already referenced in allObjects directory
391           $isNewObject = true;
392           foreach ($withObjs[$className] as $otherObject)
393           {
394             if ($otherObject->getPrimaryKey() === $withObj->getPrimaryKey())
395             {
396               $isNewObject = false;
397               $withObj = $otherObject;
398               break;
399             }
400           }
401           $this->relateObjects($withObj, $objectsInJoin, $isNewObject);
402           $objectsInJoin []= $withObj;
403           if ($isNewObject)
404           {
405             $withObjs[$className][] = $withObj;
406           }
407         }
408         // Then the columns added one by one by way of 'withColumn'
409         foreach($this->getWithColumns() as $name => $column)
410         {
411           // Additional columns are stored in the object, in a special 'namespace'
412           // see getColumn() for how to retrieve the value afterwards
413           $asName = 'WithColumn'.$name;
414           // Using the third parameter of withColumn() as a type. defaults to $rs->get() (= $rs->getString())
415           $typedGetter = 'get'.ucfirst($column['type']);
416           $obj->$asName = $resultSet->$typedGetter($startCol);
417           $startCol++;
418         }
419         
420         $objects []= $obj;
421       }
422       
423       // activate custom column getter if asColumns were added
424       if($this->getWithColumns() && !sfMixer::getCallable('Base'.$cls.':getColumn'))
425       {
426         sfMixer::register('Base'.$cls, array($this, 'getColumn'));
427       }
428       
429       return $objects;
430     }
431     else
432     {
433       // No 'with', so we use the native Propel doSelect()
434       return call_user_func(array($this->getPeerClass(), 'doSelect'), $criteria, $con);
435     }
436   }
437  
438   /**
439    * Prepare the select columns and add the missing joins
440    */
441   protected function prepareCompositeCriteria($criteria)
442   {
443     $c = clone $criteria;
444     $c->clearSelectColumns();
445     // First come the columns of the main class
446     call_user_func(array($this->getPeerClass(), 'addSelectColumns'), $c);
447     // Then the related classes added by way of 'with'
448     foreach ($this->getWithClasses() as $className)
449     {
450       $tempClass = new $className();
451       call_user_func(array($tempClass->getPeer(), 'addSelectColumns'), $c);
452       // if join() wasn't called previously on this class, do a simple join
453       if(!in_array($className.'Peer', $this->relations))
454       {
455         list($column1, $column2) = $this->getRelation($className);
456         $c->addJoin($column1, $column2);
457         $this->relations[]= $className.'Peer';
458       }
459     }
460     // Then the columns added one by one by way of 'withColumn'
461     foreach($this->getWithColumns() as $name => $column)
462     {
463       // if the column is on a related object property
464       // and if join() wasn't called previously on this object, do a simple join
465       $peerClass = $column['peerClass'];
466       if($peerClass && !in_array($peerClass, $this->relations))
467       {
468         list($column1, $column2) = $this->getRelation(str_replace('Peer', '', $peerClass));
469         $c->addJoin($column1, $column2);
470         $this->relations[]= $peerClass;
471       }
472       $c->addAsColumn($name, $column['column']);
473     }
474     
475     return $c;
476   }
477  
478   protected function relateObjects($new, $existingObjects, $isNew)
479   {
480     // brute force (to be optimized later)
481     foreach ($existingObjects as $existingObject)
482     {
483       $methodName = 'add'.get_class($existingObject);
484       if(method_exists($new, $methodName))
485       {
486         if($isNew)
487         {
488           call_user_func(array($new, 'init'.get_class($existingObject).'s'));
489         }
490         call_user_func(array($new, $methodName), $existingObject);
491         break;
492       }
493     }
494   }
495  
496   public function delete($con = null, $reinitCriteria = true)
497   {
498     $deleteCriteria = $this->getCriteria();
499     if($deleteCriteria->equals(new Criteria()))
500     {
501       // delete will delete nothing when passed an empty criteria
502       // while it should, in fact, delete all
503       $fieldNames = call_user_func(array($this->getPeerClass(), 'getFieldNames'), BasePeer::TYPE_COLNAME);
504       $firstFieldName = $fieldNames[0];
505       $deleteCriteria->add($firstFieldName, true, Criteria::BINARY_OR);
506     }
507     $ret = call_user_func(array($this->getPeerClass(), 'doDelete'), $deleteCriteria, $con);
508     $this->updateLatestQuery();
509     if($reinitCriteria)
510     {
511       $this->reinitCriteria();
512     }
513     
514     return $ret;
515   }
516  
517   // Hydrating
518  
519   public function with($classes)
520   {
521     if(!is_array($classes))
522     {
523       $classes = func_get_args();
524     }
525     foreach($classes as $class)
526     {
527       $this->addWithClass($class);
528     }
529     
530     return $this;
531   }
532
533   public function withColumn($column, $alias = null, $type = null)
534   {
535     $isCalculationColumn = strpos($column, '(') !== false;
536     if(!$alias)
537     {
538       if($isCalculationColumn)
539       {
540         throw new Exception('Calculated colums added with withColumn() need an alias as second parameter');
541       }
542       else
543       {
544         $alias = $column;
545       }
546     }
547     if($isCalculationColumn)
548     {
549       $peerClass = null;
550     }
551     else
552     {
553       list($peerClass, $columnName) = $this->getColName($column, null, true);
554     }
555     $this->withColumns [$alias]= array(
556       'column'    => $isCalculationColumn ? $column : $columnName,
557       'type'      => $type,
558       'peerClass' => $peerClass
559     );
560     
561     return $this;
562   }
563  
564   // Finder Filters
565  
566   /**
567    * Finder Fluid Interface for Criteria::setDistinct()
568    */
569   public function distinct()
570   {
571     $this->criteria->setDistinct();
572     
573     return $this;
574   }
575  
576   /**
577    * Finder Fluid Interface for Criteria::add()
578    * Infers $column, $value, $comparison from $columnName and some optional arguments
579    * Examples:
580    *   $articleFinder->where('IsPublished')
581    *    => $c->add(ArticlePeer::IS_PUBLISHED, true)
582    *   $articleFinder->where('CommentId', 3)
583    *    => $c->add(ArticlePeer::COMMENT_ID, 3)
584    *   $articleFinder->where('Title', 'like', '%foo')
585    *    => $c->add(ArticlePeer::TITLE, '%foo', Criteria::LIKE)
586    *
587    * @param      string  $columnName PHPName of the column bearing the condition
588    * @param      array   $arguments  Optional array of arguments
589    *
590    * @return     sfPropelFinder the current finder object
591    */
592   public function where($columnName, $arguments = array())
593   {
594     $column = $this->getColName($columnName);
595     if(!is_array($arguments))
596     {
597       $arguments = func_get_args();
598       array_shift($arguments);
599     }
600     list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments);
601
602     $this->addCondition('and', $column, $value, $comparison );
603     
604     return $this;
605   }
606
607   /**
608    * Finder Fluid Interface for Criteria::addAnd()
609    * Infers $column, $value, $comparison from $columnName and some optional arguments
610    * Examples:
611    *   $articleFinder->_and('CommentId', 3)
612    *    => $c->addAnd(ArticlePeer::COMMENT_ID, 3)
613    *
614    * @param      string  $columnName PHPName of the column bearing the condition
615    * @param      array   $arguments  Optional array of arguments
616    *
617    * @return     sfPropelFinder the current finder object
618    */
619   public function _and($columnName, $arguments = array())
620   {
621     $column = $this->getColName($columnName);
622     if(!is_array($arguments))
623     {
624       $arguments = func_get_args();
625       array_shift($arguments);
626     }
627     list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments);
628
629     $this->addCondition('and', $column, $value, $comparison);
630     
631     return $this;
632   }
633
634   /**
635    * Finder Fluid Interface for Criteria::addOr()
636    * Infers $column, $value, $comparison from $columnName and some optional arguments
637    * Examples:
638    *   $articleFinder->_or('CommentId', 3)
639    *    => $c->addOr(ArticlePeer::COMMENT_ID, 3)
640    *
641    * @param      string  $columnName PHPName of the column bearing the condition
642    * @param      array   $arguments  Optional array of arguments
643    *
644    * @return     sfPropelFinder the current finder object
645    */
646   public function _or($columnName, $arguments = array())
647   {
648     $column = $this->getColName($columnName);
649     if(!is_array($arguments))
650     {
651       $arguments = func_get_args();
652       array_shift($arguments);
653     }
654     list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments);
655     $this->addCondition('or', $column, $value, $comparison);
656     
657     return $this;
658   }
659
660   /**
661    * Conditions have to be stocked before being really used
662    * cf. sfPropelFinder::buildCriteria()
663    */
664   protected function addCondition($cond, $column, $value, $comparison)
665   {
666     $criterion = $this->criteria->getNewCriterion($column, $value, $comparison);
667     $criterion->func = "add".$cond;
668     $this->criterions []= $criterion;
669   }
670
671   /**
672    * We want that the Finder fuild Interface works like:
673    *   PHP : whereA()->_andB->_orC()->_orD()->_andE()
674    *   SQL : where A=? AND (B=? OR (C=? OR (D=? AND E=?)))
675    * So we have to add condition starting by the last one!
676    */
677   protected function buildCriteria()
678   {
679     $criteria = clone $this->criteria;
680     $criterions = $this->criterions;
681
682     while ($criterion = array_pop($criterions))
683     {
684       if ($c = count($criterions))
685       {
686         call_user_func(array($criterions[$c-1], $criterion->func), $criterion);
687       }
688       else
689       {
690         call_user_func(array($criteria, $criterion->func), $criterion);
691       }
692     }
693     
694     return $criteria;
695   }
696  
697   protected function getValueAndComparisonFromArguments($arguments = array())
698   {
699     $comparison = Criteria::EQUAL;
700     switch (count($arguments))
701     {
702       case 0:
703         $value = true;
704         break;
705       case 1:
706         $value = array_shift($arguments);
707         break;
708       case 2:
709         $comparison = array_shift($arguments);
710         $comparisonUp = trim(strtoupper($comparison));
711         if(in_array($comparisonUp, array('LIKE', 'NOT LIKE', 'ILIKE', 'NOT ILIKE', 'IN', 'NOT IN', 'IS NULL', 'IS NOT NULL')))
712         {
713           $comparison = ' '.$comparisonUp.' ';
714         }
715         $value = array_shift($arguments);
716         break;
717       default:
718         throw new Exception('{sfPropelFinder} whereXXX can only be called with one or two arguments');
719     }
720
721     return array($value, $comparison);
722   }
723  
724   /**
725    * Finder fluid method to restrict results to a related object
726    * Examples:
727    *   $commentFinder->relatedTo($article)
728    *    => $c->add(CommentPeer::ARTICLE_ID, $article->getId())
729    */
730   public function relatedTo($object)
731   {
732     $relatedObjectTableName = $object->getPeer()->getTableMap()->getName();
733     foreach ($this->getColumnsForPeerClass($this->getPeerClass()) as $c)
734     {
735       if($c->getRelatedTableName() == $relatedObjectTableName)
736       {
737         $this->addCondition('and', $c->getFullyQualifiedName(), $object->getByName($c->getRelatedName(), BasePeer::TYPE_COLNAME), Criteria::EQUAL);
738       }
739     }
740     
741     return $this;
742   }
743  
744   /**
745    * Finder Fluid Interface for Criteria::addAscendingOrderByColumn()
746    * and Criteria::addDescendingOrderByColumn()
747    * Infers $column and $order from $columnName and some optional arguments
748    * Examples:
749    *   $articleFinder->orderBy('CreatedAt')
750    *    => $c->addAscendingOrderByColumn(ArticlePeer::CREATED_AT)
751    *   $articlefinder->orderBy('CategoryId', 'desc')
752    *    => $c->addDescendingOrderByColumn(ArticlePeer::CATEGORY_ID)
753    */
754   public function orderBy($columnName, $arguments = array())
755   {
756     $column = $this->getColName($columnName);
757     if(!is_array($arguments))
758     {
759       $arguments = func_get_args();
760       array_shift($arguments);
761     }
762     $order = strtoupper(array_shift($arguments));
763     if(!$order)
764     {
765       $order = Criteria::ASC;
766     }
767     
768     switch ($order)
769     {
770       case Criteria::ASC:
771         $this->criteria->addAscendingOrderByColumn($column);
772         break;
773       case Criteria::DESC:
774         $this->criteria->addDescendingOrderByColumn($column);
775         break;
776       default:
777         throw new Exception('{sfPropelFinder} orderBy only accepts "asc" or "desc" as argument');
778     }
779     
780     return $this;
781   }
782  
783   /**
784    * Finder Fluid Interface for Criteria::addJoin()
785    * Infers $column1, $column2 and $operator from $relatedClass and some optional arguments
786    * Uses the Propel column maps, based on the schema, to guess the related columns
787    * Examples:
788    *   $articleFinder->join('Comment')
789    *    => $c->addJoin(ArticlePeer::ID, CommentPeer::ARTICLE_ID)
790    *   $articleFinder->join('Category', 'RIGHT JOIN')
791    *    => $c->addJoin(ArticlePeer::CATEGORY_ID, CategoryPeer::ID, Criteria::RIGHT_JOIN)
792    */
793   public function join($relatedClass, $arguments = array())
794   {
795     list($column1, $column2) = $this->getRelation($relatedClass);
796     $this->relations[]= $relatedClass.'Peer';
797     if(!is_array($arguments))
798     {
799       $arguments = func_get_args();
800       array_shift($arguments);
801     }
802     $operator = array_shift($arguments);
803     if(!$operator)
804     {
805       $operator = null;
806     }
807     $this->criteria->addJoin($column1, $column2, $operator);
808     
809     return $this;
810   }
811
812   public function getRelation($phpName)
813   {
814     foreach($this->relations as $peerClass)
815     {
816       // try to find many to one or one to one relationship
817       if($relation = $this->findRelation($phpName, $peerClass))
818       {
819         return $relation;
820       }
821       // try to find one to many relationship
822       if($relation = $this->findRelation(str_replace('Peer', '', $peerClass), $phpName.'Peer'))
823       {
824         return array_reverse($relation);
825       }
826     }
827     throw new Exception(sprintf('{sfPropelFinder} %s has no %s related table', $this->peerClass, $phpName));
828   }
829  
830   protected function findRelation($phpName, $peerClass)
831   {
832     foreach ($this->getColumnsForPeerClass($peerClass) as $c)
833     {
834       if ($c->isForeignKey())
835       {
836         
837         $related_tablename = $c ->getRelatedTableName();
838         $compare_name = $phpName;
839         
840         if( $c -> getTable() -> getDatabaseMap() -> containsTable($related_tablename) ) {
841             $table = $c -> getTable() -> getDatabaseMap() -> getTable($related_tablename);
842             $compare_name = sfInflector::camelize($table -> getName());
843         }
844         
845         if(sfInflector::camelize($c->getRelatedTableName()) == $compare_name)
846         {
847           return array(
848             constant($peerClass.'::'.$c->getColumnName()),
849             $c->getRelatedName()
850           );
851         }
852       }
853     }
854     
855     return false;
856   }
857  
858   /**
859    * Behavior-like supplementary getter for supplementary columns added by way of withColumn()
860    * Requires symfony and sfMixer enabled
861    */
862   public function getColumn($object, $arguments)
863   {
864     $asName = 'WithColumn'.$arguments;
865
866     return $object->$asName;
867   }
868  
869  
870   protected function getColumnsForPeerClass($peerClass)
871   {
872     if(class_exists($peerClass))
873     {
874       $tableMap = call_user_func(array($peerClass, 'getTableMap'));
875       return $tableMap->getColumns();
876     }
877     return false;
878   }
879  
880   protected function getColName($phpName, $peerClass = null, $withPeerClass = false)
881   {
882     if(array_key_exists($phpName, $this->withColumns))
883     {
884       return $phpName;
885     }
886     if(strpos($phpName, '_') !== false)
887     {
888       list($class, $phpName) = split('_', $phpName);
889       $peerClass = $class.'Peer';
890     }
891     if(!$peerClass)
892     {
893       $peerClass = $this->peerClass;
894     }
895     try
896     {
897       $column = call_user_func(array($peerClass, 'translateFieldName'), $phpName, BasePeer::TYPE_PHPNAME, BasePeer::TYPE_COLNAME);
898       return $withPeerClass ? array($peerClass, $column) : $column;
899     }
900     catch (PropelException $e)
901     {
902       throw new Exception(sprintf('{sfPropelFinder} %s has no %s column', $peerClass, $phpName));
903     }
904   }
905  
906   public function __call($name, $arguments)
907   {
908     if(strpos($name, 'where') === 0)
909     {
910       return $this->where(substr($name, 5), $arguments);
911     }
912     if(strpos($name, 'orderBy') === 0)
913     {
914       return $this->orderBy(substr($name, 7), $arguments);
915     }
916     if(strpos($name, 'join') === 0)
917     {
918       return $this->join(substr($name, 4), $arguments);
919     }
920     if(strpos($name, '_and') === 0)
921     {
922       return $this->_and(substr($name, 4), $arguments);
923     }
924     if(strpos($name, 'and') === 0)
925     {
926       return $this->_and(substr($name, 3), $arguments);
927     }
928     if(strpos($name, '_or') === 0)
929     {
930       return $this->_or(substr($name, 3), $arguments);
931     }
932     if(strpos($name, 'or') === 0)
933     {
934       return $this->_or(substr($name, 2), $arguments);
935     }
936     if(strpos($name, 'findBy') === 0)
937     {
938       array_unshift($arguments, substr($name, 6));
939       return call_user_func_array(array($this, 'findBy'), $arguments);
940     }
941     if(strpos($name, 'findOneBy') === 0)
942     {
943       array_unshift($arguments, substr($name, 9));
944       return call_user_func_array(array($this, 'findOneBy'), $arguments);
945     }
946     if(method_exists($this->criteria, $name))
947     {
948       call_user_func_array(array($this->criteria, $name), $arguments);
949       return $this;
950     }
951     throw new Exception(sprintf('{sfPropelFinder} Undefined method %s', $name));
952   }
953 }

The Sensio Labs Network

Since 1998, Sensio Labs has been promoting the Open-Source software movement by providing quality web application development, training, consulting, and supporting several large Open-Source projects.