summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorFrédéric Guillot <fred@kanboard.net>2014-11-22 18:22:10 -0500
committerFrédéric Guillot <fred@kanboard.net>2014-11-22 18:22:10 -0500
commit77e10d25829f3523a168bf61424fac99a539f8be (patch)
tree9b3d274146fa9543c4734a945e61b9d975960ff6 /app
parent15038cdb10f8c691edc7980fd1aed32dcbed3f9f (diff)
Improve API to return id instead of a boolean
Diffstat (limited to 'app')
-rw-r--r--app/Model/Category.php11
-rw-r--r--app/Model/Comment.php17
-rw-r--r--app/Model/SubTask.php18
-rw-r--r--app/Model/User.php12
4 files changed, 42 insertions, 16 deletions
diff --git a/app/Model/Category.php b/app/Model/Category.php
index fb54594b..913c3e35 100644
--- a/app/Model/Category.php
+++ b/app/Model/Category.php
@@ -94,11 +94,18 @@ class Category extends Base
*
* @access public
* @param array $values Form values
- * @return bool
+ * @return bool|integer
*/
public function create(array $values)
{
- return $this->db->table(self::TABLE)->save($values);
+ return $this->db->transaction(function($db) use ($values) {
+
+ if (! $db->table(Category::TABLE)->save($values)) {
+ return false;
+ }
+
+ return (int) $db->getConnection()->getLastId();
+ });
}
/**
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;
+ });
}
/**
diff --git a/app/Model/SubTask.php b/app/Model/SubTask.php
index 886ad1f3..6cb4ec4e 100644
--- a/app/Model/SubTask.php
+++ b/app/Model/SubTask.php
@@ -138,19 +138,25 @@ class SubTask extends Base
*
* @access public
* @param array $values Form values
- * @return bool
+ * @return bool|integer
*/
public function create(array $values)
{
$this->prepare($values);
- $result = $this->db->table(self::TABLE)->save($values);
- if ($result) {
- $values['id'] = $this->db->getConnection()->getLastId();
+ return $this->db->transaction(function($db) use ($values) {
+
+ if (! $db->table(SubTask::TABLE)->save($values)) {
+ return false;
+ }
+
+ $subtask_id = (int) $db->getConnection()->getLastId();
+ $values['id'] = $subtask_id;
+
$this->event->trigger(self::EVENT_CREATE, $values);
- }
- return $result;
+ return $subtask_id;
+ });
}
/**
diff --git a/app/Model/User.php b/app/Model/User.php
index 41bad0bb..3dd5993d 100644
--- a/app/Model/User.php
+++ b/app/Model/User.php
@@ -255,12 +255,20 @@ class User extends Base
*
* @access public
* @param array $values Form values
- * @return boolean
+ * @return boolean|integer
*/
public function create(array $values)
{
$this->prepare($values);
- return $this->db->table(self::TABLE)->save($values);
+
+ return $this->db->transaction(function($db) use ($values) {
+
+ if (! $db->table(User::TABLE)->save($values)) {
+ return false;
+ }
+
+ return (int) $db->getConnection()->getLastId();
+ });
}
/**