Changeset 328
- Timestamp:
- 08/27/07 20:10:14 (1 year ago)
- Files:
-
- trunk/lib/AkActiveRecord/AkHasAndBelongsToMany.php (modified) (1 diff)
- trunk/lib/AkActiveRecord/AkHasMany.php (modified) (7 diffs)
- trunk/script/test (modified) (2 diffs)
- trunk/test/fixtures/app/installers/comment_installer.php (modified) (1 diff)
- trunk/test/fixtures/app/installers/post_installer.php (modified) (1 diff)
- trunk/test/fixtures/app/models/post.php (modified) (1 diff)
- trunk/test/fixtures/app/models/tag.php (modified) (1 diff)
- trunk/test/unit/lib/AkActiveRecord/AkHasAndBelongsToMany.php (modified) (6 diffs)
- trunk/test/unit/lib/AkActiveRecord/AkHasMany.php (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/AkActiveRecord/AkHasAndBelongsToMany.php
r322 r328 440 440 441 441 if(!in_array($id, $ids_to_skip)){ 442 $JoinObjectToDelete =& $this->JoinObject->findFirstBy( 443 $options['foreign_key'].' AND '.$options['association_foreign_key'], 444 $this->Owner->getId(), $id); 445 if($JoinObjectToDelete->destroy()){ 446 $items_to_remove_from_collection[] = $id; 447 }else{ 448 $success = false; 442 443 if($JoinObjectsToDelete =& $this->JoinObject->findAllBy($options['foreign_key'].' AND '.$options['association_foreign_key'], $this->Owner->getId(), $id)){ 444 foreach (array_keys($JoinObjectsToDelete) as $k) { 445 if($JoinObjectsToDelete[$k]->destroy()){ 446 $items_to_remove_from_collection[] = $id; 447 }else{ 448 $success = false; 449 } 450 } 449 451 } 450 452 } trunk/lib/AkActiveRecord/AkHasMany.php
r315 r328 252 252 $ids = func_get_args(); 253 253 $ids = is_array($ids[0]) ? $ids[0] : $ids; 254 254 255 $AssociatedModel =& $this->getAssociatedModelInstance(); 255 256 if(!empty($ids)){ … … 314 315 break; 315 316 case 'nullify': 316 $ids_to_nullify[] = $Associated[$k]->quotedId(); 317 $id_to_nullify = $Associated[$k]->quotedId(); 318 if(!empty($id_to_nullify)){ 319 $ids_to_nullify[] = $id_to_nullify; 320 } 317 321 default: 318 322 break; … … 321 325 } 322 326 327 $ids_to_nullify = empty($ids_to_nullify) ? false : array_diff($ids_to_nullify,array('')); 323 328 if(!empty($ids_to_nullify)){ 324 329 $success = $AssociatedModel->updateAll( … … 399 404 $Member->__hasManyMemberId = Ak::randomString(); 400 405 } 401 $object_id = $Member->getId();406 $object_id = method_exists($Member,'getId') ? $Member->getId() : null; 402 407 if(!empty($object_id)){ 403 408 $this->asssociated_ids[$object_id] = $Member->__hasManyMemberId; … … 429 434 { 430 435 if(!$this->Owner->isNewRecord()){ 431 $foreign_key = $this->getOption($this->association_id, 'foreign_key'); 432 if($this->getOption($this->association_id, 'class_name') != $Associated->getModelName() || $foreign_key == $Associated->get($foreign_key)){ 433 return false; 434 } 435 $Associated->set($foreign_key, $this->Owner->getId()); 436 return true; 436 if(method_exists($Associated, 'getModelName')){ 437 $foreign_key = $this->getOption($this->association_id, 'foreign_key'); 438 if($this->getOption($this->association_id, 'class_name') != $Associated->getModelName() || $foreign_key == $Associated->get($foreign_key)){ 439 return false; 440 } 441 $Associated->set($foreign_key, $this->Owner->getId()); 442 return true; 443 } 437 444 } 438 445 return false; … … 710 717 711 718 case 'nullify': 712 $ids_to_nullify[] = $object->{$k}[$key]->quotedId(); 719 $id_to_nullify = $object->{$k}[$key]->quotedId(); 720 if(!empty($id_to_nullify)){ 721 $ids_to_nullify[] = $id_to_nullify; 722 } 713 723 break; 714 724 … … 717 727 } 718 728 } 719 729 730 $ids_to_nullify = empty($ids_to_nullify) ? false : array_diff($ids_to_nullify,array('')); 720 731 if(!empty($ids_to_nullify)){ 721 732 $success = $object->{$k}[$key]->updateAll( 722 733 ' '.$object->$v->options[$k]['foreign_key'].' = NULL ', 723 ' '.$object->$v->options[$k]['foreign_key'].' = '.$object->quotedId().' AND '.$object->{$k}[$key]->getPrimaryKey().' IN ('.join(', ', $ids_to_nullify).')'734 ' '.$object->$v->options[$k]['foreign_key'].' = '.$object->quotedId().' AND '.$object->{$k}[$key]->getPrimaryKey().' IN ('.join(', ', $ids_to_nullify).')' 724 735 ) ? $success : false; 725 736 }elseif(!empty($ids_to_delete)){ trunk/script/test
r325 r328 22 22 23 23 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true); 24 defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION') ? null : define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION', false); 25 24 26 require_once(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'test'.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php'); 25 27 … … 42 44 if(!preg_match('/^('.join('|',$____skip_tests).')$/i',$match[1])){ 43 45 $____skip_tests[] = $match[1]; 44 Ak::trace(join(',',$____skip_tests));45 46 ak_test($match[1].'TestCase', true); 46 47 } trunk/test/fixtures/app/installers/comment_installer.php
r315 r328 14 14 function down_1() 15 15 { 16 $this->dropTable('comments' );16 $this->dropTable('comments', array('sequence'=>true)); 17 17 } 18 18 trunk/test/fixtures/app/installers/post_installer.php
r313 r328 10 10 function uninstall() 11 11 { 12 $this->dropTable('posts' );12 $this->dropTable('posts', array('sequence'=>true)); 13 13 } 14 14 } trunk/test/fixtures/app/models/post.php
r315 r328 4 4 { 5 5 var $has_many = 'comments'; 6 var $habtm = 'tags'; 6 7 } 7 8 trunk/test/fixtures/app/models/tag.php
r55 r328 7 7 'join_table' => 'taggings', 8 8 'join_class_name' => 'Tagging' 9 ) 9 ), 10 'posts' 10 11 ); 11 12 trunk/test/unit/lib/AkActiveRecord/AkHasAndBelongsToMany.php
r320 r328 1 1 <?php 2 2 3 if(!defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION')){ 4 define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION',false); 5 } 6 7 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true); 8 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php'); 9 10 11 class test_AkActiveRecord_hasAndBelongsToMany_Associations extends AkUnitTest 3 class HasAndBelongsToManyTestCase extends AkUnitTest 12 4 { 5 function setup() 6 { 7 $this->installAndIncludeModels(array('Post', 'Tag')); 8 $Installer = new AkInstaller(); 9 @$Installer->dropTable('posts_tags'); 10 @Ak::file_delete(AK_MODELS_DIR.DS.'post_tag.php'); 11 } 12 /**/ 13 13 function test_start() 14 14 { … … 22 22 $installer->uninstall(); 23 23 $installer->install(); 24 25 24 26 $models = array('Picture', 'Thumbnail','Panorama', 'Property', 'PropertyType'); 25 27 foreach ($models as $model){ … … 98 100 $this->assertEqual($Property->property_type->count(), 1); 99 101 100 $Property = $Property->findFirstBy('description','Gandia Palace');101 102 $PropertyType = new PropertyType();103 104 $PropertyTypes = $PropertyType->find();102 103 $Property =& $Property->findFirstBy('description','Gandia Palace'); 104 $PropertyType =& new PropertyType(); 105 106 $PropertyTypes =& $PropertyType->find(); 105 107 106 108 $Property->property_type->set($PropertyTypes); … … 108 110 $this->assertEqual($Property->property_type->count(), count($PropertyTypes)); 109 111 110 $Property = $Property->findFirstBy('description','Gandia Palace');112 $Property =& $Property->findFirstBy('description','Gandia Palace'); 111 113 112 114 $Property->property_type->load(); 113 115 $this->assertEqual($Property->property_type->count(), count($PropertyTypes)); 114 116 115 $Property = $Property->findFirstBy('description','Gandia Palace');117 $Property =& $Property->findFirstBy('description','Gandia Palace'); 116 118 117 119 $PropertyType->set('description', 'Palace'); 120 118 121 $Property->property_type->set($PropertyType); 119 122 120 123 $this->assertEqual($Property->property_type->count(), 1); 121 124 122 125 $this->assertTrue(in_array('property_types', $Property->getAssociatedIds())); 123 126 … … 224 227 225 228 } 226 227 229 228 230 function test_find_on_unsaved_models_including_associations() 229 231 { … … 404 406 405 407 } 408 409 410 function test_remove_existing_associates_before_setting_by_id() 411 { 412 413 foreach (range(1,10) as $i){ 414 $Post =& new Post(array('title' => 'Post '.$i)); 415 $Post->tag->create(array('name' => 'Tag '.$i)); 416 $Post->save(); 417 $this->assertEqual($Post->tag->count(), 1); 418 } 419 420 $Post11 =& new Post(array('name' => 'Post 11')); 421 $this->assertTrue($Post11->save()); 422 423 $Post->tag->setByIds(1,2,3,4,5); 424 425 $this->assertTrue($Post =& $Post->find(10, array('include' => 'tags'))); 426 427 foreach (array_keys($Post->tags) as $k){ 428 $this->assertEqual($Post->tags[$k]->getId(), $k+1); 429 } 430 431 // Tag 10 should exist but unrelated to a post 432 $this->assertTrue($Tag =& $Post->tags[$k]->find(10)); 433 $this->assertEqual($Tag->post->count(), 0); 434 435 $Post11->tag->setByIds(array(10,1)); 436 437 $this->assertTrue($Tag =& $Tag->find(10, array('include'=>'posts'))); 438 $this->assertEqual($Tag->posts[0]->getId(), 11); 439 440 441 } 442 /**//** //**/ 406 443 } 407 444 408 ak_test('test_AkActiveRecord_hasAndBelongsToMany_Associations', true);409 445 410 446 ?> trunk/test/unit/lib/AkActiveRecord/AkHasMany.php
r323 r328 1 1 <?php 2 2 3 if(!defined('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION')){ 4 define('AK_ACTIVE_RECORD_PROTECT_GET_RECURSION',false); 5 } 6 7 defined('AK_TEST_DATABASE_ON') ? null : define('AK_TEST_DATABASE_ON', true); 8 require_once(dirname(__FILE__).'/../../../fixtures/config/config.php'); 9 10 11 class test_AkActiveRecord_hasMany_Associations extends AkUnitTest 3 class HasManyTestCase extends AkUnitTest 12 4 { 13 5 /**/ 14 6 function test_start() 15 7 { … … 35 27 } 36 28 37 /**/38 29 function test_for_has_many() 39 30 { … … 213 204 } 214 205 215 /**/216 217 206 function test_clean_up_dependencies() 218 207 { … … 273 262 $this->assertEqual($Result->comments[0]->get('name'), 'Aditya'); 274 263 } 275 /**/ 264 265 266 function test_remove_existing_associates_before_setting_by_id() 267 { 268 $this->installAndIncludeModels(array('Post', 'Comment')); 269 270 foreach (range(1,10) as $i){ 271 $Post =& new Post(array('title' => 'Post '.$i)); 272 $Post->comment->create(array('name' => 'Comment '.$i)); 273 $Post->save(); 274 } 275 276 $Post11 =& new Post(array('name' => 'Post 11')); 277 $this->assertTrue($Post11->save()); 278 279 $Post->comment->setByIds(1,2,3,4,5); 280 281 $this->assertTrue($Post =& $Post->find(10, array('include' => 'comments'))); 282 283 284 foreach (array_keys($Post->comments) as $k){ 285 $this->assertEqual($Post->comments[$k]->getId(), $k+1); 286 } 287 288 // Comment 10 should exist but unrelated to a post 289 $this->assertTrue($Comment =& $Post->comments[$k]->find(10)); 290 $this->assertNull($Comment->get('post_id')); 291 292 $Post11->comment->setByIds(array(10,1)); 293 294 $this->assertTrue($Comment =& $Comment->find(10)); 295 $this->assertEqual($Comment->get('post_id'), 11); 296 } 297 /**//**//**/ 276 298 } 277 299 278 ak_test('test_AkActiveRecord_hasMany_Associations', true);279 280 300 281 301 ?>
