Permalink
Browse files

Some hackery to support array as the return of primaryKey()

  • Loading branch information...
1 parent 9a4d3c9 commit 9db2d00dff844a9ab07e3f8fb38a7f4a69689f8d @lorenzo lorenzo committed Jan 10, 2014
Showing with 17 additions and 17 deletions.
  1. +17 −17 Cake/ORM/Table.php
View
34 Cake/ORM/Table.php
@@ -1181,27 +1181,27 @@ protected function _processSave($entity, $options) {
* @return \Cake\ORM\Entity|boolean
*/
protected function _insert($entity, $data) {
- $query = $this->query();
-
- $primary = $this->primaryKey();
- $id = $this->_newId($primary);
- if ($id !== null) {
- $data[$primary] = $id;
- }
+ $primary = (array)$this->primaryKey();
+ $keys = array_fill(0, count($primary), null);
+ $id = (array)$this->_newId($primary) + $keys;
+ $primary = array_combine($primary, $id);
+ $data = array_filter($primary, 'strlen') + $data;
- $statement = $query->insert(array_keys($data))
+ $statement = $this->query()->insert(array_keys($data))
->values($data)
->execute();
$success = false;
if ($statement->rowCount() > 0) {
- if ($primary && !isset($data[$primary])) {
- $id = $statement->lastInsertId($this->table(), $primary);
- }
- if ($primary && $id !== null) {
- $entity->set($primary, $id);
- }
$success = $entity;
+ $entity->set($primary, ['guard' => false]);
+ foreach ($primary as $key => $value) {
+ if (!isset($data[$key])) {
+ $id = $statement->lastInsertId($this->table(), $key);
+ $entity->set($key, $id);
+ break;
+ }
+ }
}
$statement->closeCursor();
return $success;
@@ -1214,14 +1214,14 @@ protected function _insert($entity, $data) {
* value if possible. You can override this method if you have specific requirements
* for id generation.
*
- * @param string $primary The primary key column to get a new ID for.
+ * @param array $primary The primary key columns to get a new ID for.
* @return null|mixed Either null or the new primary key value.
*/
protected function _newId($primary) {
- if (!$primary) {
+ if (!$primary || count((array)$primary) > 1) {
return null;
}
- $typeName = $this->schema()->columnType($primary);
+ $typeName = $this->schema()->columnType($primary[0]);
$type = Type::build($typeName);
return $type->newId();
}

0 comments on commit 9db2d00

Please sign in to comment.