diff --git a/src/ORM/Table.php b/src/ORM/Table.php index 7b2fb48190a..560422dd939 100644 --- a/src/ORM/Table.php +++ b/src/ORM/Table.php @@ -939,21 +939,22 @@ public function get($primaryKey, $options = []) { * * Using the attributes defined in $search a find() will be done to locate * an existing record. If that record exists it will be returned. If it does - * not exist, a new entity will be created. In both cases, the $additional properties - * will be patched into the entity. + * not exist, a new entity will be created with the $search properties, and + * the $defaults. When a new entity is created, it will be saved. * * @param array $search The criteria to find existing records by. - * @param array $additional The array of additional attributes to patch into - * the new or existing entity. + * @param array $defaults The array of defaults to patch into + * the new entity if it is created. * @return \Cake\Datasource\EntityInterface An entity. */ - public function findOrNew($search, $additional = []) { + public function findOrCreate($search, $defaults = []) { $query = $this->find()->where($search); $row = $query->first(); if ($row) { - return $this->patchEntity($row, $additional); + return $row; } - return $this->newEntity($search + $additional); + $entity = $this->newEntity($search + $defaults); + return $this->save($entity); } /** diff --git a/tests/TestCase/ORM/TableTest.php b/tests/TestCase/ORM/TableTest.php index 536f8d87f94..aa297f518b3 100644 --- a/tests/TestCase/ORM/TableTest.php +++ b/tests/TestCase/ORM/TableTest.php @@ -3537,26 +3537,26 @@ public function testDebugInfo() { */ public function testFindOrNew() { $articles = TableRegistry::get('Articles'); - $article = $articles->findOrNew(['title' => 'Not there'], ['body' => 'New body']); + $article = $articles->findOrCreate(['title' => 'Not there'], ['body' => 'New body']); - $this->assertTrue($article->isNew()); - $this->assertNull($article->id); + $this->assertFalse($article->isNew()); + $this->assertNotNull($article->id); $this->assertEquals('Not there', $article->title); $this->assertEquals('New body', $article->body); - $article = $articles->findOrNew(['title' => 'First Article'], ['body' => 'New body']); + $article = $articles->findOrCreate(['title' => 'First Article'], ['body' => 'New body']); $this->assertFalse($article->isNew()); $this->assertNotNull($article->id); $this->assertEquals('First Article', $article->title); - $this->assertEquals('New body', $article->body); + $this->assertNotEquals('New body', $article->body); - $article = $articles->findOrNew( + $article = $articles->findOrCreate( ['author_id' => 2, 'title' => 'First Article'], ['published' => 'N', 'body' => 'New body'] ); - $this->assertTrue($article->isNew()); - $this->assertNull($article->id); + $this->assertFalse($article->isNew()); + $this->assertNotNull($article->id); $this->assertEquals('First Article', $article->title); $this->assertEquals('New body', $article->body); $this->assertEquals('N', $article->published);