fixes , restoring associations in Containable. Thanks to maular for the patch

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7245 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-06-23 04:41:15 +00:00
parent 9661a54a13
commit ca38b035b5
2 changed files with 122 additions and 120 deletions
cake
libs/model/behaviors
tests/cases/libs/model/behaviors

View file

@ -134,6 +134,14 @@ class ContainableBehavior extends ModelBehavior {
if (!isset($model['keep'][$assoc])) { if (!isset($model['keep'][$assoc])) {
$unbind[] = $assoc; $unbind[] = $assoc;
} }
}
if ($unbind) {
if (!$reset && empty($instance->__backOriginalAssociation)) {
$instance->__backOriginalAssociation = $backupBindings;
}
$instance->unbindModel(array($type => $unbind), $reset);
}
foreach ($instance->{$type} as $assoc => $options) {
if (isset($model['keep'][$assoc]) && !empty($model['keep'][$assoc])) { if (isset($model['keep'][$assoc]) && !empty($model['keep'][$assoc])) {
if (isset($model['keep'][$assoc]['fields'])) { if (isset($model['keep'][$assoc]['fields'])) {
$model['keep'][$assoc]['fields'] = $this->fieldDependencies($containments['models'][$assoc]['instance'], $map, $model['keep'][$assoc]['fields']); $model['keep'][$assoc]['fields'] = $this->fieldDependencies($containments['models'][$assoc]['instance'], $map, $model['keep'][$assoc]['fields']);
@ -149,12 +157,6 @@ class ContainableBehavior extends ModelBehavior {
$instance->__backInnerAssociation[] = $assoc; $instance->__backInnerAssociation[] = $assoc;
} }
} }
if ($unbind) {
if (!$reset && empty($instance->__backOriginalAssociation)) {
$instance->__backOriginalAssociation = $backupBindings;
}
$instance->unbindModel(array($type => $unbind), $reset);
}
} }
} }
} }

View file

