ComplexBuilderDAO.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: ComplexBuilderDAO.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     abstract class ComplexBuilderDAO extends StorableDAO
00017     {
00018         abstract protected function makeSelf(&$array, $prefix = null);
00019         
00023         public function getDefaultStrategyId()
00024         {
00025             return FetchStrategy::JOIN;
00026         }
00027         
00028         public function getMapping()
00029         {
00030             static $protoMappings = array();
00031             
00032             $className = $this->getObjectName();
00033             
00034             if (!isset($protoMappings[$className]))
00035                 $protoMappings[$className] =
00036                     call_user_func(array($className, 'proto'))->
00037                     getMapping();
00038             
00039             return $protoMappings[$className];
00040         }
00041         
00042         public function getFields()
00043         {
00044             static $fields = array();
00045             
00046             $className = $this->getObjectName();
00047             
00048             if (!isset($fields[$className])) {
00049                 foreach (array_values($this->getMapping()) as $field) {
00050                     if (is_array($field))
00051                         $fields[$className] =
00052                             array_merge(
00053                                 $fields[$className],
00054                                 $field
00055                             );
00056                     elseif ($field)
00057                         $fields[$className][] = $field;
00058                 }
00059             }
00060             
00061             return $fields[$className];
00062         }
00063         
00064         public function getJoinPrefix($field, $prefix = null)
00065         {
00066             return $this->getJoinName($field, $prefix).'__';
00067         }
00068         
00069         public function getJoinName($field, $prefix = null)
00070         {
00071             return dechex(crc32($prefix.$this->getTable())).'_'.$field;
00072         }
00073         
00074         public function makeObject(&$array, $prefix = null)
00075         {
00076             return
00077                 $this->makeCascade(
00078                     $this->selfSpawn($array, $prefix),
00079                     $array,
00080                     $prefix
00081                 );
00082         }
00083         
00084         public function makeJoinedObject(&$array, $prefix = null)
00085         {
00086             return
00087                 $this->makeJoiners(
00088                     $this->selfSpawn($array, $prefix),
00089                     $array,
00090                     $prefix
00091                 );
00092         }
00093         
00095 
00096         protected function makeJoiners(
00097             /* Identifiable */ $object, &$array, $prefix = null
00098         )
00099         {
00100             return $object;
00101         }
00102         
00103         protected function makeCascade(
00104             /* Identifiable */ $object, &$array, $prefix = null
00105         )
00106         {
00107             return $object;
00108         }
00110         
00111         private function selfSpawn(&$array, $prefix = null)
00112         {
00113             if (isset($this->identityMap[$array[$prefix.$this->getIdName()]]))
00114                 $object = $this->identityMap[$array[$prefix.$this->getIdName()]];
00115             else {
00116                 $object = $this->makeSelf($array, $prefix);
00117                 $this->identityMap[$object->getId()] = $object;
00118             }
00119             
00120             return $object;
00121         }
00122     }
00123 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4