Skip to content
Permalink
Browse files

Being more strict when not specifying all keys to be inserted, adding

missing tests
  • Loading branch information...
lorenzo committed Jan 16, 2014
1 parent 1d90d29 commit 814cc84c7897bed2885b072f140501b2809d6024
@@ -101,13 +101,28 @@ protected function _stringifyValues($generator) {
continue;
}
$type = $valType = $multiType ? $type[$i] : $type;
$type = $valType = $multiType && isset($type[$i]) ? $type[$i] : $type;
$values[] = $this->_bindValue($generator, $value, $valType);
}
return implode(', ', $values);
}
/**
* Registers a value in the placeholder generator and returns the generated
* placeholder
*
* @param \Cake\Database\ValueBinder $generator
* @param mixed $value
* @param string $type
* @return string generated placeholder
*/
protected function _bindValue($generator, $value, $type) {
$placeholder = $generator->placeholder('tuple');
$generator->bind($placeholder, $value, $type);
return $placeholder;
}
/**
* Traverses the tree of expressions stored in this object, visiting first
* expressions in the left hand side and then the rest.
@@ -1189,8 +1189,23 @@ 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) {
foreach ($primary as $k => $v) {
if (!isset($data[$k])) {
$msg = 'Cannot insert row, some of the primary key values are missing. ';
$msg .= sprintf(
'Got (%s), expecting (%s)',
implode(', ', $filteredKeys + $entity->extract(array_keys($primary))),
implode(', ', array_keys($primary))
);
throw new \RuntimeException($msg);
}
}
}
$statement = $this->query()->insert(array_keys($data))
->values($data)
->execute();
@@ -136,4 +136,17 @@ public function testValueAsSingleExpression() {
$this->assertEquals('(field1, field2) = (SELECT 1, 1)', $f->sql($binder));
}
/**
* Tests that a single ExpressionInteface can be used as the field for
* comparison
*
* @return void
*/
public function testFieldAsSingleExpression() {
$value = [1, 1];
$f = new TupleComparison(new QueryExpression('a, b'), $value);
$binder = new ValueBinder;
$this->assertEquals('(a, b) = (:c0, :c1)', $f->sql($binder));
}
}
@@ -249,6 +249,26 @@ public function testSaveNewEntity() {
$this->assertEquals($entity->toArray(), $row->toArray());
}
/**
* Tests that it is possible to insert a new row using the save method
* if the entity has composite primary key
*
* @group save
* @expectedException \RuntimeException
* @expectedExceptionMessage Cannot insert row, some of the primary key values are missing. Got (5, ), expecting (id, site_id)
* @return void
*/
public function testSaveNewEntityMissingKey() {
$entity = new \Cake\ORM\Entity([
'id' => 5,
'title' => 'Fifth Article',
'body' => 'Fifth Article Body',
'author_id' => 3,
]);
$table = TableRegistry::get('SiteArticles');
$table->save($entity);
}
/**
* Test simple delete with composite primary key
*

0 comments on commit 814cc84

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