Skip to content

Commit

Permalink
Update code to create and save entity.
Browse files Browse the repository at this point in the history
The additional defaults are no longer applied to existing entities. All
entities returned from findOrCreate() will always be persisted.
  • Loading branch information
markstory committed Sep 21, 2014
1 parent a83685c commit 162d8b3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
15 changes: 8 additions & 7 deletions src/ORM/Table.php
Expand Up @@ -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);
}

/**
Expand Down
16 changes: 8 additions & 8 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -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);
Expand Down

0 comments on commit 162d8b3

Please sign in to comment.