summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFrederic Guillot <fred@kanboard.net>2016-01-03 18:18:14 -0500
committerFrederic Guillot <fred@kanboard.net>2016-01-03 18:18:14 -0500
commit0751508ae3e1e2731e7c699b69c873e31ecc9ab7 (patch)
tree8a0fabcbdef995b4d22979aafc6a6e720dc7c445
parente2b6d4fa18d5f7bcdd0f44b9c952aa80e02285de (diff)
Reduce number of SQL queries for actions
-rw-r--r--app/Model/Action.php41
-rw-r--r--app/Model/ActionParameter.php45
-rw-r--r--composer.json1
-rw-r--r--composer.lock52
4 files changed, 118 insertions, 21 deletions
diff --git a/app/Model/Action.php b/app/Model/Action.php
index c368b494..5fcfbaa7 100644
--- a/app/Model/Action.php
+++ b/app/Model/Action.php
@@ -34,10 +34,8 @@ class Action extends Base
if (! empty($project_ids)) {
$actions = $this->db->table(self::TABLE)->in('project_id', $project_ids)->findAll();
-
- foreach ($actions as &$action) {
- $action['params'] = $this->actionParameter->getAll($action['id']);
- }
+ $params = $this->actionParameter->getAllByActions(array_column($actions, 'id'));
+ $this->attachParamsToActions($actions, $params);
}
return $actions;
@@ -53,12 +51,8 @@ class Action extends Base
public function getAllByProject($project_id)
{
$actions = $this->db->table(self::TABLE)->eq('project_id', $project_id)->findAll();
-
- foreach ($actions as &$action) {
- $action['params'] = $this->actionParameter->getAll($action['id']);
- }
-
- return $actions;
+ $params = $this->actionParameter->getAllByActions(array_column($actions, 'id'));
+ return $this->attachParamsToActions($actions, $params);
}
/**
@@ -70,12 +64,8 @@ class Action extends Base
public function getAll()
{
$actions = $this->db->table(self::TABLE)->findAll();
-
- foreach ($actions as &$action) {
- $action['params'] = $this->actionParameter->getAll($action['id']);
- }
-
- return $actions;
+ $params = $this->actionParameter->getAll();
+ return $this->attachParamsToActions($actions, $params);
}
/**
@@ -90,13 +80,30 @@ class Action extends Base
$action = $this->db->table(self::TABLE)->eq('id', $action_id)->findOne();
if (! empty($action)) {
- $action['params'] = $this->actionParameter->getAll($action_id);
+ $action['params'] = $this->actionParameter->getAllByAction($action_id);
}
return $action;
}
/**
+ * Attach parameters to actions
+ *
+ * @access private
+ * @param array &$actions
+ * @param array &$params
+ * @return array
+ */
+ private function attachParamsToActions(array &$actions, array &$params)
+ {
+ foreach ($actions as &$action) {
+ $action['params'] = isset($params[$action['id']]) ? $params[$action['id']] : array();
+ }
+
+ return $actions;
+ }
+
+ /**
* Remove an action
*
* @access public
diff --git a/app/Model/ActionParameter.php b/app/Model/ActionParameter.php
index f170ef66..1e4d7544 100644
--- a/app/Model/ActionParameter.php
+++ b/app/Model/ActionParameter.php
@@ -24,10 +24,53 @@ class ActionParameter extends Base
* Get all action params
*
* @access public
+ * @return array
+ */
+ public function getAll()
+ {
+ $params = $this->db->table(self::TABLE)->findAll();
+ return $this->toDictionary($params);
+ }
+
+ /**
+ * Get all params for a list of actions
+ *
+ * @access public
+ * @param array $action_ids
+ * @return array
+ */
+ public function getAllByActions(array $action_ids)
+ {
+ $params = $this->db->table(self::TABLE)->in('action_id', $action_ids)->findAll();
+ return $this->toDictionary($params);
+ }
+
+ /**
+ * Build params dictionary
+ *
+ * @access private
+ * @param array $params
+ * @return array
+ */
+ private function toDictionary(array $params)
+ {
+ $result = array();
+
+ foreach ($params as $param) {
+ $result[$param['action_id']][$param['name']] = $param['value'];
+ }
+
+ return $result;
+ }
+
+ /**
+ * Get all action params for a given action
+ *
+ * @access public
* @param integer $action_id
* @return array
*/
- public function getAll($action_id)
+ public function getAllByAction($action_id)
{
return $this->db->hashtable(self::TABLE)->eq('action_id', $action_id)->getAll('name', 'value');
}
diff --git a/composer.json b/composer.json
index 69840ec9..b2eed1f1 100644
--- a/composer.json
+++ b/composer.json
@@ -26,6 +26,7 @@
"fguillot/simple-validator" : "1.0.0",
"league/html-to-markdown" : "~4.0",
"pimple/pimple" : "~3.0",
+ "ramsey/array_column": "@stable",
"swiftmailer/swiftmailer" : "~5.4",
"symfony/console" : "~2.7",
"symfony/event-dispatcher" : "~2.7",
diff --git a/composer.lock b/composer.lock
index 76bc3d11..d2549d4f 100644
--- a/composer.lock
+++ b/composer.lock
@@ -4,8 +4,8 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically"
],
- "hash": "8230f229ff6260e337d500aea1b42429",
- "content-hash": "87b8db514c808c0775bd52ad72c1a71d",
+ "hash": "061fd1874ea29264f1aeee3d9394f441",
+ "content-hash": "85eaeb7f843881473efd22773a1e97d4",
"packages": [
{
"name": "christian-riesen/base32",
@@ -544,6 +544,51 @@
"time": "2012-12-21 11:40:51"
},
{
+ "name": "ramsey/array_column",
+ "version": "1.1.3",
+ "source": {
+ "type": "git",
+ "url": "https://github.com/ramsey/array_column.git",
+ "reference": "f8e52eb28e67eb50e613b451dd916abcf783c1db"
+ },
+ "dist": {
+ "type": "zip",
+ "url": "https://api.github.com/repos/ramsey/array_column/zipball/f8e52eb28e67eb50e613b451dd916abcf783c1db",
+ "reference": "f8e52eb28e67eb50e613b451dd916abcf783c1db",
+ "shasum": ""
+ },
+ "require-dev": {
+ "jakub-onderka/php-parallel-lint": "0.8.*",
+ "phpunit/phpunit": "~4.5",
+ "satooshi/php-coveralls": "0.6.*",
+ "squizlabs/php_codesniffer": "~2.2"
+ },
+ "type": "library",
+ "autoload": {
+ "files": [
+ "src/array_column.php"
+ ]
+ },
+ "notification-url": "https://packagist.org/downloads/",
+ "license": [
+ "MIT"
+ ],
+ "authors": [
+ {
+ "name": "Ben Ramsey",
+ "homepage": "http://benramsey.com"
+ }
+ ],
+ "description": "Provides functionality for array_column() to projects using PHP earlier than version 5.5.",
+ "homepage": "https://github.com/ramsey/array_column",
+ "keywords": [
+ "array",
+ "array_column",
+ "column"
+ ],
+ "time": "2015-03-20 22:07:39"
+ },
+ {
"name": "swiftmailer/swiftmailer",
"version": "v5.4.1",
"source": {
@@ -830,7 +875,8 @@
"aliases": [],
"minimum-stability": "stable",
"stability-flags": {
- "fguillot/picodb": 20
+ "fguillot/picodb": 20,
+ "ramsey/array_column": 0
},
"prefer-stable": false,
"prefer-lowest": false,