Changeset 10118
- Timestamp:
- 07/04/08 14:50:48 (5 months ago)
- Files:
-
- plugins/sfPropelFinderPlugin/README (modified) (1 diff)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php (modified) (19 diffs)
- plugins/sfPropelFinderPlugin/lib/sfPropelFinderUtils.php (added)
- plugins/sfPropelFinderPlugin/package.xml (modified) (1 diff)
- plugins/sfPropelFinderPlugin/test/prove.php (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/sfPropelFinderPlugin/README
r10112 r10118 473 473 }}} 474 474 475 == TODO ==475 == TODO / Ideas == 476 476 477 477 * Put as a parent class in the PeerBuilder so that every Peer class can be a finder 478 478 * Merge with sfPropelImpersonatorPlugin! 479 479 * Add support for `with()` in `findPk()` (and document the method) 480 * Add the ability to do left, right, inner joins, etc. 481 * Make `join()` useless if there is an explicit `where()` on the table afterwards 482 * Change `with()` and `join()` to accept only one relation at a time, but with the ability to set the local and foreign parts of the relation. maybe replace the current multiple ability of these methods by `joins()` and `withs()` 483 * Handle complex queries with And and Or 484 * Add a `__toString()` method which returns a var_export() of the results, or a description of the conditions if not yet executed 485 * Implement iterator interface? That way, the query is only executed upon a foreach or an array access... And the finder can be seen as a collection 486 * Column finder, which provides an easy interface to Creole (and PDO) for retrieval of columns instead of objects? 480 487 481 488 == Changelog == 482 489 483 === 2008-07-03 | Trunk === 484 490 === 2008-07-04 | Trunk === 491 492 * francois: Added a `prove.php` test file to launch all tests at once in a test harness 493 * francois: Moved utility methods as static methods of a third-party class to take some weight off the main class 485 494 * francois: Preferring `ClassName.ColumnName` over `ClassName.ColumnName` for complete column names 486 495 * francois: Added Propel 1.3 compatibility plugins/sfPropelFinderPlugin/lib/sfPropelFinder.php
r10112 r10118 423 423 } 424 424 } 425 426 425 427 426 public function doFind($criteria, $con = null) … … 497 496 } 498 497 } 499 $this->relateObjects($withObj, $objectsInJoin, $isNewObject);498 sfPropelFinderUtils::relateObjects($withObj, $objectsInJoin, $isNewObject); 500 499 $objectsInJoin []= $withObj; 501 500 if ($isNewObject) … … 555 554 call_user_func(array($tempClass->getPeer(), 'addSelectColumns'), $c); 556 555 // if join() wasn't called previously on this class, do a simple join 557 if(!in_array( $this->getPeerClassFromClass($className), $this->relations))556 if(!in_array(sfPropelFinderUtils::getPeerClassFromClass($className), $this->relations)) 558 557 { 559 558 list($column1, $column2) = $this->getRelation($className); 560 559 $c->addJoin($column1, $column2); 561 $this->relations[]= $this->getPeerClassFromClass($className);560 $this->relations[]= sfPropelFinderUtils::getPeerClassFromClass($className); 562 561 } 563 562 } … … 570 569 if($peerClass && !in_array($peerClass, $this->relations)) 571 570 { 572 list($column1, $column2) = $this->getRelation( $this->getClassFromPeerClass($peerClass));571 list($column1, $column2) = $this->getRelation(sfPropelFinderUtils::getClassFromPeerClass($peerClass)); 573 572 $c->addJoin($column1, $column2); 574 573 $this->relations[]= $peerClass; … … 578 577 579 578 return $c; 580 }581 582 protected function relateObjects($new, $existingObjects, $isNew)583 {584 // brute force (to be optimized later)585 foreach ($existingObjects as $existingObject)586 {587 $methodName = 'add'.get_class($existingObject);588 if(method_exists($new, $methodName))589 {590 if($isNew)591 {592 call_user_func(array($new, 'init'.get_class($existingObject).'s'));593 }594 call_user_func(array($new, $methodName), $existingObject);595 break;596 }597 }598 579 } 599 580 … … 701 682 array_shift($arguments); 702 683 } 703 list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments);684 list($value, $comparison) = sfPropelFinderUtils::getValueAndComparisonFromArguments($arguments); 704 685 705 686 $this->addCondition('and', $column, $value, $comparison ); … … 728 709 array_shift($arguments); 729 710 } 730 list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments);711 list($value, $comparison) = sfPropelFinderUtils::getValueAndComparisonFromArguments($arguments); 731 712 732 713 $this->addCondition('and', $column, $value, $comparison); … … 755 736 array_shift($arguments); 756 737 } 757 list($value, $comparison) = $this->getValueAndComparisonFromArguments($arguments);738 list($value, $comparison) = sfPropelFinderUtils::getValueAndComparisonFromArguments($arguments); 758 739 $this->addCondition('or', $column, $value, $comparison); 759 740 … … 798 779 } 799 780 800 protected function getValueAndComparisonFromArguments($arguments = array())801 {802 $comparison = Criteria::EQUAL;803 switch (count($arguments))804 {805 case 0:806 $value = true;807 break;808 case 1:809 $value = array_shift($arguments);810 break;811 case 2:812 $comparison = array_shift($arguments);813 $comparisonUp = trim(strtoupper($comparison));814 if(in_array($comparisonUp, array('LIKE', 'NOT LIKE', 'ILIKE', 'NOT ILIKE', 'IN', 'NOT IN', 'IS NULL', 'IS NOT NULL')))815 {816 $comparison = ' '.$comparisonUp.' ';817 }818 $value = array_shift($arguments);819 break;820 default:821 throw new Exception('sfPropelFinder::whereXXX() can only be called with one or two arguments');822 }823 824 return array($value, $comparison);825 }826 827 781 /** 828 782 * Finder fluid method to restrict results to a related object … … 834 788 { 835 789 $relatedObjectTableName = $object->getPeer()->getTableMap()->getName(); 836 foreach ( $this->getColumnsForPeerClass($this->getPeerClass()) as $c)790 foreach (sfPropelFinderUtils::getColumnsForPeerClass($this->getPeerClass()) as $c) 837 791 { 838 792 if($c->getRelatedTableName() == $relatedObjectTableName) … … 912 866 { 913 867 $columnNames = array(); 914 foreach ( $this->getColumnsForPeerClass($this->getPeerClass()) as $c)868 foreach (sfPropelFinderUtils::getColumnsForPeerClass($this->getPeerClass()) as $c) 915 869 { 916 870 $columnNames []= $c->getPhpName(); … … 941 895 { 942 896 list($column1, $column2) = $this->getRelation($relatedClass); 943 $this->relations[]= $this->getPeerClassFromClass($relatedClass);897 $this->relations[]= sfPropelFinderUtils::getPeerClassFromClass($relatedClass); 944 898 if(!is_array($arguments)) 945 899 { … … 968 922 // try to find one to many relationship 969 923 if($relation = $this->findRelation( 970 $this->getClassFromPeerClass($peerClass),971 $this->getPeerClassFromClass($phpName)))924 sfPropelFinderUtils::getClassFromPeerClass($peerClass), 925 sfPropelFinderUtils::getPeerClassFromClass($phpName))) 972 926 { 973 927 return array_reverse($relation); … … 979 933 protected function findRelation($phpName, $peerClass) 980 934 { 981 foreach ( $this->getColumnsForPeerClass($peerClass) as $c)935 foreach (sfPropelFinderUtils::getColumnsForPeerClass($peerClass) as $c) 982 936 { 983 937 if ($c->isForeignKey()) … … 985 939 if(!$this->databaseMap->containsTable($c->getRelatedTableName())) 986 940 { 987 $mapBuilder = call_user_func(array( $this->getPeerClassFromClass($phpName), 'getMapBuilder'));941 $mapBuilder = call_user_func(array(sfPropelFinderUtils::getPeerClassFromClass($phpName), 'getMapBuilder')); 988 942 $mapBuilder->doBuild(); 989 943 } … … 1001 955 } 1002 956 1003 protected function getPeerClassFromClass($class)1004 {1005 $tmp = new $class();1006 return get_class($tmp->getPeer());1007 $tmp->__destroy();1008 }1009 1010 protected function getClassFromPeerClass($peerClass)1011 {1012 $omClass = call_user_func(array($peerClass, 'getOMClass'));1013 return substr('.'.$omClass, strrpos('.'.$omClass, '.') + 1);1014 }1015 1016 957 /** 1017 958 * Behavior-like supplementary getter for supplementary columns added by way of withColumn() … … 1039 980 } 1040 981 1041 protected function getColumnsForPeerClass($peerClass)1042 {1043 if(class_exists($peerClass))1044 {1045 $tableMap = call_user_func(array($peerClass, 'getTableMap'));1046 return $tableMap->getColumns();1047 }1048 return false;1049 }1050 1051 982 protected function getColName($phpName, $peerClass = null, $withPeerClass = false) 1052 983 { … … 1059 990 // Table.Column 1060 991 list($class, $phpName) = explode('.', $phpName); 1061 $peerClass = $this->getPeerClassFromClass($class);992 $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); 1062 993 } 1063 994 else if(strpos($phpName, '_') !== false) … … 1067 998 $class = substr($phpName, 0, $limit); 1068 999 $phpName = substr($phpName, $limit + 1); 1069 $peerClass = $this->getPeerClassFromClass($class);1000 $peerClass = sfPropelFinderUtils::getPeerClassFromClass($class); 1070 1001 } 1071 1002 if(!$peerClass) plugins/sfPropelFinderPlugin/package.xml
r10047 r10118 29 29 <file name="sfPropelFinder.php" role="data"/> 30 30 <file name="sfPropelFinderPager.php" role="data"/> 31 <file name="sfPropelFinderUtils.php" role="data"/> 31 32 </dir> 32 33 </dir>