Skip to content

Commit

Permalink
Add insert/delete/update to ORM\Query
Browse files Browse the repository at this point in the history
Use the repository object reference to get the table name, removing the
need for additional parameters.
  • Loading branch information
markstory committed Dec 28, 2013
1 parent 79cd455 commit 1cd5f8d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
47 changes: 47 additions & 0 deletions Cake/ORM/Query.php
Expand Up @@ -947,4 +947,51 @@ public function _dirty() {
parent::_dirty();
}

/**
* Create an update query.
*
* This changes the query type to be 'update'.
* Can be combined with set() and where() methods to create update queries.
*
* @param string $table Unused parameter.
* @return Query
*/
public function update($table = null) {
$table = $this->repository()->table();
return parent::update($table);
}

/**
* Create a delete query.
*
* This changes the query type to be 'delete'.
* Can be combined with the where() method to create delete queries.
*
* @param string $table Unused parameter.
* @return Query
*/
public function delete($table = null) {
$table = $this->repository()->table();
return parent::delete($table);
}

/**
* Create an insert query.
*
* This changes the query type to be 'insert'.
* Note calling this method will reset any data previously set
* with Query::values()
*
* Can be combined with the where() method to create delete queries.
*
* @param array $columns The columns to insert into.
* @param array $types A map between columns & their datatypes.
* @param array $unused An unused parameter from the parent class' interface
* @return Query
*/
public function insert($columns, $types = [], $unused = []) {
$table = $this->repository()->table();
return parent::insert($table, $columns, $types);
}

}
47 changes: 30 additions & 17 deletions Cake/Test/TestCase/ORM/QueryTest.php
Expand Up @@ -1413,40 +1413,53 @@ public function testCountWithGroup() {
}

/**
* Test that there is no beforeFind event with update queries.
* Test update method.
*
* @return void
*/
public function testUpdateNoBeforeFind() {
public function testUpdate() {
$table = TableRegistry::get('articles');
$table->getEventManager()->attach(function() {
$this->fail('No callback should be fired');
}, 'Model.beforeFind');

$query = $table->query()
->update($table->table())
->set(['title' => 'First']);
$result = $table->query()
->update()
->set(['title' => 'First'])
->execute();

$result = $query->execute();
$this->assertInstanceOf('Cake\Database\StatementInterface', $result);
$this->assertTrue($result->rowCount() > 0);
}

/**
* Test that there is no beforeFind event with delete queries.
* Test insert method.
*
* @return void
*/
public function testDeleteNoBeforeFind() {
public function testInsert() {
$table = TableRegistry::get('articles');
$table->getEventManager()->attach(function() {
$this->fail('No callback should be fired');
}, 'Model.beforeFind');

$query = $table->query()
->delete($table->table());
$result = $table->query()
->insert(['title'])
->values(['title' => 'First'])
->values(['title' => 'Second'])
->execute();

$this->assertInstanceOf('Cake\Database\StatementInterface', $result);
$this->assertEquals(2, $result->rowCount());
}

/**
* Test delete method.
*
* @return void
*/
public function testDelete() {
$table = TableRegistry::get('articles');

$result = $table->query()
->delete()
->where(['id >=' => 1])
->execute();

$result = $query->execute();
$this->assertInstanceOf('Cake\Database\StatementInterface', $result);
$this->assertTrue($result->rowCount() > 0);
}
Expand Down

0 comments on commit 1cd5f8d

Please sign in to comment.