コード例 #1
0
ファイル: Orm.classes.php プロジェクト: ahri/orm
 /** Takes an SQL result and converts it into a chain of objects (returning an OrmChainResult) **/
 protected static final function objectsFromResult($name, $result, $chain)
 {
     $chain_result = new OrmChainResult(&$chain);
     foreach (SSql::getResultsFor($result) as $row) {
         # by row
         $row_class_vars = array();
         $row_rship_vars = array();
         foreach ($row as $key => $val) {
             # by col
             switch (sizeof($parts = explode('__', $key))) {
                 case 3:
                     # result_of, iterable, property -- i.e. populate a relationship
                     list($r_alias, $i, $r_property) = $parts;
                     $r_alias = self::dbToClassName($r_alias);
                     $r_property = self::dbToPropertyName($r_property);
                     $key = sprintf('%s__%s', Orm::dbToClassName($r_alias), $i);
                     if (!isset($row_rship_vars[$key])) {
                         $row_rship_vars[$key] = array();
                     }
                     $row_rship_vars[$key][$r_property] = $val;
                     break;
                 case 4:
                     # result_of, iterable, class, property -- i.e. populate a class
                     list($r_alias, $i, $r_class, $r_property) = $parts;
                     $r_class = self::dbToClassName($r_class);
                     $r_alias = self::dbToClassName($r_alias);
                     $r_property = self::dbToPropertyName($r_property);
                     $key = sprintf('%s__%s__%s', Orm::dbToClassName($r_alias), $i, Orm::dbToClassName($r_class));
                     if (!isset($row_class_vars[$key])) {
                         $row_class_vars[$key] = array();
                     }
                     $row_class_vars[$key][$r_property] = $val;
                     break;
                 case 6:
                     # child_result_of, iterable, child, "inherits", class, property -- i.e. populate the rest of a class
                     list($r_child_alias, $i, $r_child, $inherits_e_inherits, $r_class, $r_property) = $parts;
                     $r_class = self::dbToClassName($r_class);
                     $inherits_e_inherits = self::dbToClassName($inherits_e_inherits);
                     $r_child = self::dbToClassName($r_child);
                     $r_child_alias = self::dbToClassName($r_child_alias);
                     $r_property = self::dbToPropertyName($r_property);
                     if ($inherits_e_inherits != self::RELATIONSHIP_INHERITS) {
                         throw new OrmException('Not expecting key of form %s', $key);
                     }
                     $key = sprintf('%s__%s__%s', Orm::dbToClassName($r_child_alias), $i, Orm::dbToClassName($r_child));
                     if (!isset($row_class_vars[$key])) {
                         $row_class_vars[$key] = array();
                     }
                     if ($r_property == self::AUTO_PROPERTY_ID) {
                         $row_class_vars[$key][sprintf('__%s__%s', $r_class, self::AUTO_PROPERTY_ID)] = $val;
                     } else {
                         $row_class_vars[$key][$r_property] = $val;
                     }
                     break;
                 default:
                     throw new OrmException('Unexpected key encountered: %s', print_r($parts, true));
             }
         }
         $row_class_objs = array();
         $row_rship_objs = array();
         foreach ($row_class_vars as $alias__class => $vars) {
             list($alias, $i, $class) = explode('__', $alias__class);
             if (!self::isRegisteredClass($name, $class)) {
                 throw new OrmException('Class %s is not registered for Schema %s', $class, $name);
             }
             $obj = new $class(OrmClass::CONSTR_CMD_POPULATE_SYNCED_ARRAY, $name, $vars);
             $row_class_objs[sprintf('%s__%s', $alias, $i)] = $obj;
         }
         foreach ($row_rship_vars as $relationship => $vars) {
             list($class, $i) = explode('__', $relationship);
             if (!self::isRegisteredIRelationship($name, $class)) {
                 throw new OrmException('Relationship %s is not registered or is not instanciable for Schema %s', $class, $name);
             }
             $obj = new $class($vars, $name);
             /*$obj->setLoadedFromDb();*/
             # TODO: WORKING_ON -- this should set each class it's loading
             $row_rship_objs[$relationship] = $obj;
         }
         $chain_result->add(new OrmChainRow(&$chain, $row_class_objs, $row_rship_objs));
     }
     return $chain_result->finalize();
 }