Skip to content
Permalink
Browse files

#8671 Improving Table::findOrCreate()

  • Loading branch information...
burzum committed Apr 21, 2016
1 parent 623e662 commit 7c104a9c910d4f6762fe3ce0fd5f7bc5e5082bf6
Showing with 37 additions and 2 deletions.
  1. +21 −2 src/ORM/Table.php
  2. +16 −0 tests/TestCase/ORM/TableTest.php
@@ -1213,15 +1213,15 @@ public function get($primaryKey, $options = [])
* called allowing you to define additional default values. The new
* entity will be saved and returned.
*
* @param array $search The criteria to find existing records by.
* @param array|\Cake\ORM\Query|string $search The criteria to find existing records by.
* @param callable|null $callback A callback that will be invoked for newly
* created entities. This callback will be called *before* the entity
* is persisted.
* @return \Cake\Datasource\EntityInterface An entity.
*/
public function findOrCreate($search, callable $callback = null)
{
$query = $this->find()->where($search);
$query = $this->_getFindOrCreateQuery($search);
$row = $query->first();
if ($row) {
return $row;
@@ -1234,6 +1234,25 @@ public function findOrCreate($search, callable $callback = null)
return $this->save($entity) ?: $entity;
}
/**
* Gets the query object for findOrCreate().
*
* @param array|\Cake\ORM\Query|string $search The criteria to find existing records by.
* @return \Cake\ORM\Query
*/
protected function _getFindOrCreateQuery($search) {
if ($search instanceof Query) {
return $search;
} elseif (is_string($search)) {
if (method_exists($this, $search)) {
return $this->{$search}();
} else {
throw new InvalidArgumentException('Method `' . $search . '` does not exist!');
}
}
return $this->find()->where($search);
}
/**
* {@inheritDoc}
*/
@@ -5383,6 +5383,22 @@ function ($article) {
$this->assertEquals('New body', $article->body);
$this->assertEquals('N', $article->published);
$this->assertEquals(2, $article->author_id);
$query = $articles->find()->where(['author_id' => 2, 'title' => 'First Article']);
$article = $articles->findOrCreate($query);
$this->assertEquals('First Article', $article->title);
$this->assertEquals(2, $article->author_id);
}
/**
* Test that exceptions from the findOrCreate are thrown.
*
* @expectedException \InvalidArgumentException
*/
public function testFindOrCreateException()
{
$articles = TableRegistry::get('Articles');
$articles->findOrCreate('doesNotExist');
}
/**

0 comments on commit 7c104a9

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