[DB][Attachments] Use count function rathar than fetch and count, rename to refCount, rather than countDepencies
This commit is contained in:
parent
8880405dee
commit
39006fb6b5
@ -75,10 +75,9 @@ class Avatar extends Component
|
||||
return Event::next;
|
||||
}
|
||||
|
||||
public function onAttachmentCountDependencies(int $attachment_id, int &$dependencies): bool
|
||||
public function onAttachmentRefCount(int $attachment_id, int &$dependencies): bool
|
||||
{
|
||||
$avatars = DB::findBy('avatar', ['attachment_id' => $attachment_id]);
|
||||
$dependencies += count($avatars);
|
||||
$dependencies += DB::count('avatar', ['attachment_id' => $attachment_id]);
|
||||
return Event::next;
|
||||
}
|
||||
|
||||
|
@ -133,7 +133,7 @@ class Avatar extends Entity
|
||||
if ($cascade) {
|
||||
$attachment = $this->getAttachment();
|
||||
// We can't use $attachment->isSafeDelete() because underlying findBy doesn't respect remove persistence
|
||||
if ($attachment->countDependencies() - 1 === 0) {
|
||||
if ($attachment->refCount() - 1 === 0) {
|
||||
$attachment->delete(cascade: true, flush: false);
|
||||
}
|
||||
}
|
||||
|
@ -246,28 +246,22 @@ class Attachment extends Entity
|
||||
const URLHASH_ALGO = 'sha256';
|
||||
const FILEHASH_ALGO = 'sha256';
|
||||
|
||||
/**
|
||||
*
|
||||
* Warning: Underlying DB::findBy doesn't respect remove persistence
|
||||
* @return int
|
||||
*/
|
||||
public function countDependencies(): int
|
||||
public function refCount(): int
|
||||
{
|
||||
$attachment_id = $this->getId();
|
||||
$notes = DB::findBy('attachment_to_note', ['attachment_id' => $attachment_id]);
|
||||
$dependencies = count($notes);
|
||||
Event::handle('AttachmentCountDependencies', [$attachment_id, &$dependencies]);
|
||||
$dependencies = DB::count('attachment_to_note', ['attachment_id' => $attachment_id]);
|
||||
Event::handle('AttachmentRefCount', [$attachment_id, &$dependencies]);
|
||||
return $dependencies;
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends Attachment->refCount()
|
||||
*
|
||||
* @depends Attachment->countDependencies()
|
||||
* @return bool
|
||||
*/
|
||||
public function isSafeDelete(): bool
|
||||
{
|
||||
return $this->countDependencies() === 0;
|
||||
return $this->refCount() === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
|
Loading…
Reference in New Issue
Block a user