Skip to content
Permalink
Browse files

Allow top level array containing a table name.

CakePHP has in the past added the top level model name to the POST data
as a container. Be compatible with that convention as it is still kind
of useful.
  • Loading branch information...
markstory committed Dec 15, 2013
1 parent b897091 commit 59e1cdd78038d73b573fc4f5208cfe46bb050896
Showing with 33 additions and 1 deletion.
  1. +6 −1 Cake/ORM/Marshaller.php
  2. +27 −0 Cake/Test/TestCase/ORM/MarshallerTest.php
@@ -62,9 +62,14 @@ public function __construct(Table $table, $safe = false) {
public function one(array $data, $include = []) {
$include = Hash::normalize($include);
$tableName = $this->_table->alias();
$entityClass = $this->_table->entityClass();
$entity = new $entityClass();
if (isset($data[$tableName])) {
$data = $data[$tableName];
}
foreach ($data as $key => $value) {
$assoc = null;
if (array_key_exists($key, $include)) {
@@ -77,6 +77,33 @@ public function testOneSimple() {
$this->assertNull($result->isNew(), 'Should be detached');
}
/**
* test one() with a wrapping model name.
*
* @return void
*/
public function testOneWithAdditionalName() {
$data = [
'Articles' => [
'title' => 'My title',
'body' => 'My content',
'author_id' => 1,
'not_in_schema' => true,
'Users' => [
'username' => 'mark',
]
]
];
$marshall = new Marshaller($this->articles);
$result = $marshall->one($data, ['Users']);
$this->assertInstanceOf('Cake\ORM\Entity', $result);
$this->assertTrue($result->dirty(), 'Should be a dirty entity.');
$this->assertNull($result->isNew(), 'Should be detached');
$this->assertEquals($data['Articles']['title'], $result->title);
$this->assertEquals($data['Articles']['Users']['username'], $result->user->username);
}
/**
* test one() with association data.
*

0 comments on commit 59e1cdd

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