updating habtm save handling, fixes #5579

git-svn-id: https://svn.cakephp.org/repo/branches/1.2.x.x@7795 3807eeeb-6ff5-0310-8944-8be069107fe0
This commit is contained in:
gwoo 2008-10-29 07:19:26 +00:00
parent 318c2b4952
commit 6624d41ecd
2 changed files with 79 additions and 44 deletions

View file

@ -1216,39 +1216,29 @@ class Model extends Overloadable {
function __saveMulti($joined, $id) { function __saveMulti($joined, $id) {
$db =& ConnectionManager::getDataSource($this->useDbConfig); $db =& ConnectionManager::getDataSource($this->useDbConfig);
foreach ($joined as $assoc => $value) { foreach ($joined as $assoc => $data) {
$newValues = array();
if (empty($value)) {
$value = array();
}
if (isset($this->hasAndBelongsToMany[$assoc])) { if (isset($this->hasAndBelongsToMany[$assoc])) {
list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']); list($join) = $this->joinModel($this->hasAndBelongsToMany[$assoc]['with']);
$conditions = array($join . '.' . $this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id);
$links = array();
if ($this->hasAndBelongsToMany[$assoc]['unique']) { $conditions = array($join . '.' . $this->hasAndBelongsToMany[$assoc]['foreignKey'] => $id);
$this->{$join}->deleteAll($conditions);
} else { $links = $this->{$join}->find('all', array(
list($recursive, $fields) = array(-1, $this->hasAndBelongsToMany[$assoc]['associationForeignKey']); 'conditions' => $conditions,
$links = Set::extract( 'recursive' => -1,
$this->{$join}->find('all', compact('conditions', 'recursive', 'fields')), 'fields' => $this->hasAndBelongsToMany[$assoc]['associationForeignKey']
"{n}.{$join}." . $this->hasAndBelongsToMany[$assoc]['associationForeignKey'] ));
);
}
$isUUID = !empty($this->{$join}->primaryKey) && (($this->{$join}->_schema[$this->{$join}->primaryKey]['type'] === 'string' && $this->{$join}->_schema[$this->{$join}->primaryKey]['length'] === 36) $isUUID = !empty($this->{$join}->primaryKey) && (($this->{$join}->_schema[$this->{$join}->primaryKey]['type'] === 'string' && $this->{$join}->_schema[$this->{$join}->primaryKey]['length'] === 36)
|| ($this->{$join}->_schema[$this->{$join}->primaryKey]['type'] === 'binary' && $this->{$join}->_schema[$this->{$join}->primaryKey]['length'] === 16)); || ($this->{$join}->_schema[$this->{$join}->primaryKey]['type'] === 'binary' && $this->{$join}->_schema[$this->{$join}->primaryKey]['length'] === 16));
foreach ($value as $update) { $newData = $newValues = array();
if (!empty($update)) {
if (is_array($update)) { foreach ($data as $row) {
$update[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $id; if (($isUUID && (strlen($row) == 36 || strlen($row) == 16)) || is_numeric($row)) {
$this->{$join}->create($update);
$this->{$join}->save();
} elseif (!in_array($update, $links)) {
$values = array( $values = array(
$db->value($id, $this->getColumnType($this->primaryKey)), $db->value($id, $this->getColumnType($this->primaryKey)),
$db->value($update) $db->value($row)
); );
if ($isUUID) { if ($isUUID) {
$values[] = $db->value(String::uuid()); $values[] = $db->value(String::uuid());
@ -1256,8 +1246,26 @@ class Model extends Overloadable {
$values = join(',', $values); $values = join(',', $values);
$newValues[] = "({$values})"; $newValues[] = "({$values})";
unset($values); unset($values);
} else if (isset($row[$this->hasAndBelongsToMany[$assoc]['associationForeignKey']])) {
$newData[] = $row;
} }
} }
if (!empty($newData)) {
foreach ($newData as $data) {
$data[$this->hasAndBelongsToMany[$assoc]['foreignKey']] = $id;
$this->{$join}->create($data);
$this->{$join}->save();
}
}
if (empty($newData) && $this->hasAndBelongsToMany[$assoc]['unique']) {
$associationForeignKey = "{$join}." . $this->hasAndBelongsToMany[$assoc]['associationForeignKey'];
$oldLinks = Set::extract($links, "{n}.{$associationForeignKey}");
if (!empty($oldLinks)) {
$conditions[$associationForeignKey] = $oldLinks;
$db->delete($this->{$join}, $conditions);
}
} }
if (!empty($newValues)) { if (!empty($newValues)) {
@ -1265,7 +1273,6 @@ class Model extends Overloadable {
$db->name($this->hasAndBelongsToMany[$assoc]['foreignKey']), $db->name($this->hasAndBelongsToMany[$assoc]['foreignKey']),
$db->name($this->hasAndBelongsToMany[$assoc]['associationForeignKey']) $db->name($this->hasAndBelongsToMany[$assoc]['associationForeignKey'])
); );
if ($isUUID) { if ($isUUID) {
$fields[] = $db->name($this->{$join}->primaryKey); $fields[] = $db->name($this->{$join}->primaryKey);
} }

View file

@ -2882,9 +2882,38 @@ class ModelTest extends CakeTestCase {
function testHabtmSaveKeyResolution() { function testHabtmSaveKeyResolution() {
$this->loadFixtures('Apple', 'Device', 'ThePaperMonkies'); $this->loadFixtures('Apple', 'Device', 'ThePaperMonkies');
$ThePaper =& new ThePaper(); $ThePaper =& new ThePaper();
$ThePaper->id = 1;
$ThePaper->id = 1;
$ThePaper->save(array('Monkey' => array(2, 3))); $ThePaper->save(array('Monkey' => array(2, 3)));
$result = $ThePaper->findById(1);
$expected = array(
array('id' => '2', 'device_type_id' => '1', 'name' => 'Device 2', 'typ' => '1'),
array('id' => '3', 'device_type_id' => '1', 'name' => 'Device 3', 'typ' => '2')
);
$this->assertEqual($result['Monkey'], $expected);
$ThePaper->id = 2;
$ThePaper->save(array('Monkey' => array(1, 2, 3)));
$result = $ThePaper->findById(2);
$expected = array(
array('id' => '1', 'device_type_id' => '1', 'name' => 'Device 1', 'typ' => '1'),
array('id' => '2', 'device_type_id' => '1', 'name' => 'Device 2', 'typ' => '1'),
array('id' => '3', 'device_type_id' => '1', 'name' => 'Device 3', 'typ' => '2'),
);
$this->assertEqual($result['Monkey'], $expected);
$ThePaper->id = 2;
$ThePaper->save(array('Monkey' => array(1, 3)));
$result = $ThePaper->findById(2);
$expected = array(
array('id' => '3', 'device_type_id' => '1', 'name' => 'Device 3', 'typ' => '2'),
array('id' => '1', 'device_type_id' => '1', 'name' => 'Device 1', 'typ' => '1'),
);
$this->assertEqual($result['Monkey'], $expected);
$result = $ThePaper->findById(1); $result = $ThePaper->findById(1);
$expected = array( $expected = array(
array('id' => '2', 'device_type_id' => '1', 'name' => 'Device 2', 'typ' => '1'), array('id' => '2', 'device_type_id' => '1', 'name' => 'Device 2', 'typ' => '1'),
@ -2892,7 +2921,6 @@ class ModelTest extends CakeTestCase {
); );
$this->assertEqual($result['Monkey'], $expected); $this->assertEqual($result['Monkey'], $expected);
} }
/** /**
* test that Caches are getting cleared on save(). * test that Caches are getting cleared on save().
* ensure that both inflections of controller names are getting cleared * ensure that both inflections of controller names are getting cleared