Permalink
Browse files

Data returned by Model::save() not contains primary key too when new …

…record is created. Closes #1869
  • Loading branch information...
1 parent fdacc9d commit 7983b4109bba41d871ca7a7b795a72f20ca54c35 @ADmad ADmad committed Aug 13, 2011
View
@@ -1414,6 +1414,9 @@ public function save($data = null, $validate = true, $fieldList = array()) {
if ($success && $count > 0) {
if (!empty($this->data)) {
$success = $this->data;
+ if ($created) {
+ $this->data[$this->alias][$this->primaryKey] = $this->id;
+ }
}
if ($options['callbacks'] === true || $options['callbacks'] === 'after') {
$this->Behaviors->trigger('afterSave', array(&$this, $created, $options));
@@ -69,6 +69,7 @@ public function testAftersaveCallback() {
$expected = array('AfterTree' => array('name' => 'Six and One Half Changed in AfterTree::afterSave() but not in database', 'parent_id' => 6, 'lft' => 11, 'rght' => 12));
$result = $this->Tree->save(array('AfterTree' => array('name' => 'Six and One Half', 'parent_id' => 6)));
+ $expected['AfterTree']['id'] = $this->Tree->id;
$this->assertEqual($expected, $result);
$expected = array('AfterTree' => array('name' => 'Six and One Half', 'parent_id' => 6, 'lft' => 11, 'rght' => 12, 'id' => 8));
@@ -885,37 +885,55 @@ public function testBehaviorSaveCallbacks() {
$Sample->Behaviors->attach('Test', array('beforeSave' => 'off'));
$Sample->create();
- $this->assertIdentical($Sample->save($record), $record);
+ $result = $Sample->save($record);
+ $expected = $record;
+ $expected['Sample']['id'] = $Sample->id;
+ $this->assertIdentical($result, $expected);
$Sample->Behaviors->attach('Test', array('beforeSave' => 'test'));
$Sample->create();
- $this->assertIdentical($Sample->save($record), $record);
+ $result = $Sample->save($record);
+ $expected = $record;
+ $expected['Sample']['id'] = $Sample->id;
+ $this->assertIdentical($result, $expected);
$Sample->Behaviors->attach('Test', array('beforeSave' => 'modify'));
$expected = Set::insert($record, 'Sample.name', 'sample99 modified before');
$Sample->create();
- $this->assertIdentical($Sample->save($record), $expected);
+ $result = $Sample->save($record);
+ $expected['Sample']['id'] = $Sample->id;
+ $this->assertIdentical($result, $expected);
$Sample->Behaviors->disable('Test');
$this->assertIdentical($Sample->save($record), $record);
$Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'on'));
$expected = Set::merge($record, array('Sample' => array('aftersave' => 'modified after on create')));
$Sample->create();
- $this->assertIdentical($Sample->save($record), $expected);
+ $result = $Sample->save($record);
+ $expected['Sample']['id'] = $Sample->id;
+ $this->assertEqual($result, $expected);
$Sample->Behaviors->attach('Test', array('beforeSave' => 'modify', 'afterSave' => 'modify'));
$expected = Set::merge($record, array('Sample' => array('name' => 'sample99 modified before modified after on create')));
$Sample->create();
- $this->assertIdentical($Sample->save($record), $expected);
+ $result = $Sample->save($record);
+ $expected['Sample']['id'] = $Sample->id;
+ $this->assertIdentical($result, $expected);
$Sample->Behaviors->attach('Test', array('beforeSave' => 'off', 'afterSave' => 'test'));
$Sample->create();
- $this->assertIdentical($Sample->save($record), $record);
+ $expected = $record;
+ $result = $Sample->save($record);
+ $expected['Sample']['id'] = $Sample->id;
+ $this->assertIdentical($result, $expected);
$Sample->Behaviors->attach('Test', array('afterSave' => 'test2'));
$Sample->create();
- $this->assertIdentical($Sample->save($record), $record);
+ $expected = $record;
+ $result = $Sample->save($record);
+ $expected['Sample']['id'] = $Sample->id;
+ $this->assertIdentical($result, $expected);
$Sample->Behaviors->attach('Test', array('beforeFind' => 'off', 'afterFind' => 'off'));
$Sample->recursive = -1;
@@ -1023,7 +1041,7 @@ public function testBehaviorValidateMethods() {
$Apple->validate['color'] = 'validateField';
$result = $Apple->save(array('name' => 'Genetically Modified Apple', 'color' => 'Orange'));
- $this->assertEqual(array_keys($result['Apple']), array('name', 'color', 'modified', 'created'));
+ $this->assertEqual(array_keys($result['Apple']), array('name', 'color', 'modified', 'created', 'id'));
$Apple->create();
$result = $Apple->save(array('name' => 'Regular Apple', 'color' => 'Red'));
@@ -55,8 +55,9 @@ public function testInsertAnotherHabtmRecordWithSameForeignKey() {
'updated' => '2008-01-03 10:56:44'
);
$result = $TestModel->JoinAsJoinB->save($data);
- $this->assertEquals($result, array('JoinAsJoinB' => $data));
$lastInsertId = $TestModel->JoinAsJoinB->getLastInsertID();
+ $data['id'] = $lastInsertId;
+ $this->assertEquals($result, array('JoinAsJoinB' => $data));
$this->assertTrue($lastInsertId != null);
$result = $TestModel->JoinAsJoinB->findById(1);
@@ -5294,4 +5295,23 @@ public function testWriteFloatAsGerman() {
setlocale(LC_ALL, $restore);
}
+/**
+ * Test returned array contains primary key when save creates a new record
+ *
+ * @return void
+ */
+ public function testPkInReturnArrayForCreate() {
+ $this->loadFixtures('Article');
+ $TestModel = new Article();
+
+ $data = array('Article' => array(
+ 'user_id' => '1',
+ 'title' => 'Fourth Article',
+ 'body' => 'Fourth Article Body',
+ 'published' => 'Y'
+ ));
+ $result = $TestModel->save($data);
+ $this->assertIdentical($result['Article']['id'], $TestModel->id);
+ }
+
}

3 comments on commit 7983b41

Member

dereuromark commented on 7983b41 Aug 14, 2011

"Data returned by Model::save() not contains primary key too when new record is created. Closes #1869"
not should probably read now :)

Member

ADmad replied Aug 14, 2011

Right its a typo, "not" should be "now". I realized that after I had already pushed couple of other commits so couldn't really do anything about it. Maybe if you had sent a pull request too with required changes for your ticket this would have been avoided :)

Member

dereuromark replied Aug 14, 2011

Well, I guess I take full responsibility then.

Please sign in to comment.