Changeset 418

Show
Ignore:
Timestamp:
10/21/07 20:32:08 (1 year ago)
Author:
kaste
Message:

AkHasAndBelongsToMany? now uses AkInstaller? to create the join table. Because of that it creates the sequence_table for sqlite straight away. refactored ->_loadJoinObject

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/kaste/framework/lib/AkActiveRecord/AkHasAndBelongsToMany.php

    r415 r418  
    174174        $Collection->association_id = $association_id; 
    175175 
    176         $Collection->_loadJoinObject() ? null : trigger_error(Ak::t('Could find join model %model_name for hasAndBelongsToMany association %id',array('%table_name'=>$options['join_class_name'],'id'=>$this->association_id)),E_USER_ERROR)
     176        $Collection->_loadJoinObject()
    177177 
    178178        return $Collection; 
     
    217217        $options = $this->getOptions($this->association_id); 
    218218 
    219         $join_model_file = AkInflector::toModelFilename($options['join_class_name']); 
    220         if(file_exists($join_model_file)){ 
    221             require_once($join_model_file); 
    222             if(class_exists($options['join_class_name'])){ 
    223                 $this->JoinObject =& new $options['join_class_name'](); 
     219        if (class_exists($options['join_class_name']) || $this->_loadJoinClass($options['join_class_name']) || $this->_createJoinClass()) { 
     220            $this->JoinObject =& new $options['join_class_name'](); 
     221            //if($this->JoinObject->setTableName($options['join_table'], true, true) || $this->_createJoinTable()){ 
     222            if($this->_tableExists($options['join_table']) || $this->_createJoinTable()){ 
    224223                $this->JoinObject->setPrimaryKey($options['foreign_key']); 
    225224                return true; 
    226             } 
    227         } 
    228         if($this->_createJoinClass()){ 
    229             $this->JoinObject =& new $options['join_class_name'](); 
    230             if(!$this->_hasJoinTable()){ 
    231                 $this->_createJoinTable() ? null : trigger_error(Ak::t('Could not find join table %table_name for hasAndBelongsToMany association %id',array('%table_name'=>$options['join_table'],'id'=>$this->association_id)),E_USER_ERROR); 
    232             } 
    233             $this->JoinObject->setPrimaryKey($options['foreign_key']); 
    234             return true; 
    235         } 
     225                 
     226            } else trigger_error(Ak::t('Could not find join table %table_name for hasAndBelongsToMany association %id',array('%table_name'=>$options['join_table'],'id'=>$this->association_id)),E_USER_ERROR); 
     227        } else     trigger_error(Ak::t('Could not find join model %model_name for hasAndBelongsToMany association %id',array('%table_name'=>$options['join_class_name'],'id'=>$this->association_id)),E_USER_ERROR); return false; 
    236228        return false; 
    237229    } 
     
    242234        return !empty($this->JoinObject) && (strtolower($options['join_class_name']) == strtolower(get_class($this->JoinObject))); 
    243235    } 
     236     
     237    function _loadJoinClass($class_name) 
     238    { 
     239        $model_file = AkInflector::toModelFilename($class_name); 
     240        return file_exists($model_file) && require_once($model_file); 
     241    } 
     242     
    244243    function _createJoinClass() 
    245244    { 
    246245        $options = $this->getOptions($this->association_id); 
    247         if(!class_exists($options['join_class_name'])){ 
    248             $class_file_code = "<?php \n\n//This code was generated automatically by the active record hasAndBelongsToMany Method\n\n"; 
    249             $class_code = 
    250             "class {$options['join_class_name']} extends {$options['join_class_extends']} { 
     246 
     247        $class_file_code = "<?php \n\n//This code was generated automatically by the active record hasAndBelongsToMany Method\n\n"; 
     248        $class_code = 
     249        "class {$options['join_class_name']} extends {$options['join_class_extends']} { 
    251250    var \$_avoidTableNameValidation = true; 
    252251    function {$options['join_class_name']}() 
     
    258257    } 
    259258}"; 
    260             $class_file_code .= $class_code. "\n\n?>"; 
    261             $join_file = AkInflector::toModelFilename($options['join_class_name']); 
    262             if($this->_automatically_create_join_model_files && !file_exists($join_file) && @Ak::file_put_contents($join_file, $class_file_code)){ 
    263                 require_once($join_file); 
    264             }else{ 
    265                 eval($class_code); 
    266             } 
     259        $class_file_code .= $class_code. "\n\n?>"; 
     260        $join_file = AkInflector::toModelFilename($options['join_class_name']); 
     261        if($this->_automatically_create_join_model_files && !file_exists($join_file) && @Ak::file_put_contents($join_file, $class_file_code)){ 
     262            require_once($join_file); 
     263        }else{ 
     264            eval($class_code); 
    267265        } 
    268266        return class_exists($options['join_class_name']); 
    269267    } 
    270268 
    271     function _hasJoinTable() 
    272     { 
    273         $options = $this->getOptions($this->association_id); 
    274         if(isset($this->JoinObject)){ 
    275             return $this->JoinObject->setTableName($options['join_table'], true, true); 
    276         } 
    277         return false; 
     269    function _tableExists($table_name) 
     270    { 
     271        return $this->JoinObject->setTableName($table_name, true, true); 
    278272    } 
    279273 
     
    281275    { 
    282276        $options = $this->getOptions($this->association_id); 
    283         require_once(AK_LIB_DIR.DS.'AkDbManager.php'); 
    284  
    285         AkDbManager::createTable($options['join_table'], "id I AUTO KEY,{$options['foreign_key']} I, {$options['association_foreign_key']} I",array('mysql' => 'TYPE=InnoDB'),false, 
    286         "{$options['foreign_key']},{$options['association_foreign_key']}"); 
    287         return $this->_hasJoinTable(); 
    288     } 
    289  
    290  
     277        require_once(AK_LIB_DIR.DS.'AkInstaller.php'); 
     278        $Installer =& new AkInstaller(); 
     279        $Installer->createTable($options['join_table'],"id,{$options['foreign_key']},{$options['association_foreign_key']}"); 
     280        return $this->JoinObject->setTableName($options['join_table'],false); 
     281    } 
    291282 
    292283    function &load($force_reload = false) 
  • branches/kaste/framework/lib/AkUnitTest.php

    r397 r418  
    9797            } 
    9898            $installer = new AkInstaller(); 
    99             $installer->dropTable($table_name); 
     99            $installer->dropTable($table_name,array('sequence'=>true)); 
    100100            $installer->createTable($table_name,$table_definition); 
    101101        }