Skip to content
Permalink
Browse files

Saving an empty entity should fail.

Saving entity with no data is a failure as we cannot generate meaningful
SQL for this situation.
  • Loading branch information...
markstory committed Jan 17, 2014
1 parent d95b678 commit 015fb32691c0dd879158c2982244d076cdfd540d
Showing with 19 additions and 4 deletions.
  1. +7 −4 src/ORM/Table.php
  2. +12 −0 tests/TestCase/ORM/TableTest.php
@@ -1189,10 +1189,9 @@ protected function _insert($entity, $data) {
$id = (array)$this->_newId($primary) + $keys;
$primary = array_combine($primary, $id);
$filteredKeys = array_filter($primary, 'strlen');
$total = count($primary);
$data = $filteredKeys + $data;
if ($total > 1) {
if (count($primary) > 1) {
foreach ($primary as $k => $v) {
if (!isset($data[$k])) {
$msg = 'Cannot insert row, some of the primary key values are missing. ';
@@ -1206,11 +1205,15 @@ protected function _insert($entity, $data) {
}
}
$success = false;
if (empty($data)) {
return $success;
}
$statement = $this->query()->insert(array_keys($data))
->values($data)
->execute();
$success = false;
if ($statement->rowCount() > 0) {
$success = $entity;
$entity->set($filteredKeys, ['guard' => false]);
@@ -1263,7 +1266,7 @@ protected function _update($entity, $data) {
$filtered = array_filter($primaryKey, 'strlen');
if (count($filtered) < count($primaryKey)) {
$message = 'A primary key value is needed for updating';
$message = 'All primary key value(s) are needed for updating';
throw new \InvalidArgumentException($message);
}
@@ -1102,6 +1102,18 @@ public function testSaveNewEntity() {
$this->assertEquals($entity->toArray(), $row->toArray());
}
/**
* Test that saving a new empty entity does nothing.
*
* @group save
* @return void
*/
public function testSaveNewEmptyEntity() {
$entity = new \Cake\ORM\Entity();
$table = TableRegistry::get('users');
$this->assertFalse($table->save($entity));
}
/**
* Tests that saving an entity will filter out properties that
* are not present in the table schema when saving

0 comments on commit 015fb32

Please sign in to comment.
You can’t perform that action at this time.