示例#1
0
 /**
  * Collection initializer
  *
  * @param array $from Array of model objects of the same class
  * @param string $class Optional classname of the desired objects
  * @param string $class Optional column name of the primary key
  *
  * @return DbFinder a finder object
  * @throws Exception If the array is empty, contains not model objects or composite objects
  */
 public static function fromCollection($collection, $class = '', $pkName = '')
 {
     if (count($collection) == 0) {
         if (!$class) {
             throw new Exception('A DbFinder cannot be initialized with an empty array and no class');
         } else {
             return self::fromClass($class);
         }
     }
     $testObject = $collection[0];
     if ($testObject instanceof BaseObject) {
         $finder = sfPropelFinder::fromCollection($collection, $class, $pkName);
     } else {
         if ($testObject instanceof Doctrine_Record) {
             $finder = sfDoctrineFinder::fromCollection($collection, $class, $pkName);
         } else {
             throw new Exception('A DbFinder can only be initialized from an array of Propel or Doctrine objects');
         }
     }
     if (class_exists($finderClass = get_class($testObject) . 'Finder')) {
         $dbFinder = new $finderClass();
         $dbFinder->setQueryObject($finder->getQueryObject());
     } else {
         $me = __CLASS__;
         $dbFinder = new $me($finder);
     }
     return $dbFinder;
 }