@ -31,7 +31,7 @@ App::import('Core', array('AppModel', 'Model'));
require_once(dirname(__FILE__) . DS . '..' . DS . 'models.php'); require_once(dirname(__FILE__) . DS . '..' . DS . 'models.php');
/** /**
* ContainableTest class * ContainableTest class
* *
* @package cake * @package cake
* @subpackage cake.tests.cases.libs.model.behaviors * @subpackage cake.tests.cases.libs.model.behaviors
*/ */
@ -79,7 +79,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testContainments method * testContainments method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -130,7 +130,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testInvalidContainments method * testInvalidContainments method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -143,7 +143,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testBeforeFind method * testBeforeFind method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -217,7 +217,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testContain method * testContain method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -232,7 +232,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testFindEmbeddedNoBindings method * testFindEmbeddedNoBindings method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -256,7 +256,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testFindFirstLevel method * testFindFirstLevel method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -364,7 +364,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testFindEmbeddedFirstLevel method * testFindEmbeddedFirstLevel method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -470,7 +470,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testFindSecondLevel method * testFindSecondLevel method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -816,7 +816,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testFindEmbeddedSecondLevel method * testFindEmbeddedSecondLevel method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -1158,7 +1158,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testFindThirdLevel method * testFindThirdLevel method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -1478,7 +1478,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testFindEmbeddedThirdLevel method * testFindEmbeddedThirdLevel method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -1795,7 +1795,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testSettingsThirdLevel method * testSettingsThirdLevel method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -2035,7 +2035,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testFindThirdLevelNonReset method * testFindThirdLevelNonReset method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -2359,7 +2359,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testFindEmbeddedThirdLevelNonReset method * testFindEmbeddedThirdLevelNonReset method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -2848,7 +2848,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testEmbeddedFindFields method * testEmbeddedFindFields method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -2901,7 +2901,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testFindConditionalBinding method * testFindConditionalBinding method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -3003,24 +3003,24 @@ class ContainableTest extends CakeTestCase {
) )
); );
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$this->assertTrue(empty($this->User->Article->hasAndBelongsToMany['Tag']['conditions'])); $this->assertTrue(empty($this->User->Article->hasAndBelongsToMany['Tag']['conditions']));
$result = $this->User->find('all', array('contain' => array( $result = $this->User->find('all', array('contain' => array(
'Article.Tag' => array('conditions' => array('created >=' => '2007-03-18 12:24')) 'Article.Tag' => array('conditions' => array('created >=' => '2007-03-18 12:24'))
))); )));
$this->assertTrue(Set::matches('/User[id=1]', $result)); $this->assertTrue(Set::matches('/User[id=1]', $result));
$this->assertFalse(Set::matches('/Article[id=1]/Tag[id=1]', $result)); $this->assertFalse(Set::matches('/Article[id=1]/Tag[id=1]', $result));
$this->assertTrue(Set::matches('/Article[id=1]/Tag[id=2]', $result)); $this->assertTrue(Set::matches('/Article[id=1]/Tag[id=2]', $result));
$this->assertTrue(empty($this->User->Article->hasAndBelongsToMany['Tag']['conditions'])); $this->assertTrue(empty($this->User->Article->hasAndBelongsToMany['Tag']['conditions']));
$this->assertTrue(empty($this->User->Article->hasAndBelongsToMany['Tag']['order'])); $this->assertTrue(empty($this->User->Article->hasAndBelongsToMany['Tag']['order']));
$result = $this->User->find('all', array('contain' => array( $result = $this->User->find('all', array('contain' => array(
'Article.Tag' => array('order' => 'created DESC') 'Article.Tag' => array('order' => 'created DESC')
))); )));
$this->assertTrue(Set::matches('/User[id=1]', $result)); $this->assertTrue(Set::matches('/User[id=1]', $result));
$this->assertTrue(Set::matches('/Article[id=1]/Tag[id=1]', $result)); $this->assertTrue(Set::matches('/Article[id=1]/Tag[id=1]', $result));
$this->assertTrue(Set::matches('/Article[id=1]/Tag[id=2]', $result)); $this->assertTrue(Set::matches('/Article[id=1]/Tag[id=2]', $result));
@ -3028,7 +3028,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testOtherFinds method * testOtherFinds method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -3091,7 +3091,7 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testPaginate method * testPaginate method
* *
* @access public * @access public
* @return void * @return void
*/ */
@ -3176,17 +3176,17 @@ class ContainableTest extends CakeTestCase {
$this->assertTrue(Set::matches('/Article[id=1]', $r)); $this->assertTrue(Set::matches('/Article[id=1]', $r));
$this->assertTrue(Set::matches('/User[id=1]', $r)); $this->assertTrue(Set::matches('/User[id=1]', $r));
$this->assertTrue(Set::matches('/Tag[id=1]', $r)); $this->assertTrue(Set::matches('/Tag[id=1]', $r));
$Controller->Article->unbindModel(array('hasMany' => array('Comment'), 'belongsTo' => array('User'), 'hasAndBelongsToMany' => array('Tag')), false); $Controller->Article->unbindModel(array('hasMany' => array('Comment'), 'belongsTo' => array('User'), 'hasAndBelongsToMany' => array('Tag')), false);
$Controller->Article->bindModel(array('hasMany' => array('Comment'), 'belongsTo' => array('User')), false); $Controller->Article->bindModel(array('hasMany' => array('Comment'), 'belongsTo' => array('User')), false);
$Controller->paginate = array('Article' => array('contain' => array('Comment(comment)', 'User(user)'), 'fields' => array('title'))); $Controller->paginate = array('Article' => array('contain' => array('Comment(comment)', 'User(user)'), 'fields' => array('title')));
$r = $Controller->paginate('Article'); $r = $Controller->paginate('Article');
$this->assertTrue(Set::matches('/Article[id=1]', $r)); $this->assertTrue(Set::matches('/Article[id=1]', $r));
$this->assertTrue(Set::matches('/User[id=1]', $r)); $this->assertTrue(Set::matches('/User[id=1]', $r));
$this->assertTrue(Set::matches('/Comment[article_id=1]', $r)); $this->assertTrue(Set::matches('/Comment[article_id=1]', $r));
$this->assertFalse(Set::matches('/Comment[id=1]', $r)); $this->assertFalse(Set::matches('/Comment[id=1]', $r));
$r = $this->Article->find('all'); $r = $this->Article->find('all');
$this->assertTrue(Set::matches('/Article[id=1]', $r)); $this->assertTrue(Set::matches('/Article[id=1]', $r));
$this->assertTrue(Set::matches('/User[id=1]', $r)); $this->assertTrue(Set::matches('/User[id=1]', $r));
@ -3195,91 +3195,91 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testOriginalAssociations method * testOriginalAssociations method
* *
* @access public * @access public
* @return void * @return void
*/ */
function testOriginalAssociations() { function testOriginalAssociations() {
$this->Article->Comment->Behaviors->attach('Containable'); $this->Article->Comment->Behaviors->attach('Containable');
$options = array( $options = array(
'conditions' => array( 'conditions' => array(
'Comment.comment' => '!= Crazy', 'Comment.comment !=' => 'Crazy',
'Comment.published' => 'Y', 'Comment.published' => 'Y',
), ),
'contain' => 'User', 'contain' => 'User',
'recursive' => 1 'recursive' => 1
); );
$firstResult = $this->Article->Comment->find('all', $options); $firstResult = $this->Article->Comment->find('all', $options);
$dummyResult = $this->Article->Comment->find('all', array( $dummyResult = $this->Article->Comment->find('all', array(
'conditions' => array( 'conditions' => array(
'Comment.comment' => '!= Silly', 'Comment.comment !=' => 'Silly',
'User.user' => 'mariano' 'User.user' => 'mariano'
), ),
'fields' => array('User.password'), 'fields' => array('User.password'),
'contain' => array('User.password'), 'contain' => array('User.password'),
)); ));
$result = $this->Article->Comment->find('all', $options); $result = $this->Article->Comment->find('all', $options);
$this->assertEqual($result, $firstResult); $this->assertEqual($result, $firstResult);
$this->Article->unbindModel(array('hasMany' => array('Comment'), 'belongsTo' => array('User'), 'hasAndBelongsToMany' => array('Tag')), false); $this->Article->unbindModel(array('hasMany' => array('Comment'), 'belongsTo' => array('User'), 'hasAndBelongsToMany' => array('Tag')), false);
$this->Article->bindModel(array('hasMany' => array('Comment'), 'belongsTo' => array('User')), false); $this->Article->bindModel(array('hasMany' => array('Comment'), 'belongsTo' => array('User')), false);
$r = $this->Article->find('all', array('contain' => array('Comment(comment)', 'User(user)'), 'fields' => array('title'))); $r = $this->Article->find('all', array('contain' => array('Comment(comment)', 'User(user)'), 'fields' => array('title')));
$this->assertTrue(Set::matches('/Article[id=1]', $r)); $this->assertTrue(Set::matches('/Article[id=1]', $r));
$this->assertTrue(Set::matches('/User[id=1]', $r)); $this->assertTrue(Set::matches('/User[id=1]', $r));
$this->assertTrue(Set::matches('/Comment[article_id=1]', $r)); $this->assertTrue(Set::matches('/Comment[article_id=1]', $r));
$this->assertFalse(Set::matches('/Comment[id=1]', $r)); $this->assertFalse(Set::matches('/Comment[id=1]', $r));
$r = $this->Article->find('all'); $r = $this->Article->find('all');
$this->assertTrue(Set::matches('/Article[id=1]', $r)); $this->assertTrue(Set::matches('/Article[id=1]', $r));
$this->assertTrue(Set::matches('/User[id=1]', $r)); $this->assertTrue(Set::matches('/User[id=1]', $r));
$this->assertTrue(Set::matches('/Comment[article_id=1]', $r)); $this->assertTrue(Set::matches('/Comment[article_id=1]', $r));
$this->assertTrue(Set::matches('/Comment[id=1]', $r)); $this->assertTrue(Set::matches('/Comment[id=1]', $r));
$this->Article->bindModel(array('hasAndBelongsToMany' => array('Tag')), false); $this->Article->bindModel(array('hasAndBelongsToMany' => array('Tag')), false);
$this->Article->contain(false, array('User(id,user)', 'Comment' => array('fields' => array('comment'), 'conditions' => array('created >=' => '2007-03-18 10:49')))); $this->Article->contain(false, array('User(id,user)', 'Comment' => array('fields' => array('comment'), 'conditions' => array('created >=' => '2007-03-18 10:49'))));
$result = $this->Article->find('all', array('fields' => array('title'), 'limit' => 1, 'page' => 1, 'order' => 'Article.id ASC')); $result = $this->Article->find('all', array('fields' => array('title'), 'limit' => 1, 'page' => 1, 'order' => 'Article.id ASC'));
$expected = array(array( $expected = array(array(
'Article' => array('id' => 1, 'title' => 'First Article'), 'Article' => array('id' => 1, 'title' => 'First Article'),
'User' => array('id' => 1, 'user' => 'mariano'), 'User' => array('id' => 1, 'user' => 'mariano'),
'Comment' => array( 'Comment' => array(
array('comment' => 'Third Comment for First Article', 'article_id' => 1), array('comment' => 'Third Comment for First Article', 'article_id' => 1),
array('comment' => 'Fourth Comment for First Article', 'article_id' => 1) array('comment' => 'Fourth Comment for First Article', 'article_id' => 1)
) )
)); ));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->Article->find('all', array('fields' => array('title', 'User.id', 'User.user'), 'limit' => 1, 'page' => 2, 'order' => 'Article.id ASC')); $result = $this->Article->find('all', array('fields' => array('title', 'User.id', 'User.user'), 'limit' => 1, 'page' => 2, 'order' => 'Article.id ASC'));
$expected = array(array( $expected = array(array(
'Article' => array('id' => 2, 'title' => 'Second Article'), 'Article' => array('id' => 2, 'title' => 'Second Article'),
'User' => array('id' => 3, 'user' => 'larry'), 'User' => array('id' => 3, 'user' => 'larry'),
'Comment' => array( 'Comment' => array(
array('comment' => 'First Comment for Second Article', 'article_id' => 2), array('comment' => 'First Comment for Second Article', 'article_id' => 2),
array('comment' => 'Second Comment for Second Article', 'article_id' => 2) array('comment' => 'Second Comment for Second Article', 'article_id' => 2)
) )
)); ));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$result = $this->Article->find('all', array('fields' => array('title', 'User.id', 'User.user'), 'limit' => 1, 'page' => 3, 'order' => 'Article.id ASC')); $result = $this->Article->find('all', array('fields' => array('title', 'User.id', 'User.user'), 'limit' => 1, 'page' => 3, 'order' => 'Article.id ASC'));
$expected = array(array( $expected = array(array(
'Article' => array('id' => 3, 'title' => 'Third Article'), 'Article' => array('id' => 3, 'title' => 'Third Article'),
'User' => array('id' => 1, 'user' => 'mariano'), 'User' => array('id' => 1, 'user' => 'mariano'),
'Comment' => array() 'Comment' => array()
)); ));
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);
$this->Article->contain(false, array('User' => array('fields' => 'user'), 'Comment')); $this->Article->contain(false, array('User' => array('fields' => 'user'), 'Comment'));
$result = $this->Article->find('all'); $result = $this->Article->find('all');
$this->assertTrue(Set::matches('/Article[id=1]', $result)); $this->assertTrue(Set::matches('/Article[id=1]', $result));
$this->assertTrue(Set::matches('/User[user=mariano]', $result)); $this->assertTrue(Set::matches('/User[user=mariano]', $result));
$this->assertTrue(Set::matches('/Comment[article_id=1]', $result)); $this->assertTrue(Set::matches('/Comment[article_id=1]', $result));
$this->Article->resetBindings(); $this->Article->resetBindings();
$this->Article->contain(false, array('User' => array('fields' => array('user')), 'Comment')); $this->Article->contain(false, array('User' => array('fields' => array('user')), 'Comment'));
$result = $this->Article->find('all'); $result = $this->Article->find('all');
$this->assertTrue(Set::matches('/Article[id=1]', $result)); $this->assertTrue(Set::matches('/Article[id=1]', $result));
@ -3289,43 +3289,43 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* testResetAssociation method * testResetAssociation method
* *
* @access public * @access public
*/ */
function testResetAssociation() { function testResetAssociation() {
$this->Article->Behaviors->attach('Containable'); $this->Article->Behaviors->attach('Containable');
$this->Article->Comment->Behaviors->attach('Containable'); $this->Article->Comment->Behaviors->attach('Containable');
$this->Article->User->Behaviors->attach('Containable'); $this->Article->User->Behaviors->attach('Containable');
$initialOptions = array( $initialOptions = array(
'conditions' => array( 'conditions' => array(
'Comment.comment' => '!= Crazy', 'Comment.comment' => '!= Crazy',
'Comment.published' => 'Y', 'Comment.published' => 'Y',
), ),
'contain' => 'User', 'contain' => 'User',
'recursive' => 1, 'recursive' => 1,
); );
$initialModels = $this->Article->Comment->find('all', $initialOptions); $initialModels = $this->Article->Comment->find('all', $initialOptions);
$findOptions = array( $findOptions = array(
'conditions' => array( 'conditions' => array(
'Comment.comment' => '!= Silly', 'Comment.comment' => '!= Silly',
'User.user' => 'mariano', 'User.user' => 'mariano',
), ),
'fields' => array('User.password'), 'fields' => array('User.password'),
'contain' => array('User.password') 'contain' => array('User.password')
); );
$result = $this->Article->Comment->find('all', $findOptions); $result = $this->Article->Comment->find('all', $findOptions);
$result = $this->Article->Comment->find('all', $initialOptions); $result = $this->Article->Comment->find('all', $initialOptions);
$this->assertEqual($result, $initialModels); $this->assertEqual($result, $initialModels);
} }
/** /**
* containments method * containments method
* *
* @param mixed $Model * @param mixed $Model
* @param array $contain * @param array $contain
* @access private * @access private
* @return void * @return void
*/ */
@ -3344,9 +3344,9 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* assertBindings method * assertBindings method
* *
* @param mixed $Model * @param mixed $Model
* @param array $expected * @param array $expected
* @access private * @access private
* @return void * @return void
*/ */
@ -3359,10 +3359,10 @@ class ContainableTest extends CakeTestCase {
} }
/** /**
* bindings method * bindings method
* *
* @param mixed $Model * @param mixed $Model
* @param array $extra * @param array $extra
* @param bool $output * @param bool $output
* @access private * @access private
* @return void * @return void
*/ */