Skip to content
Permalink
Browse files

Adding Table::get() as a less verbose alternative for finding a single

record by primary key
  • Loading branch information...
lorenzo committed Dec 20, 2013
1 parent a162606 commit 2c2bb02c9ed7e18131cd967a404f774d1e5700fd
Showing with 22 additions and 0 deletions.
  1. +22 −0 Cake/ORM/Table.php
@@ -790,6 +790,28 @@ public function findThreaded(Query $query, array $options = []) {
return $query;
}
/**
* Returns a single record after finding it by its primary key, if no record is
* found this method throws an exception.
*
* @param mixed primary key value to find
* @param array $options options accepted by `Table::find()`
* @throws \Cake\ORM\RecordNotFoundException
* @return \Cake\ORM\Entity
* @see Table::find()
*/
public function get($primaryKey, $options = []) {
$key = (array)$this->primaryKey();
$conditions = array_combine($key, (array)$primaryKey);
$entity = $this->find('all', $options)->where($conditions)->first();
if (!$entity) {
throw new Error\RecordNotFoundException;
}
return $entity;
}
/**
* Creates a new Query instance for this table
*

0 comments on commit 2c2bb02

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