order documents by id in getDocumentByName(), return null if not document was found

This commit is contained in:
Uwe Steinmann 2021-09-22 09:40:25 +02:00
parent b777b786dd
commit 96f8c1db21

View File

@ -748,12 +748,12 @@ class SeedDMS_Core_DMS {
* *
* This function searches a document by its name and restricts the search * This function searches a document by its name and restricts the search
* to the given folder if passed as the second parameter. * to the given folder if passed as the second parameter.
* If there are more than one document with that name, then only the first * If there are more than one document with that name, then only the
* one will be returned. * one with the highest id will be returned.
* *
* @param string $name * @param string $name Name of the document
* @param object $folder * @param object $folder parent folder of document
* @return SeedDMS_Core_Document|boolean found document or false * @return SeedDMS_Core_Document|null|boolean found document or null if not document was found or false in case of an error
*/ */
function getDocumentByName($name, $folder=null) { /* {{{ */ function getDocumentByName($name, $folder=null) { /* {{{ */
$name = trim($name); $name = trim($name);
@ -765,14 +765,14 @@ class SeedDMS_Core_DMS {
"WHERE `tblDocuments`.`name` = " . $this->db->qstr($name); "WHERE `tblDocuments`.`name` = " . $this->db->qstr($name);
if($folder) if($folder)
$queryStr .= " AND `tblDocuments`.`folder` = ". $folder->getID(); $queryStr .= " AND `tblDocuments`.`folder` = ". $folder->getID();
$queryStr .= " LIMIT 1"; $queryStr .= " ORDER BY `tblDocuments`.`id` DESC LIMIT 1";
$resArr = $this->db->getResultArray($queryStr); $resArr = $this->db->getResultArray($queryStr);
if (is_bool($resArr) && !$resArr) if (is_bool($resArr) && !$resArr)
return false; return false;
if(!$resArr) if(!$resArr)
return false; return null;
$row = $resArr[0]; $row = $resArr[0];
/** @var SeedDMS_Core_Document $document */ /** @var SeedDMS_Core_Document $document */