/**
  * testSaveHabtm method
  *
  * @return void
  */
 public function testSaveHabtm()
 {
     $this->loadFixtures('Article', 'User', 'Comment', 'Tag', 'ArticlesTag');
     $TestModel = new Article();
     $result = $TestModel->findById(2);
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y', 'created' => '2007-03-18 10:41:23', 'updated' => '2007-03-18 10:43:31'), 'User' => array('id' => '3', 'user' => 'larry', 'password' => '5f4dcc3b5aa765d61d8327deb882cf99', 'created' => '2007-03-17 01:20:23', 'updated' => '2007-03-17 01:22:31'), 'Comment' => array(array('id' => '5', 'article_id' => '2', 'user_id' => '1', 'comment' => 'First Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'), array('id' => '6', 'article_id' => '2', 'user_id' => '2', 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31')), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEquals($expected, $result);
     $data = array('Article' => array('id' => '2', 'title' => 'New Second Article'), 'Tag' => array('Tag' => array(1, 2)));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $this->assertEquals($data['Tag'], $result['Tag']);
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')));
     $this->assertEquals($expected, $result);
     $data = array('Article' => array('id' => '2'), 'Tag' => array('Tag' => array(2, 3)));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEquals($expected, $result);
     $data = array('Tag' => array('Tag' => array(1, 2, 3)));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEquals($expected, $result);
     $data = array('Tag' => array('Tag' => array()));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $data = array('Tag' => array('Tag' => ''));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array());
     $this->assertEquals($expected, $result);
     $data = array('Tag' => array('Tag' => array(2, 3)));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEquals($expected, $result);
     $data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article'));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')));
     $this->assertEquals($expected, $result);
     $data = array('Tag' => array('Tag' => array(1, 2)), 'Article' => array('id' => '2', 'title' => 'New Second Article Title'));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'New Second Article Title', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31')));
     $this->assertEquals($expected, $result);
     $data = array('Tag' => array('Tag' => array(2, 3)), 'Article' => array('id' => '2', 'title' => 'Changed Second Article'));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Changed Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '2', 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEquals($expected, $result);
     $data = array('Tag' => array('Tag' => array(1, 3)), 'Article' => array('id' => '2'));
     $result = $TestModel->set($data);
     $this->assertFalse(empty($result));
     $result = $TestModel->save();
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->find('first', array('fields' => array('id', 'user_id', 'title', 'body'), 'conditions' => array('Article.id' => 2)));
     $expected = array('Article' => array('id' => '2', 'user_id' => '3', 'title' => 'Changed Second Article', 'body' => 'Second Article Body'), 'Tag' => array(array('id' => '1', 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), array('id' => '3', 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEquals($expected, $result);
     $data = array('Article' => array('id' => 10, 'user_id' => '2', 'title' => 'New Article With Tags and fieldList', 'body' => 'New Article Body with Tags and fieldList', 'created' => '2007-03-18 14:55:23', 'updated' => '2007-03-18 14:57:31'), 'Tag' => array('Tag' => array(1, 2, 3)));
     $result = $TestModel->create() && $TestModel->save($data, true, array('user_id', 'title', 'published'));
     $this->assertFalse(empty($result));
     $TestModel->unbindModel(array('belongsTo' => array('User'), 'hasMany' => array('Comment')));
     $result = $TestModel->read();
     $expected = array('Article' => array('id' => 4, 'user_id' => 2, 'title' => 'New Article With Tags and fieldList', 'body' => '', 'published' => 'N', 'created' => '', 'updated' => ''), 'Tag' => array(0 => array('id' => 1, 'tag' => 'tag1', 'created' => '2007-03-18 12:22:23', 'updated' => '2007-03-18 12:24:31'), 1 => array('id' => 2, 'tag' => 'tag2', 'created' => '2007-03-18 12:24:23', 'updated' => '2007-03-18 12:26:31'), 2 => array('id' => 3, 'tag' => 'tag3', 'created' => '2007-03-18 12:26:23', 'updated' => '2007-03-18 12:28:31')));
     $this->assertEquals($expected, $result);
     $this->loadFixtures('JoinA', 'JoinC', 'JoinAC', 'JoinB', 'JoinAB');
     $TestModel = new JoinA();
     $TestModel->hasBelongsToMany = array('JoinC' => array('unique' => true));
     $data = array('JoinA' => array('id' => 1, 'name' => 'Join A 1', 'body' => 'Join A 1 Body'), 'JoinC' => array('JoinC' => array(array('join_c_id' => 2, 'other' => 'new record'), array('join_c_id' => 3, 'other' => 'new record'))));
     $TestModel->save($data);
     $result = $TestModel->read(null, 1);
     $expected = array(4, 5);
     $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.id'));
     $expected = array('new record', 'new record');
     $this->assertEquals($expected, Hash::extract($result, 'JoinC.{n}.JoinAsJoinC.other'));
 }