Skip to content

Commit

Permalink
save only changed values, new method reload
Browse files Browse the repository at this point in the history
  • Loading branch information
oscarotero committed Jan 27, 2014
1 parent 4839ae4 commit 46a5460
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 21 deletions.
50 changes: 33 additions & 17 deletions SimpleCrud/Entity.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 18 additions & 3 deletions SimpleCrud/Row.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 9 additions & 1 deletion tests/SimpleCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static function setUpBeforeClass () {
self::$db = $db;
}


public function testAutocreate () {
$db = self::$db;

Expand All @@ -45,6 +46,7 @@ public function testAutocreate () {
$this->assertCount(3, $db->tags_in_posts->getFields());
}


public function testInsert () {
$db = self::$db;

Expand All @@ -65,6 +67,7 @@ public function testInsert () {
$this->assertSame(1, $db->tags->count());
}


public function testRow () {
$db = self::$db;

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -132,6 +140,7 @@ public function testRow () {
$this->assertTrue($post->changed());
}


public function testRelations () {
$db = self::$db;

Expand All @@ -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);

Expand Down

0 comments on commit 46a5460

Please sign in to comment.