Skip to content
Permalink
Browse files

Update code to create and save entity.

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 162d8b392d65b9e28443e3e5e2af0970e29aa922
Showing with 16 additions and 15 deletions.
  1. +8 −7 src/ORM/Table.php
  2. +8 −8 tests/TestCase/ORM/TableTest.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);
}
/**
@@ -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);

0 comments on commit 162d8b3

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