diff --git a/core/MY_Model.php b/core/MY_Model.php index 32990da..d09d8b9 100644 --- a/core/MY_Model.php +++ b/core/MY_Model.php @@ -342,16 +342,23 @@ public function update_all($data) */ public function delete($id) { - $this->trigger('before_delete', $id); - - $this->_database->where($this->primary_key, $id); - if ($this->soft_delete) { - $result = $this->_database->update($this->_table, array( $this->soft_delete_key => TRUE )); + $data = $this->as_array() + ->get($id); + + $data = $this->trigger('before_delete', $data); + + $data = array_merge($data, array($this->soft_delete_key => TRUE)); + + $result = $this->update($id, $data); } else { + $this->trigger('before_delete', $id); + + $this->_database->where($this->primary_key, $id); + $result = $this->_database->delete($this->_table); } diff --git a/tests/MY_Model_test.php b/tests/MY_Model_test.php index f45470c..753f664 100644 --- a/tests/MY_Model_test.php +++ b/tests/MY_Model_test.php @@ -557,14 +557,39 @@ public function test_soft_delete() $this->model = new Soft_delete_model(); $this->model->_database = $this->getMock('MY_Model_Mock_DB'); + $this->model->_database->expects($this->at(0)) + ->method('where') + ->with($this->equalTo('deleted'), FALSE); + + $this->model->_database->expects($this->at(1)) + ->method('where') + ->with('id', 2) + ->will($this->returnValue($this->model->_database)); + + $fake_data = array('fake_record_here' => 'fake data'); + $this->model->_database->expects($this->once()) - ->method('where') - ->with($this->equalTo('id'), $this->equalTo(2)) - ->will($this->returnValue($this->model->_database)); + ->method('row_array') + ->will($this->returnValue($fake_data)); + + $this->_expect_get(); + + $this->model->_database->expects($this->at(4)) + ->method('where') + ->with('id', 2) + ->will($this->returnValue($this->model->_database)); + + $fake_data = array_merge($fake_data, array('deleted' => TRUE)); + $this->model->_database->expects($this->once()) - ->method('update') - ->with($this->equalTo('records'), $this->equalTo(array( 'deleted' => TRUE ))) - ->will($this->returnValue(TRUE)); + ->method('set') + ->with($fake_data) + ->will($this->returnValue($this->model->_database)); + + $this->model->_database->expects($this->once()) + ->method('update') + ->with('records') + ->will($this->returnValue(TRUE)); $this->assertEquals($this->model->delete(2), TRUE); } @@ -573,15 +598,40 @@ public function test_soft_delete_custom_key() { $this->model = new Soft_delete_model('record_deleted'); $this->model->_database = $this->getMock('MY_Model_Mock_DB'); + + $this->model->_database->expects($this->at(0)) + ->method('where') + ->with($this->equalTo('record_deleted'), FALSE); + + $this->model->_database->expects($this->at(1)) + ->method('where') + ->with('id', 2) + ->will($this->returnValue($this->model->_database)); + + $fake_data = array('fake_record_here' => 'fake data'); $this->model->_database->expects($this->once()) - ->method('where') - ->with($this->equalTo('id'), $this->equalTo(2)) - ->will($this->returnValue($this->model->_database)); + ->method('row_array') + ->will($this->returnValue($fake_data)); + + $this->_expect_get(); + + $this->model->_database->expects($this->at(4)) + ->method('where') + ->with('id', 2) + ->will($this->returnValue($this->model->_database)); + + $fake_data = array_merge($fake_data, array('record_deleted' => TRUE)); + $this->model->_database->expects($this->once()) - ->method('update') - ->with($this->equalTo('records'), $this->equalTo(array( 'record_deleted' => TRUE ))) - ->will($this->returnValue(TRUE)); + ->method('set') + ->with($fake_data) + ->will($this->returnValue($this->model->_database)); + + $this->model->_database->expects($this->once()) + ->method('update') + ->with('records') + ->will($this->returnValue(TRUE)); $this->assertEquals($this->model->delete(2), TRUE); }