8000 Fix getChildren/hasChildren/getSiblings/hasSiblings on unsaved new models by blankse · Pull Request #11111 · pimcore/pimcore · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Fix getChildren/hasChildren/getSiblings/hasSiblings on unsaved new models #11111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions models/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
36 changes: 30 additions & 6 deletions models/Asset/Dao.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -326,6 +330,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();
Expand All @@ -346,7 +354,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;
}
Expand All @@ -360,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();
Expand All @@ -370,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()]);
}

/**
Expand Down
16 changes: 10 additions & 6 deletions models/Asset/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
47 changes: 29 additions & 18 deletions models/DataObject/AbstractObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down
34 changes: 27 additions & 7 deletions models/DataObject/AbstractObject/Dao.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -299,6 +303,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())) {
Expand Down Expand Up @@ -332,15 +340,25 @@ 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';
}

$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;
}
Expand All @@ -355,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)) {
Expand All @@ -368,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()]);
}

/**
Expand Down
8 changes: 4 additions & 4 deletions models/DataObject/Classificationstore/GroupConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
final class GroupConfig extends Model\AbstractModel
{
/**
* @var int
* @var int|null
*/
protected $id;

Expand All @@ -41,7 +41,7 @@ final class GroupConfig extends Model\AbstractModel
/**
* Parent id
*
* @var int
* @var int|null
*/
protected $parentId;

Expand Down Expand Up @@ -145,15 +145,15 @@ public function setId($id)
}

/**
* @return int
* @return int|null
*/
public function getId()
{
return $this->id;
}

/**
* @return int
* @return int|null
*/
public function getParentId()
{
Expand Down
9 changes: 3 additions & 6 deletions models/DataObject/Classificationstore/GroupConfig/Dao.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
}

/**
Expand Down
44 changes: 27 additions & 17 deletions models/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -979,9 +989,9 @@ public function getCreationDate()
/**
* {@inheritdoc}
*/
public function getId(): int
public function getId(): ?int
{
return (int) $this->id;
return $this->id;
}

/**
Expand Down
Loading
0