diff --git a/src/Database/DBA.php b/src/Database/DBA.php index 6e9bc89be..9bc314c9c 100644 --- a/src/Database/DBA.php +++ b/src/Database/DBA.php @@ -408,6 +408,23 @@ class DBA return self::$database->selectFirst($table, $fields, $condition, $params); } + /** + * @brief Select rows from a table and fills an array with the data + * + * @param string $table Table name + * @param array $fields Array of selected fields, empty for all + * @param array $condition Array of fields for condition + * @param array $params Array of several parameters + * + * @return array Data array + * @throws \Exception + * @see self::select + */ + public static function selectToArray(string $table, array $fields = [], array $condition = [], array $params = []) + { + return self::$database->selectToArray($table, $fields, $condition, $params); + } + /** * @brief Select rows from a table * diff --git a/src/Database/Database.php b/src/Database/Database.php index c50341691..29d1ac8cc 100644 --- a/src/Database/Database.php +++ b/src/Database/Database.php @@ -1407,6 +1407,23 @@ class Database } } + /** + * @brief Select rows from a table and fills an array with the data + * + * @param string $table Table name + * @param array $fields Array of selected fields, empty for all + * @param array $condition Array of fields for condition + * @param array $params Array of several parameters + * + * @return array Data array + * @throws \Exception + * @see self::select + */ + public function selectToArray(string $table, array $fields = [], array $condition = [], array $params = []) + { + return $this->toArray($this->select($table, $fields, $condition, $params)); + } + /** * @brief Select rows from a table * diff --git a/src/Model/Attach.php b/src/Model/Attach.php index d65e67fe3..758d3ae83 100644 --- a/src/Model/Attach.php +++ b/src/Model/Attach.php @@ -38,7 +38,7 @@ class Attach extends BaseObject } /** - * @brief Select rows from the attach table + * @brief Select rows from the attach table and return them as array * * @param array $fields Array of selected fields, empty for all * @param array $conditions Array of fields for conditions @@ -47,16 +47,15 @@ class Attach extends BaseObject * @return boolean|array * * @throws \Exception - * @see \Friendica\Database\DBA::select + * @see \Friendica\Database\DBA::selectToArray */ - public static function select(array $fields = [], array $conditions = [], array $params = []) + public static function selectToArray(array $fields = [], array $conditions = [], array $params = []) { if (empty($fields)) { $fields = self::getFields(); } - $r = DBA::select('attach', $fields, $conditions, $params); - return DBA::toArray($r); + $r = DBA::selectToArray('attach', $fields, $conditions, $params); } /** @@ -264,7 +263,7 @@ class Attach extends BaseObject { if (!is_null($img)) { // get items to update - $items = self::select(['backend-class','backend-ref'], $conditions); + $items = self::selectToArray(['backend-class','backend-ref'], $conditions); foreach($items as $item) { /** @var IStorage $backend_class */ @@ -297,7 +296,7 @@ class Attach extends BaseObject public static function delete(array $conditions, array $options = []) { // get items to delete data info - $items = self::select(['backend-class','backend-ref'], $conditions); + $items = self::selectToArray(['backend-class','backend-ref'], $conditions); foreach($items as $item) { /** @var IStorage $backend_class */ diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 398acc2d7..f01cb7360 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -117,11 +117,9 @@ class Contact extends BaseObject * @return array * @throws \Exception */ - public static function select(array $fields = [], array $condition = [], array $params = []) + public static function selectToArray(array $fields = [], array $condition = [], array $params = []) { - $statement = DBA::select('contact', $fields, $condition, $params); - - return DBA::toArray($statement); + return DBA::selectToArray('contact', $fields, $condition, $params); } /** diff --git a/src/Model/Item.php b/src/Model/Item.php index 81c60b30b..c2bb6c892 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -368,6 +368,33 @@ class Item extends BaseObject } } + /** + * @brief Select rows from the item table and returns them as an array + * + * @param array $selected Array of selected fields, empty for all + * @param array $condition Array of fields for condition + * @param array $params Array of several parameters + * + * @return array + * @throws \Exception + */ + public static function selectToArray(array $fields = [], array $condition = [], $params = []) + { + $result = self::select($fields, $condition, $params); + + if (is_bool($result)) { + return $result; + } + + $data = []; + while ($row = self::fetch($result)) { + $data[] = $row; + } + DBA::close($result); + + return $data; + } + /** * @brief Select rows from the item table * diff --git a/src/Model/Photo.php b/src/Model/Photo.php index 0e3661b0f..9fa279cf8 100644 --- a/src/Model/Photo.php +++ b/src/Model/Photo.php @@ -29,7 +29,7 @@ require_once "include/dba.php"; class Photo extends BaseObject { /** - * @brief Select rows from the photo table + * @brief Select rows from the photo table and returns them as array * * @param array $fields Array of selected fields, empty for all * @param array $conditions Array of fields for conditions @@ -38,16 +38,15 @@ class Photo extends BaseObject * @return boolean|array * * @throws \Exception - * @see \Friendica\Database\DBA::select + * @see \Friendica\Database\DBA::selectToArray */ - public static function select(array $fields = [], array $conditions = [], array $params = []) + public static function selectToArray(array $fields = [], array $conditions = [], array $params = []) { if (empty($fields)) { $fields = self::getFields(); } - $r = DBA::select("photo", $fields, $conditions, $params); - return DBA::toArray($r); + return DBA::selectToArray('photo', $fields, $conditions, $params); } /** @@ -89,7 +88,7 @@ class Photo extends BaseObject $conditions["resource-id"] = $resourceid; $conditions["uid"] = $uid; - return self::select([], $conditions, $params); + return self::selectToArray([], $conditions, $params); } /** @@ -350,7 +349,7 @@ class Photo extends BaseObject public static function delete(array $conditions, array $options = []) { // get photo to delete data info - $photos = self::select(["backend-class","backend-ref"], $conditions); + $photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions); foreach($photos as $photo) { /** @var IStorage $backend_class */ @@ -380,7 +379,7 @@ class Photo extends BaseObject { if (!is_null($img)) { // get photo to update - $photos = self::select(["backend-class","backend-ref"], $conditions); + $photos = self::selectToArray(['backend-class', 'backend-ref'], $conditions); foreach($photos as $photo) { /** @var IStorage $backend_class */ diff --git a/src/Module/Admin/Blocklist/Contact.php b/src/Module/Admin/Blocklist/Contact.php index 60fe04bf7..de3c717e3 100644 --- a/src/Module/Admin/Blocklist/Contact.php +++ b/src/Module/Admin/Blocklist/Contact.php @@ -53,7 +53,7 @@ class Contact extends BaseAdminModule $pager = new Pager($a->query_string, 30); - $contacts = Model\Contact::select([], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]); + $contacts = Model\Contact::selectToArray([], $condition, ['limit' => [$pager->getStart(), $pager->getItemsPerPage()]]); $t = Renderer::getMarkupTemplate('admin/blocklist/contact.tpl'); $o = Renderer::replaceMacros($t, [ diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 5fceab326..12c93ea2f 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -2176,7 +2176,7 @@ class DFRN if (($entrytype == DFRN::TOP_LEVEL) && !empty($importer['id'])) { // The filling of the the "contact" variable is done for legcy reasons // The functions below are partly used by ostatus.php as well - where we have this variable - $contact = Contact::select([], ['id' => $importer['id']]); + $contact = Contact::selectFirst([], ['id' => $importer['id']]); // Big question: Do we need these functions? They were part of the "consume_feed" function. // This function once was responsible for DFRN and OStatus.