From c1be30bc96fd222635a7a6e3479d92f27941a13c Mon Sep 17 00:00:00 2001 From: Sebastian Blank Date: Thu, 30 Dec 2021 18:24:19 +0100 Subject: [PATCH 1/6] Fix getChildren/hasChildren/getSiblings/hasSiblings on unsaved new objects --- models/DataObject/AbstractObject.php | 47 +++++++++++++++--------- models/DataObject/AbstractObject/Dao.php | 18 ++++++++- 2 files changed, 45 insertions(+), 20 deletions(-) diff --git a/models/DataObject/AbstractObject.php b/models/DataObject/AbstractObject.php index e92479b38f8..919126be165 100644 --- a/models/DataObject/AbstractObject.php +++ b/models/DataObject/AbstractObject.php @@ -464,14 +464,19 @@ public function getChildren(array $objectTypes = [self::OBJECT_TYPE_OBJECT, self $cacheKey = $this->getListingCacheKey(func_get_args()); if (!isset($this->o_children[$cacheKey])) { - $list = new Listing(); - $list->setUnpublished($includingUnpublished); - $list->setCondition('o_parentId = ?', $this->getId()); - $list->setOrderKey(sprintf('o_%s', $this->getChildrenSortBy())); - $list->setOrder($this->getChildrenSortOrder()); - $list->setObjectTypes($objectTypes); - $this->o_children[$cacheKey] = $list->load(); - $this->o_hasChildren[$cacheKey] = (bool) count($this->o_children[$cacheKey]); + if ($this->getId()) { + $list = new Listing(); + $list->setUnpublished($includingUnpublished); + $list->setCondition('o_parentId = ?', $this->getId()); + $list->setOrderKey(sprintf('o_%s', $this->getChildrenSortBy())); + $list->setOrder($this->getChildrenSortOrder()); + $list->setObjectTypes($objectTypes); + $this->o_children[$cacheKey] = $list->load(); + $this->o_hasChildren[$cacheKey] = (bool) count($this->o_children[$cacheKey]); + } else { + $this->o_children[$cacheKey] = []; + $this->o_hasChildren[$cacheKey] = false; + } } return $this->o_children[$cacheKey]; @@ -509,16 +514,22 @@ public function getSiblings(array $objectTypes = [self::OBJECT_TYPE_OBJECT, self $cacheKey = $this->getListingCacheKey(func_get_args()); if (!isset($this->o_siblings[$cacheKey])) { - $list = new Listing(); - $list->setUnpublished($includingUnpublished); - // string conversion because parentId could be 0 - $list->addConditionParam('o_parentId = ?', (string)$this->getParentId()); - $list->addConditionParam('o_id != ?', $this->getId()); - $list->setOrderKey('o_key'); - $list->setObjectTypes($objectTypes); - $list->setOrder('asc'); - $this->o_siblings[$cacheKey] = $list->load(); - $this->o_hasSiblings[$cacheKey] = (bool) count($this->o_siblings[$cacheKey]); + if ($this->getParentId()) { + $list = new Listing(); + $list->setUnpublished($includingUnpublished); + $list->addConditionParam('o_parentId = ?', $this->getParentId()); + if ($this->getId()) { + $list->addConditionParam('o_id != ?', $this->getId()); + } + $list->setOrderKey('o_key'); + $list->setObjectTypes($objectTypes); + $list->setOrder('asc'); + $this->o_siblings[$cacheKey] = $list->load(); + $this->o_hasSiblings[$cacheKey] = (bool) count($this->o_siblings[$cacheKey]); + } else { + $this->o_siblings[$cacheKey] = []; + $this->o_hasSiblings[$cacheKey] = false; + } } return $this->o_siblings[$cacheKey]; diff --git a/models/DataObject/AbstractObject/Dao.php b/models/DataObject/AbstractObject/Dao.php index d27d43e95ad..87a0a05f116 100644 --- a/models/DataObject/AbstractObject/Dao.php +++ b/models/DataObject/AbstractObject/Dao.php @@ -299,6 +299,10 @@ public function deleteAllPermissions() */ public function hasChildren($objectTypes = [DataObject::OBJECT_TYPE_OBJECT, DataObject::OBJECT_TYPE_FOLDER], $includingUnpublished = null, $user = null) { + if (!$this->model->getId()) { + return false; + } + $sql = 'SELECT 1 FROM objects o WHERE o_parentId = ?'; if ((isset($includingUnpublished) && !$includingUnpublished) || (!isset($includingUnpublished) && Model\Document::doHideUnpublished())) { @@ -332,7 +336,17 @@ public function hasChildren($objectTypes = [DataObject::OBJECT_TYPE_OBJECT, Data */ public function hasSiblings($objectTypes = [DataObject::OBJECT_TYPE_OBJECT, DataObject::OBJECT_TYPE_FOLDER], $includingUnpublished = null) { - $sql = 'SELECT 1 FROM objects WHERE o_parentId = ? and o_id != ?'; + if (!$this->model->getParentId()) { + return false; + } + + $sql = 'SELECT 1 FROM objects WHERE o_parentId = ?'; + $params = [$this->model->getParentId()]; + + if ($this->model->getId()) { + $sql .= ' AND o_id != ?'; + $params[] = $this->model->getId(); + } if ((isset($includingUnpublished) && !$includingUnpublished) || (!isset($includingUnpublished) && Model\Document::doHideUnpublished())) { $sql .= ' AND o_published = 1'; @@ -340,7 +354,7 @@ public function hasSiblings($objectTypes = [DataObject::OBJECT_TYPE_OBJECT, Data $sql .= " AND o_type IN ('" . implode("','", $objectTypes) . "') LIMIT 1"; - $c = $this->db->fetchOne($sql, [$this->model->getParentId(), $this->model->getId()]); + $c = $this->db->fetchOne($sql, $params); return (bool)$c; } From 85684ff58a1aad4f2134faf6bbad23012e2887fb Mon Sep 17 00:00:00 2001 From: Sebastian Blank Date: Fri, 31 Dec 2021 17:33:59 +0100 Subject: [PATCH 2/6] Fix getChildren/hasChildren/getSiblings/hasSiblings --- models/Asset.php | 19 +++++--- models/Asset/Dao.php | 20 ++++++++- models/Asset/Folder.php | 16 ++++--- .../Classificationstore/GroupConfig.php | 8 ++-- .../Classificationstore/GroupConfig/Dao.php | 9 ++-- models/Document.php | 44 ++++++++++++------- models/Document/Dao.php | 18 +++++++- models/Element/Tag.php | 12 +++-- models/User/AbstractUser/Dao.php | 4 ++ models/User/Folder.php | 12 +++-- models/User/UserRole/Folder.php | 30 ++++++++----- 11 files changed, 129 insertions(+), 63 deletions(-) diff --git a/models/Asset.php b/models/Asset.php index dd60bccdb4f..0a04615432c 100644 --- a/models/Asset.php +++ b/models/Asset.php @@ -965,13 +965,18 @@ public function getRealFullPath() public function getSiblings() { if ($this->siblings === null) { - $list = new Asset\Listing(); - // string conversion because parentId could be 0 - $list->addConditionParam('parentId = ?', (string)$this->getParentId()); - $list->addConditionParam('id != ?', $this->getId()); - $list->setOrderKey('filename'); - $list->setOrder('asc'); - $this->siblings = $list->getAssets(); + if ($this->getParentId()) { + $list = new Asset\Listing(); + $list->addConditionParam('parentId = ?', $this->getParentId()); + if ($this->getId()) { + $list->addConditionParam('id != ?', $this->getId()); + } + $list->setOrderKey('filename'); + $list->setOrder('asc'); + $this->siblings = $list->getAssets(); + } else { + $this->siblings = []; + } } return $this->siblings; diff --git a/models/Asset/Dao.php b/models/Asset/Dao.php index 83933992b80..76f48777fff 100644 --- a/models/Asset/Dao.php +++ b/models/Asset/Dao.php @@ -326,6 +326,10 @@ public function getVersionCountForUpdate(): int */ public function hasChildren($user = null) { + if (!$this->model->getId()) { + return false; + } + $query = 'SELECT `a`.`id` FROM `assets` a WHERE `parentId` = ?'; if ($user && !$user->isAdmin()) { $userIds = $user->getRoles(); @@ -346,7 +350,21 @@ public function hasChildren($user = null) */ public function hasSiblings() { - $c = $this->db->fetchOne('SELECT id FROM assets WHERE parentId = ? and id != ? LIMIT 1', [$this->model->getParentId(), $this->model->getId()]); + if (!$this->model->getParentId()) { + return false; + } + + $sql = 'SELECT 1 FROM assets WHERE parentId = ?'; + $params = [$this->model->getParentId()]; + + if ($this->model->getId()) { + $sql .= ' AND id != ?'; + $params[] = $this->model->getId(); + } + + $sql .= ' LIMIT 1'; + + $c = $this->db->fetchOne($sql, $params); return (bool)$c; } diff --git a/models/Asset/Folder.php b/models/Asset/Folder.php index f2fe0831f77..a269ac070d4 100644 --- a/models/Asset/Folder.php +++ b/models/Asset/Folder.php @@ -69,12 +69,16 @@ public function setChildren($children) public function getChildren() { if ($this->children === null) { - $list = new Asset\Listing(); - $list->setCondition('parentId = ?', $this->getId()); - $list->setOrderKey('filename'); - $list->setOrder('asc'); - - $this->children = $list->getAssets(); + if ($this->getId()) { + $list = new Asset\Listing(); + $list->setCondition('parentId = ?', $this->getId()); + $list->setOrderKey('filename'); + $list->setOrder('asc'); + + $this->children = $list->getAssets(); + } else { + $this->children = []; + } } return $this->children; diff --git a/models/DataObject/Classificationstore/GroupConfig.php b/models/DataObject/Classificationstore/GroupConfig.php index 8975c3427b0..af958226d76 100644 --- a/models/DataObject/Classificationstore/GroupConfig.php +++ b/models/DataObject/Classificationstore/GroupConfig.php @@ -27,7 +27,7 @@ final class GroupConfig extends Model\AbstractModel { /** - * @var int + * @var int|null */ protected $id; @@ -41,7 +41,7 @@ final class GroupConfig extends Model\AbstractModel /** * Parent id * - * @var int + * @var int|null */ protected $parentId; @@ -145,7 +145,7 @@ public function setId($id) } /** - * @return int + * @return int|null */ public function getId() { @@ -153,7 +153,7 @@ public function getId() } /** - * @return int + * @return int|null */ public function getParentId() { diff --git a/models/DataObject/Classificationstore/GroupConfig/Dao.php b/models/DataObject/Classificationstore/GroupConfig/Dao.php index 224f992e01f..2fd99f881b0 100644 --- a/models/DataObject/Classificationstore/GroupConfig/Dao.php +++ b/models/DataObject/Classificationstore/GroupConfig/Dao.php @@ -78,14 +78,11 @@ public function getByName($name = null) */ public function hasChildren() { - $amount = 0; - - try { - $amount = (int) $this->db->fetchOne('SELECT COUNT(*) as amount FROM ' . self::TABLE_NAME_GROUPS . ' where parentId= ' . $this->model->getId()); - } catch (\Exception $e) { + if (!$this->model->getId()) { + return 0; } - return $amount; + return (int) $this->db->fetchOne('SELECT COUNT(*) as amount FROM ' . self::TABLE_NAME_GROUPS . ' WHERE parentId = ?', [$this->model->getId()]); } /** diff --git a/models/Document.php b/models/Document.php index aa421cbbdf0..a2b0e6b8e92 100644 --- a/models/Document.php +++ b/models/Document.php @@ -674,12 +674,16 @@ public function getChildren($includingUnpublished = false) $cacheKey = $this->getListingCacheKey(func_get_args()); if (!isset($this->children[$cacheKey])) { - $list = new Document\Listing(); - $list->setUnpublished($includingUnpublished); - $list->setCondition('parentId = ?', $this->getId()); - $list->setOrderKey('index'); - $list->setOrder('asc'); - $this->children[$cacheKey] = $list->load(); + if ($this->getId()) { + $list = new Document\Listing(); + $list->setUnpublished($includingUnpublished); + $list->setCondition('parentId = ?', $this->getId()); + $list->setOrderKey('index'); + $list->setOrder('asc'); + $this->children[$cacheKey] = $list->load(); + } else { + $this->children[$cacheKey] = []; + } } return $this->children[$cacheKey]; @@ -715,15 +719,21 @@ public function getSiblings($includingUnpublished = false) $cacheKey = $this->getListingCacheKey(func_get_args()); if (!isset($this->siblings[$cacheKey])) { - $list = new Document\Listing(); - $list->setUnpublished($includingUnpublished); - // string conversion because parentId could be 0 - $list->addConditionParam('parentId = ?', (string)$this->getParentId()); - $list->addConditionParam('id != ?', $this->getId()); - $list->setOrderKey('index'); - $list->setOrder('asc'); - $this->siblings[$cacheKey] = $list->load(); - $this->hasSiblings[$cacheKey] = (bool) count($this->siblings[$cacheKey]); + if ($this->getParentId()) { + $list = new Document\Listing(); + $list->setUnpublished($includingUnpublished); + $list->addConditionParam('parentId = ?', $this->getParentId()); + if ($this->getId()) { + $list->addConditionParam('id != ?', $this->getId()); + } + $list->setOrderKey('index'); + $list->setOrder('asc'); + $this->siblings[$cacheKey] = $list->load(); + $this->hasSiblings[$cacheKey] = (bool) count($this->siblings[$cacheKey]); + } else { + $this->siblings[$cacheKey] = []; + $this->hasSiblings[$cacheKey] = false; + } } return $this->siblings[$cacheKey]; @@ -979,9 +989,9 @@ public function getCreationDate() /** * {@inheritdoc} */ - public function getId(): int + public function getId(): ?int { - return (int) $this->id; + return $this->id; } /** diff --git a/models/Document/Dao.php b/models/Document/Dao.php index 6df2e7b95d5..e968e7f91aa 100644 --- a/models/Document/Dao.php +++ b/models/Document/Dao.php @@ -332,6 +332,10 @@ public function deleteAllPermissions() */ public function hasChildren($includingUnpublished = null, $user = null) { + if (!$this->model->getId()) { + return false; + } + $sql = 'SELECT id FROM documents d WHERE parentId = ?'; if ((isset($includingUnpublished) && !$includingUnpublished) || (!isset($includingUnpublished) && Model\Document::doHideUnpublished())) { @@ -383,7 +387,17 @@ public function getChildAmount($user = null) */ public function hasSiblings($includingUnpublished = null) { - $sql = 'SELECT id FROM documents WHERE parentId = ? and id != ?'; + if (!$this->model->getParentId()) { + return false; + } + + $sql = 'SELECT id FROM documents WHERE parentId = ?'; + $params = [$this->model->getParentId()]; + + if ($this->model->getId()) { + $sql .= ' AND id != ?'; + $params[] = $this->model->getId(); + } if ((isset($includingUnpublished) && !$includingUnpublished) || (!isset($includingUnpublished) && Model\Document::doHideUnpublished())) { $sql .= ' AND published = 1'; @@ -391,7 +405,7 @@ public function hasSiblings($includingUnpublished = null) $sql .= ' LIMIT 1'; - $c = $this->db->fetchOne($sql, [$this->model->getParentId(), $this->model->getId()]); + $c = $this->db->fetchOne($sql, $params); return (bool)$c; } diff --git a/models/Element/Tag.php b/models/Element/Tag.php index 1160d5fe203..1bbbd1f675a 100644 --- a/models/Element/Tag.php +++ b/models/Element/Tag.php @@ -345,10 +345,14 @@ public function __toString() public function getChildren() { if ($this->children == null) { - $listing = new Tag\Listing(); - $listing->setCondition('parentId = ?', $this->getId()); - $listing->setOrderKey('name'); - $this->children = $listing->load(); + if ($this->getId()) { + $listing = new Tag\Listing(); + $listing->setCondition('parentId = ?', $this->getId()); + $listing->setOrderKey('name'); + $this->children = $listing->load(); + } else { + $this->children = []; + } } return $this->children; diff --git a/models/User/AbstractUser/Dao.php b/models/User/AbstractUser/Dao.php index febcbc1565e..3fa57e87386 100644 --- a/models/User/AbstractUser/Dao.php +++ b/models/User/AbstractUser/Dao.php @@ -80,6 +80,10 @@ public function create() */ public function hasChildren() { + if (!$this->model->getId()) { + return false; + } + $c = $this->db->fetchOne('SELECT id FROM users WHERE parentId = ?', $this->model->getId()); return (bool) $c; diff --git a/models/User/Folder.php b/models/User/Folder.php index 1609068d69a..dc96105d170 100644 --- a/models/User/Folder.php +++ b/models/User/Folder.php @@ -30,11 +30,15 @@ class Folder extends UserRole\Folder */ public function getChildren() { - if (empty($this->children)) { - $list = new Listing(); - $list->setCondition('parentId = ?', $this->getId()); + if ($this->children === null) { + if ($this->getId()) { + $list = new Listing(); + $list->setCondition('parentId = ?', $this->getId()); - $this->children = $list->getUsers(); + $this->children = $list->getUsers(); + } else { + $this->children = []; + } } return $this->children; diff --git a/models/User/UserRole/Folder.php b/models/User/UserRole/Folder.php index 7aec918d7e5..0b131dc2ac5 100644 --- a/models/User/UserRole/Folder.php +++ b/models/User/UserRole/Folder.php @@ -28,14 +28,14 @@ class Folder extends Model\User\AbstractUser /** * @internal * - * @var array + * @var array|null */ - protected $children = []; + protected $children; /** * @internal * - * @var bool + * @var bool|null */ protected $hasChildren; @@ -58,11 +58,15 @@ public function hasChildren() */ public function getChildren() { - if (empty($this->children)) { - $list = new Role\Listing(); - $list->setCondition('parentId = ?', $this->getId()); + if ($this->children === null) { + if ($this->getId()) { + $list = new Role\Listing(); + $list->setCondition('parentId = ?', $this->getId()); - $this->children = $list->getRoles(); + $this->children = $list->getRoles(); + } else { + $this->children = []; + } } return $this->children; @@ -75,11 +79,13 @@ public function getChildren() */ public function setChildren($children) { - $this->children = $children; - if (is_array($children) && count($children) > 0) { - $this->hasChildren = true; - } else { - $this->hasChildren = false; + if (is_array($children)) { + $this->children = $children; + if (count($children) > 0) { + $this->hasChildren = true; + } else { + $this->hasChildren = false; + } } return $this; From ba924f3ad58a812408ee4e8ee1799df9af26432d Mon Sep 17 00:00:00 2001 From: Sebastian Blank Date: Fri, 31 Dec 2021 17:43:16 +0100 Subject: [PATCH 3/6] Fix test --- models/Document/Dao.php | 7 +++++-- models/User/AbstractUser/Dao.php | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/models/Document/Dao.php b/models/Document/Dao.php index e968e7f91aa..f13ce441e0b 100644 --- a/models/Document/Dao.php +++ b/models/Document/Dao.php @@ -235,10 +235,13 @@ public function getCurrentFullPath() */ public function getVersionCountForUpdate(): int { - $versionCount = (int) $this->db->fetchOne('SELECT versionCount FROM documents WHERE id = ? FOR UPDATE', $this->model->getId()); + if (!$this->model->getId()) { + return 0; + } + $versionCount = (int) $this->db->fetchOne('SELECT versionCount FROM documents WHERE id = ? FOR UPDATE', [$this->model->getId()]); if ($this->model instanceof PageSnippet) { - $versionCount2 = (int) $this->db->fetchOne("SELECT MAX(versionCount) FROM versions WHERE cid = ? AND ctype = 'document'", $this->model->getId()); + $versionCount2 = (int) $this->db->fetchOne("SELECT MAX(versionCount) FROM versions WHERE cid = ? AND ctype = 'document'", [$this->model->getId()]); $versionCount = max($versionCount, $versionCount2); } diff --git a/models/User/AbstractUser/Dao.php b/models/User/AbstractUser/Dao.php index 3fa57e87386..7cc9559eed3 100644 --- a/models/User/AbstractUser/Dao.php +++ b/models/User/AbstractUser/Dao.php @@ -84,7 +84,7 @@ public function hasChildren() return false; } - $c = $this->db->fetchOne('SELECT id FROM users WHERE parentId = ?', $this->model->getId()); + $c = $this->db->fetchOne('SELECT id FROM users WHERE parentId = ?', [$this->model->getId()]); return (bool) $c; } From 73e839731eb9506123ba28e41d6bd2d2eae41727 Mon Sep 17 00:00:00 2001 From: Sebastian Blank Date: Sat, 1 Jan 2022 14:27:45 +0100 Subject: [PATCH 4/6] Update models/User/UserRole/Folder.php Co-authored-by: Jacob Dreesen --- models/User/UserRole/Folder.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/models/User/UserRole/Folder.php b/models/User/UserRole/Folder.php index 0b131dc2ac5..5fea717d0af 100644 --- a/models/User/UserRole/Folder.php +++ b/models/User/UserRole/Folder.php @@ -81,11 +81,7 @@ public function setChildren($children) { if (is_array($children)) { $this->children = $children; - if (count($children) > 0) { - $this->hasChildren = true; - } else { - $this->hasChildren = false; - } + $this->hasChildren = count($children) > 0; } return $this; From 37caca3eb223279d31e4dbd1de43a54a48daf7ba Mon Sep 17 00:00:00 2001 From: Sebastian Blank Date: Mon, 3 Jan 2022 11:58:32 +0100 Subject: [PATCH 5/6] Fix getVersionCountForUpdate method --- models/Asset/Dao.php | 8 ++++++-- models/DataObject/AbstractObject/Dao.php | 8 ++++++-- models/Document/Dao.php | 1 + 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/models/Asset/Dao.php b/models/Asset/Dao.php index 76f48777fff..184631a5824 100644 --- a/models/Asset/Dao.php +++ b/models/Asset/Dao.php @@ -307,10 +307,14 @@ public function getCurrentFullPath() */ public function getVersionCountForUpdate(): int { - $versionCount = (int) $this->db->fetchOne('SELECT versionCount FROM assets WHERE id = ? FOR UPDATE', $this->model->getId()); + if (!$this->model->getId()) { + return 0; + } + + $versionCount = (int) $this->db->fetchOne('SELECT versionCount FROM assets WHERE id = ? FOR UPDATE', [$this->model->getId()]); if (!$this->model instanceof Folder) { - $versionCount2 = (int) $this->db->fetchOne("SELECT MAX(versionCount) FROM versions WHERE cid = ? AND ctype = 'asset'", $this->model->getId()); + $versionCount2 = (int) $this->db->fetchOne("SELECT MAX(versionCount) FROM versions WHERE cid = ? AND ctype = 'asset'", [$this->model->getId()]); $versionCount = max($versionCount, $versionCount2); } diff --git a/models/DataObject/AbstractObject/Dao.php b/models/DataObject/AbstractObject/Dao.php index 87a0a05f116..19f8d6c3736 100644 --- a/models/DataObject/AbstractObject/Dao.php +++ b/models/DataObject/AbstractObject/Dao.php @@ -216,10 +216,14 @@ public function getCurrentFullPath() */ public function getVersionCountForUpdate(): int { - $versionCount = (int) $this->db->fetchOne('SELECT o_versionCount FROM objects WHERE o_id = ? FOR UPDATE', $this->model->getId()); + if (!$this->model->getId()) { + return 0; + } + + $versionCount = (int) $this->db->fetchOne('SELECT o_versionCount FROM objects WHERE o_id = ? FOR UPDATE', [$this->model->getId()]); if ($this->model instanceof DataObject\Concrete) { - $versionCount2 = (int) $this->db->fetchOne("SELECT MAX(versionCount) FROM versions WHERE cid = ? AND ctype = 'object'", $this->model->getId()); + $versionCount2 = (int) $this->db->fetchOne("SELECT MAX(versionCount) FROM versions WHERE cid = ? AND ctype = 'object'", [$this->model->getId()]); $versionCount = max($versionCount, $versionCount2); } diff --git a/models/Document/Dao.php b/models/Document/Dao.php index f13ce441e0b..9a445542501 100644 --- a/models/Document/Dao.php +++ b/models/Document/Dao.php @@ -238,6 +238,7 @@ public function getVersionCountForUpdate(): int if (!$this->model->getId()) { return 0; } + $versionCount = (int) $this->db->fetchOne('SELECT versionCount FROM documents WHERE id = ? FOR UPDATE', [$this->model->getId()]); if ($this->model instanceof PageSnippet) { From c7914f6b4328761c1ffcf14a569d647e667083b0 Mon Sep 17 00:00:00 2001 From: Sebastian Blank Date: Mon, 3 Jan 2022 12:08:37 +0100 Subject: [PATCH 6/6] Fix getChildAmount method --- models/Asset/Dao.php | 8 +++++--- models/DataObject/AbstractObject/Dao.php | 10 ++++++---- models/Document/Dao.php | 7 +++++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/models/Asset/Dao.php b/models/Asset/Dao.php index 184631a5824..e97c1503413 100644 --- a/models/Asset/Dao.php +++ b/models/Asset/Dao.php @@ -382,6 +382,10 @@ public function hasSiblings() */ public function getChildAmount($user = null) { + if (!$this->model->getId()) { + return 0; + } + if ($user && !$user->isAdmin()) { $userIds = $user->getRoles(); $userIds[] = $user->getId(); @@ -392,9 +396,7 @@ public function getChildAmount($user = null) $query = 'SELECT COUNT(*) AS count FROM assets WHERE parentId = ?'; } - $c = $this->db->fetchOne($query, $this->model->getId()); - - return $c; + return (int) $this->db->fetchOne($query, [$this->model->getId()]); } /** diff --git a/models/DataObject/AbstractObject/Dao.php b/models/DataObject/AbstractObject/Dao.php index 19f8d6c3736..249c1527ebc 100644 --- a/models/DataObject/AbstractObject/Dao.php +++ b/models/DataObject/AbstractObject/Dao.php @@ -306,7 +306,7 @@ public function hasChildren($objectTypes = [DataObject::OBJECT_TYPE_OBJECT, Data if (!$this->model->getId()) { return false; } - + $sql = 'SELECT 1 FROM objects o WHERE o_parentId = ?'; if ((isset($includingUnpublished) && !$includingUnpublished) || (!isset($includingUnpublished) && Model\Document::doHideUnpublished())) { @@ -373,6 +373,10 @@ public function hasSiblings($objectTypes = [DataObject::OBJECT_TYPE_OBJECT, Data */ public function getChildAmount($objectTypes = [DataObject::OBJECT_TYPE_OBJECT, DataObject::OBJECT_TYPE_FOLDER], $user = null) { + if (!$this->model->getId()) { + return 0; + } + $query = 'SELECT COUNT(*) AS count FROM objects o WHERE o_parentId = ?'; if (!empty($objectTypes)) { @@ -386,9 +390,7 @@ public function getChildAmount($objectTypes = [DataObject::OBJECT_TYPE_OBJECT, D $query .= ' AND (select list as locate from users_workspaces_object where userId in (' . implode(',', $userIds) . ') and LOCATE(cpath,CONCAT(o.o_path,o.o_key))=1 ORDER BY LENGTH(cpath) DESC LIMIT 1)=1;'; } - $c = $this->db->fetchOne($query, $this->model->getId()); - - return $c; + return (int) $this->db->fetchOne($query, [$this->model->getId()]); } /** diff --git a/models/Document/Dao.php b/models/Document/Dao.php index 9a445542501..0c384ddadbb 100644 --- a/models/Document/Dao.php +++ b/models/Document/Dao.php @@ -368,6 +368,10 @@ public function hasChildren($includingUnpublished = null, $user = null) */ public function getChildAmount($user = null) { + if (!$this->model->getId()) { + return 0; + } + if ($user && !$user->isAdmin()) { $userIds = $user->getRoles(); $userIds[] = $user->getId(); @@ -377,9 +381,8 @@ public function getChildAmount($user = null) } else { $query = 'SELECT COUNT(*) AS count FROM documents WHERE parentId = ?'; } - $c = $this->db->fetchOne($query, $this->model->getId()); - return $c; + return (int) $this->db->fetchOne($query, [$this->model->getId()]); } /**