From 46a5460a158bfd6207fb1a6815951af7a1cf41b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?O=CC=81scar=20Otero?= Date: Tue, 28 Jan 2014 00:17:39 +0100 Subject: [PATCH] save only changed values, new method reload --- SimpleCrud/Entity.php | 50 ++++++++++++++++++++++++++-------------- SimpleCrud/Row.php | 21 ++++++++++++++--- tests/SimpleCrudTest.php | 10 +++++++- 3 files changed, 60 insertions(+), 21 deletions(-) diff --git a/SimpleCrud/Entity.php b/SimpleCrud/Entity.php index 98f5f6e..d0c032a 100644 --- a/SimpleCrud/Entity.php +++ b/SimpleCrud/Entity.php @@ -403,22 +403,40 @@ public function dataFromDatabase (array $data) { /** - * Executes an 'insert' query in the database - * - * @param array $data The values to insert - * @param boolean $duplicateKey Set true if you can avoid duplicate key errors - * - * @return array The new values of the inserted row + * Prepare the data before save into database (used by update and insert) + * + * @param array $data The data to save + * @param bool $new True if it's a new value (insert) + * @param array $changedFields Array of changed fields. If isn't specified, all fields are changed */ - public function insert (array $data, $duplicateKey = false) { + private function getDataToSave (array $data, $new, array $changedFields = null) { if (array_diff_key($data, $this->fields)) { throw new \Exception("Invalid fields"); } - if (!($data = $this->dataToDatabase($data, true))) { + if (!($newData = $this->dataToDatabase($data, $new))) { throw new \Exception("Data not valid"); } + if ($changedFields === null) { + return $newData; + } + + return array_diff_assoc($newData, $data) + array_intersect_key($newData, $changedFields); + } + + + /** + * Executes an 'insert' query in the database + * + * @param array $data The values to insert + * @param boolean $duplicateKey Set true if you can avoid duplicate key errors + * + * @return array The new values of the inserted row + */ + public function insert (array $data, $duplicateKey = false, array $changedFields = null) { + $data = $this->getDataToSave($data, true, $changedFields); + if (empty($data['id'])) { unset($data['id']); } @@ -467,18 +485,16 @@ public function insert (array $data, $duplicateKey = false) { * * @return array The new values of the updated row */ - public function update (array $data, $where = '', $marks = null, $limit = null) { - if (array_diff_key($data, $this->fields)) { - throw new \Exception("Invalid fields"); - } + public function update (array $data, $where = '', $marks = null, $limit = null, array $changedFields = null) { + $data = $this->getDataToSave($data, false, $changedFields); - if (!($data = $this->dataToDatabase($data, false))) { - throw new \Exception("Data not valid"); - } + unset($data['id']); - $quoted = $this->manager->quote($data, $this->getFields(self::FIELDS_DATA_TYPE)); - unset($quoted['id']); + if (empty($data)) { + return $data; + } + $quoted = $this->manager->quote($data, $this->getFields(self::FIELDS_DATA_TYPE)); $set = []; foreach ($this->getFields(self::FIELDS_SQL, array_keys($quoted)) as $name => $field) { diff --git a/SimpleCrud/Row.php b/SimpleCrud/Row.php index a2150f1..92e5165 100644 --- a/SimpleCrud/Row.php +++ b/SimpleCrud/Row.php @@ -121,6 +121,19 @@ public function changed () { } + /** + * Reload the row from the database + */ + public function reload () { + if (!$this->id || !($row = $this->entity->selectBy($this->id))) { + throw new \Exception("This row does not exist in database"); + } + + $this->set($row->get()); + $this->emptyChanges(); + } + + /** * Relate 'has-one' elements with this row * @@ -225,16 +238,18 @@ public function get ($name = null, $onlyChangedValues = false) { * Saves this row in the database * * @param boolean $duplicateKey Set true to detect duplicates index + * @param boolean $onlyChangedValues Set false to save all values instead only the changed * * @return $this */ public function save ($duplicateKey = false, $onlyChangedValues = true) { - $data = $this->get(true, $onlyChangedValues); + $data = $this->get(true); + $onlyChangedValues = $onlyChangedValues ? $this->changes : null; if (empty($this->id)) { - $data = $this->entity->insert($data, $duplicateKey); + $data = $this->entity->insert($data, $duplicateKey, $onlyChangedValues); } else { - $data = $this->entity->update($data, 'id = :id', [':id' => $this->id], 1); + $data = $this->entity->update($data, 'id = :id', [':id' => $this->id], 1, $onlyChangedValues); } $this->set($data); diff --git a/tests/SimpleCrudTest.php b/tests/SimpleCrudTest.php index bbb6f9f..16b8ad4 100644 --- a/tests/SimpleCrudTest.php +++ b/tests/SimpleCrudTest.php @@ -27,6 +27,7 @@ public static function setUpBeforeClass () { self::$db = $db; } + public function testAutocreate () { $db = self::$db; @@ -45,6 +46,7 @@ public function testAutocreate () { $this->assertCount(3, $db->tags_in_posts->getFields()); } + public function testInsert () { $db = self::$db; @@ -65,6 +67,7 @@ public function testInsert () { $this->assertSame(1, $db->tags->count()); } + public function testRow () { $db = self::$db; @@ -97,7 +100,12 @@ public function testRow () { //Check row saving $post->save(); + $this->assertEquals(2, $post->get('id')); + $this->assertEquals('2º post', $post->title); + $this->assertNull($post->categories_id); + $this->assertSame(2, $db->posts->count()); + $post->reload(); $this->assertEquals(2, $post->get('id')); $this->assertEquals('2º post', $post->title); $this->assertNull($post->categories_id); @@ -132,6 +140,7 @@ public function testRow () { $this->assertTrue($post->changed()); } + public function testRelations () { $db = self::$db; @@ -151,7 +160,6 @@ public function testRelations () { $post->setRelation($category)->save(); $this->assertEquals(1, $post->categories_id); - $this->assertInstanceOf('SimpleCrud\\Row', $post->categories); $this->assertEquals(1, $post->categories->id);