From 47ff2df73a4bf22b91f7e5fa96d05bbd26a109d7 Mon Sep 17 00:00:00 2001 From: Anthony Nemirovsky Date: Thu, 13 Nov 2014 10:33:02 -0800 Subject: [PATCH] Add a new findById DAO method. We have found it very useful to have this simple function, which retrieves a DAO object of a specific type by ID or throws an exception if one is not found. This can be called by using something like CRM_Contribute_BAO_Contribution::findById(35), which will retrieve a Contribution Object for that id, if it exists. --- CRM/Core/DAO.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index c0f9c5b055..dea2dd1bc9 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -937,6 +937,22 @@ FROM civicrm_domain return trim($version) == trim($dbVersion) ? TRUE : FALSE; } + /** + * Find a DAO object for the given ID and return it. + * + * @param int $id Id of the DAO object being searched for. + * + * @return object Object of the type of the class that called this function. + */ + static function findById($id) { + $object = new static(); + $object->id = $id; + if (!$object->find(TRUE)) { + throw new Exception("Unable to find a " . get_called_class() . " with id {$id}."); + } + return $object; + } + /** * Given a DAO name, a column name and a column value, find the record and GET the value of another column in that record * -- 2.25.1