Queries are executed with {@link \Elgg\Database::executeQuery()} and results
are retrieved with {@link \PDO::fetchObject()}. If a callback
function $callback is defined, each row will be passed as a single
argument to $callback. If no callback function is defined, the
entire result set is returned as an array.
public getData ( string $query, callable $callback = null, array $params = [] ) : array | ||
$query | string | The query being passed. |
$callback | callable | Optionally, the name of a function to call back to on each row |
$params | array | Query params. E.g. [1, 'steve'] or [':id' => 1, ':name' => 'steve'] |
return | array | An array of database result objects or callback function results. If the query returned nothing, an empty array. |
/** * Get all the relationships for a given GUID. * * @param int $guid GUID of the subject or target entity (see $inverse) * @param bool $inverse_relationship Is $guid the target of the deleted relationships? By default $guid is * the subject of the relationships. * * @return \ElggRelationship[] */ public function getAll($guid, $inverse_relationship = false) { $guid = (int) $guid; $where = $inverse_relationship ? "guid_two='{$guid}'" : "guid_one='{$guid}'"; $query = "SELECT * from {$this->db->getTablePrefix()}entity_relationships WHERE {$where}"; return $this->db->getData($query, array($this, 'rowToElggRelationship')); }
/** * Get all the relationships for a given GUID. * * @param int $guid GUID of the subject or target entity (see $inverse) * @param bool $inverse_relationship Is $guid the target of the deleted relationships? By default $guid is * the subject of the relationships. * * @return \ElggRelationship[] */ function getAll($guid, $inverse_relationship = false) { $guid = (int) $guid; $where = $inverse_relationship ? "guid_two='{$guid}'" : "guid_one='{$guid}'"; $query = "SELECT * from {$this->db->getTablePrefix()}entity_relationships WHERE {$where}"; return $this->db->getData($query, "row_to_elggrelationship"); }
/** * Get all the relationships for a given GUID. * * @param int $guid GUID of the subject or target entity (see $inverse) * @param bool $inverse_relationship Is $guid the target of the deleted relationships? By default $guid is * the subject of the relationships. * * @return \ElggRelationship[] */ public function getAll($guid, $inverse_relationship = false) { $params[':guid'] = (int) $guid; $where = $inverse_relationship ? "guid_two = :guid" : "guid_one = :guid"; $query = "SELECT * from {$this->db->prefix}entity_relationships WHERE {$where}"; return $this->db->getData($query, [$this, 'rowToElggRelationship'], $params); }
/** * Get a populated cache object * * @return array */ protected function getPopulatedCache() { if ($this->cache === null) { $rows = $this->db->getData("\n\t\t\t\tSELECT *\n\t\t\t\tFROM {$this->db->prefix}entity_subtypes\n\t\t\t"); $this->cache = []; foreach ($rows as $row) { $this->cache[$row->id] = $row; } } return $this->cache; }
/** * Load entire datalist in memory. * * This could cause OOM problems if the datalists table is large. * * @todo make a list of datalists that we want to get in one grab * * @return array * @access private */ function loadAll() { $result = $this->db->getData("SELECT * FROM {$this->table}"); $map = array(); if (is_array($result)) { foreach ($result as $row) { $map[$row->name] = $row->value; $this->cache->put($row->name, $row->value); } } return $map; }
/** * Get subscription records from the database * * Records are an object with two vars: guid and methods with the latter * being a comma-separated list of subscription relationship names. * * @param int $container_guid The GUID of the subscription target * @return array */ protected function getSubscriptionRecords($container_guid) { $container_guid = $this->db->sanitizeInt($container_guid); // create IN clause $rels = $this->getMethodRelationships(); if (!$rels) { return array(); } array_walk($rels, array($this->db, 'sanitizeString')); $methods_string = "'" . implode("','", $rels) . "'"; $db_prefix = $this->db->prefix; $query = "SELECT guid_one AS guid, GROUP_CONCAT(relationship SEPARATOR ',') AS methods\n\t\t\tFROM {$db_prefix}entity_relationships\n\t\t\tWHERE guid_two = {$container_guid} AND\n\t\t\t\t\trelationship IN ({$methods_string}) GROUP BY guid_one"; return $this->db->getData($query); }
/** * Gets all ids associated with this string when taken case-insensitively. * Will add the string to the table if not present. * * @param string $string The value * * @return int[] */ private function getIdCaseInsensitive($string) { $string = (string) $string; // caching doesn't work for case insensitive requests $escaped_string = $this->db->sanitizeString($string); $query = "SELECT * FROM {$this->getTableName()} WHERE string = '{$escaped_string}'"; $results = $this->db->getData($query); $ids = array(); foreach ($results as $result) { $ids[] = $result->id; } if (empty($ids)) { $ids[] = $this->add($string); } return $ids; }
/** * Return an array of all private settings. * * @param int $entity_guid The entity GUID * * @return string[] empty array if no settings */ public function getAll($entity_guid) { if (!$this->entities->exists($entity_guid)) { return []; } $query = "\n\t\t\tSELECT * FROM {$this->table}\n\t\t\tWHERE entity_guid = :entity_guid\n\t\t"; $params = [':entity_guid' => (int) $entity_guid]; $result = $this->db->getData($query, null, $params); $return = []; if ($result) { foreach ($result as $r) { $return[$r->name] = $r->value; } } return $return; }
/** * Return an array of all private settings. * * @param int $entity_guid The entity GUID * * @return string[] empty array if no settings */ function getAll($entity_guid) { $entity_guid = (int) $entity_guid; $entity = $this->entities->get($entity_guid); if (!$entity instanceof \ElggEntity) { return false; } $query = "SELECT * FROM {$this->table} WHERE entity_guid = {$entity_guid}"; $result = $this->db->getData($query); if ($result) { $return = array(); foreach ($result as $r) { $return[$r->name] = $r->value; } return $return; } return array(); }
/** * Checks if mutex is locked * * @param string $namespace Namespace to use for the database table * @return bool */ public function isLocked($namespace) { $this->assertNamespace($namespace); return (bool) count($this->db->getData("SHOW TABLES LIKE '{$this->db->getTablePrefix()}{$namespace}_lock'")); }
/** * {@inheritdoc} */ public function getData($query, $callback = '', array $params = []) { return $this->db->getData($query, $callback, $params); }
/** * Populate the boot data * * @param \stdClass $config Elgg CONFIG object * @param \Elgg\Database $db Elgg database * @param EntityTable $entities Entities service * @param Plugins $plugins Plugins service * * @return void * @throws \InstallationException */ public function populate(\stdClass $config, Database $db, EntityTable $entities, Plugins $plugins) { // get subtypes $rows = $db->getData("\n\t\t\tSELECT *\n\t\t\tFROM {$db->prefix}entity_subtypes\n\t\t"); foreach ($rows as $row) { $this->subtype_data[$row->id] = $row; } // get config $rows = $db->getData("\n\t\t\tSELECT *\n\t\t\tFROM {$db->prefix}config\n\t\t"); foreach ($rows as $row) { $this->config_values[$row->name] = unserialize($row->value); } if (!array_key_exists('installed', $this->config_values)) { // try to fetch from old pre 3.0 datalists table // need to do this to be able to perform an upgrade from 2.x to 3.0 try { $rows = $db->getData("\n\t\t\t\t\tSELECT *\n\t\t\t\t\tFROM {$db->prefix}datalists\n\t\t\t\t"); foreach ($rows as $row) { $value = $row->value; if ($row->name == 'processed_upgrades') { // config table already serializes data so no need to double serialize $value = unserialize($value); } $this->config_values[$row->name] = $value; } } catch (\Exception $e) { } } // get site entity $this->site = $entities->get($this->config_values['default_site'], 'site'); if (!$this->site) { throw new \InstallationException("Unable to handle this request. This site is not configured or the database is down."); } // get plugins $this->active_plugins = $plugins->find('active'); // get plugin settings if (!$this->active_plugins) { return; } // find GUIDs with not too many private settings $guids = array_map(function (\ElggPlugin $plugin) { return $plugin->guid; }, $this->active_plugins); // find plugin GUIDs with not too many settings $limit = 40; $set = implode(',', $guids); $sql = "\n\t\t\tSELECT entity_guid\n\t\t\tFROM {$db->prefix}private_settings\n\t\t\tWHERE entity_guid IN ({$set})\n\t\t\t AND name NOT LIKE 'plugin:user_setting:%'\n\t\t\t AND name NOT LIKE 'elgg:internal:%'\n\t\t\tGROUP BY entity_guid\n\t\t\tHAVING COUNT(*) > {$limit}\n\t\t"; $unsuitable_guids = $db->getData($sql, function ($row) { return (int) $row->entity_guid; }); $guids = array_values($guids); $guids = array_diff($guids, $unsuitable_guids); if ($guids) { // get the settings $set = implode(',', $guids); $rows = $db->getData("\n\t\t\t\tSELECT entity_guid, `name`, `value`\n\t\t\t\tFROM {$db->prefix}private_settings\n\t\t\t\tWHERE entity_guid IN ({$set})\n\t\t\t\t AND name NOT LIKE 'plugin:user_setting:%'\n\t\t\t\t AND name NOT LIKE 'elgg:internal:%'\n\t\t\t\tORDER BY entity_guid\n\t\t\t"); // make sure we show all entities as loaded $this->plugin_settings = array_fill_keys($guids, []); foreach ($rows as $i => $row) { $this->plugin_settings[$row->entity_guid][$row->name] = $row->value; } } }
/** * Returns a list of months in which entities were updated or created. * * @tip Use this to generate a list of archives by month for when entities were added or updated. * * @todo document how to pass in array for $subtype * * @warning Months are returned in the form YYYYMM. * * @param string $type The type of entity * @param string $subtype The subtype of entity * @param int $container_guid The container GUID that the entities belong to * @param string $order_by Order_by SQL order by clause * * @return array|false Either an array months as YYYYMM, or false on failure */ public function getDates($type = '', $subtype = '', $container_guid = 0, $order_by = 'time_created') { $where = array(); if ($type != "") { $type = sanitise_string($type); $where[] = "type='{$type}'"; } if (is_array($subtype)) { $tempwhere = ""; if (sizeof($subtype)) { foreach ($subtype as $typekey => $subtypearray) { foreach ($subtypearray as $subtypeval) { $typekey = sanitise_string($typekey); if (!empty($subtypeval)) { if (!($subtypeval = (int) get_subtype_id($typekey, $subtypeval))) { return false; } } else { $subtypeval = 0; } if (!empty($tempwhere)) { $tempwhere .= " or "; } $tempwhere .= "(type = '{$typekey}' and subtype = {$subtypeval})"; } } } if (!empty($tempwhere)) { $where[] = "({$tempwhere})"; } } else { if ($subtype) { if (!($subtype_id = get_subtype_id($type, $subtype))) { return false; } else { $where[] = "subtype={$subtype_id}"; } } } if ($container_guid !== 0) { if (is_array($container_guid)) { foreach ($container_guid as $key => $val) { $container_guid[$key] = (int) $val; } $where[] = "container_guid in (" . implode(",", $container_guid) . ")"; } else { $container_guid = (int) $container_guid; $where[] = "container_guid = {$container_guid}"; } } $where[] = _elgg_get_access_where_sql(array('table_alias' => '')); $sql = "SELECT DISTINCT EXTRACT(YEAR_MONTH FROM FROM_UNIXTIME(time_created)) AS yearmonth\n\t\t\tFROM {$this->db->prefix}entities where "; foreach ($where as $w) { $sql .= " {$w} and "; } $sql .= "1=1 ORDER BY {$order_by}"; if ($result = $this->db->getData($sql)) { $endresult = array(); foreach ($result as $res) { $endresult[] = $res->yearmonth; } return $endresult; } return false; }