getQuery(true); $query->select('COUNT(*)') ->from($db->quoteName('#__' . $table)); if (!empty($conditions)) { foreach ($conditions as $condition) { if (isset($condition[2]) && $condition[2] != '') { $query->where($db->quoteName($condition[0]) . ' ' . $condition[1] . ' ' . $db->quote($condition[2])); } else { $query->where($db->quoteName($condition[0]) . ' ' . $condition[1]); } } } $db->setQuery($query); $total = $db->loadResult(); return $total; } // 02. Get single row from $table where id is $pk public static function get_item($table, $pk = null, $column = 'id') { if ($pk == null) { $input = Factory::getApplication()->input; $pk = $input->get('id', 0, 'int'); } $db = Factory::getDbo(); $query = $db->getQuery(true); $query->select('*') ->from($db->quoteName('#__' . $table)) ->where($db->quoteName($column) . ' = ' . $db->quote($pk)); $db->setQuery($query); $data = $db->loadObject(); return $data; } // 03. Get single record from $table based on the $conditions public static function get_record($table, $conditions = array(), $order = null, $select = array()) { if (empty($select)) { $select = array('*'); } $db = Factory::getDbo(); $query = $db->getQuery(true); $query->select($select) ->from($db->quoteName('#__' . $table)) ->setLimit('1'); if (!empty($conditions)) { foreach ($conditions as $condition) { if (isset($condition[2]) && $condition[2] != '') { $query->where($db->quoteName($condition[0]) . ' ' . $condition[1] . ' ' . $db->quote($condition[2])); } else { $query->where($db->quoteName($condition[0]) . ' ' . $condition[1]); } } } if ($order) { $query->order($order); } $db->setQuery($query); $data = $db->loadObject(); return $data; } // 04. Get multiple rows from $table public static function get_items($table, $conditions = array(), $order = null, $limit = null, $column = null) { $db = Factory::getDbo(); $query = $db->getQuery(true); $select = $column ? $column : '*'; $query->select('*') ->from($db->quoteName('#__' . $table)); if (!empty($conditions)) { foreach ($conditions as $condition) { if (isset($condition[2]) && $condition[2] != '') { $query->where($db->quoteName($condition[0]) . ' ' . $condition[1] . ' ' . $db->quote($condition[2])); } else { $query->where($db->quoteName($condition[0]) . ' ' . $condition[1]); } } } if ($order) { $query->order($order); } if ($limit) { $query->setLimit($limit); } $db->setQuery($query); if ($column) { $data = $db->loadColumn(); } else { $data = $db->loadObjectList(); } return $data; } // 05. Get multiple records from $table; supports joining and select columns public static function get_records($table, $select = '*', $conditions = array(), $order = null, $limit = null, $joins = array()) { $char = 'a'; $db = Factory::getDbo(); $query = $db->getQuery(true); $query->select($select) ->from($db->quoteName('#__' . $table, $char)); if (!empty($conditions)) { foreach ($conditions as $condition) { if (isset($condition[2]) && $condition[2] != '') { $query->where($db->quoteName($condition[0]) . ' ' . $condition[1] . ' ' . $db->quote($condition[2])); } else { $query->where($db->quoteName($condition[0]) . ' ' . $condition[1]); } } } if (!empty($joins)) { $char++; foreach ($joins as $join) { $query->join('LEFT', $db->quoteName('#__' . $join['table'], $char) . ' ON ' . $join['on'] . ' = ' . $char . '.id'); } } if ($order) { $query->order($order); } if ($limit) { $query->setLimit($limit); } $db->setQuery($query); $data = $db->loadObjectList(); return $data; } // 06. Insert or update record in $table public static function insert_update($table, $data, $pk = 'id') { if (is_array($data)) { $data = (object) $data; } if ($pk) { if ($data->id == 0) { $result = Factory::getDbo()->insertObject('#__' . $table, $data, $pk); } else { $result = Factory::getDbo()->updateObject('#__' . $table, $data, $pk); } } else { $result = Factory::getDbo()->insertObject('#__' . $table, $data); } return $data; } // 07. Check if database table exists public static function table_exists($table) { $db = Factory::getDbo(); $tables = $db->getTableList(); $prefix = $db->getPrefix(); if (in_array($prefix . $table, $tables)) { return true; } return false; } // 08. Delete Records public static function delete_items($table, $conditions = array()) { if (empty($conditions)) { return; } $db = Factory::getDbo(); $query = $db->getQuery(true); $query->delete($db->quoteName('#__' . $table)); foreach ($conditions as $condition) { $query->where($db->quoteName($condition[0]) . ' ' . $condition[1] . ' ' . $db->quote($condition[2])); } $db->setQuery($query); $db->execute(); return $db->getAffectedRows(); } // 09. Update records based on conditions public static function update_records($table, $conditions = array(), $sets = array()) { $db = Factory::getDbo(); $query = $db->getQuery(true); $query->update($db->quoteName('#__' . $table)); foreach ($conditions as $condition) { $query->where($db->quoteName($condition[0]) . ' ' . $condition[1] . ' ' . $db->quote($condition[2])); } foreach ($sets as $k => $v) { $query->set($db->quoteName($k) . ' = ' . $db->quote($v)); } $db->setQuery($query); $db->execute(); return $db->getAffectedRows(); } // 10. Truncate table public static function truncate_table($table) { $db = Factory::getDbo(); $db->truncateTable('#__' . $table); } // 11. Get columns in a table public static function get_table_columns($table) { $db = Factory::getDbo(); $cols = $db->getTableColumns('#__' . $table); return $cols; } // 12. Drop table public static function drop_table($table) { if (!self::table_exists($table)) { return false; } $db = Factory::getDbo(); $query = $db->getQuery(true); $query = 'DROP TABLE ' . $db->quoteName('#__' . $table); $db->setQuery($query); $db->execute(); } // 13. Drop column from table public static function drop_column($table, $column) { $db = Factory::getDbo(); $query = $db->getQuery(true); $query = 'ALTER TABLE ' . $db->quoteName('#__' . $table); $query .= ' DROP COLUMN ' . $db->quoteName($column); $db->setQuery($query); $db->execute(); } }