From 77e10d25829f3523a168bf61424fac99a539f8be Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 22 Nov 2014 18:22:10 -0500 Subject: Improve API to return id instead of a boolean --- app/Model/Comment.php | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) (limited to 'app/Model/Comment.php') diff --git a/app/Model/Comment.php b/app/Model/Comment.php index cd361b1d..8ef89094 100644 --- a/app/Model/Comment.php +++ b/app/Model/Comment.php @@ -99,20 +99,25 @@ class Comment extends Base * * @access public * @param array $values Form values - * @return boolean + * @return boolean|integer */ public function create(array $values) { $values['date'] = time(); - if ($this->db->table(self::TABLE)->save($values)) { + return $this->db->transaction(function($db) use ($values) { + + if (! $db->table(Comment::TABLE)->save($values)) { + return false; + } + + $comment_id = (int) $db->getConnection()->getLastId(); + $values['id'] = $comment_id; - $values['id'] = $this->db->getConnection()->getLastId(); $this->event->trigger(self::EVENT_CREATE, $values); - return true; - } - return false; + return $comment_id; + }); } /** -- cgit v1.2.3