Merge pull request #7805 from JakubOnderka/event-tag-attach

chg: [internal] Faster event tag attaching
pull/7809/head
Jakub Onderka 2021-10-06 07:49:55 +02:00 committed by GitHub
commit 9260da8d0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 51 additions and 59 deletions

View File

@ -3226,6 +3226,21 @@ class AppModel extends Model
);
}
/**
* Faster version of default `hasAny` method
* @param array|null $conditions
* @return bool
*/
public function hasAny($conditions = null)
{
return (bool)$this->find('first', array(
'fields' => [$this->alias . '.' . $this->primaryKey],
'conditions' => $conditions,
'recursive' => -1,
'callbacks' => false,
));
}
/**
* @return AttachmentTool
*/

View File

@ -138,15 +138,11 @@ class AttributeTag extends AppModel
*/
public function attachTagToAttribute($attribute_id, $event_id, $tag_id, $local = false)
{
$existingAssociation = $this->find('first', array(
'recursive' => -1,
'fields' => ['id'],
'conditions' => array(
'tag_id' => $tag_id,
'attribute_id' => $attribute_id
)
));
if (empty($existingAssociation)) {
$existingAssociation = $this->hasAny([
'tag_id' => $tag_id,
'attribute_id' => $attribute_id,
]);
if (!$existingAssociation) {
$data = [
'attribute_id' => $attribute_id,
'event_id' => $event_id,

View File

@ -6544,18 +6544,14 @@ class Event extends AppModel
if (isset($recovered_uuids[$reference['referenced_uuid']])) {
$reference['referenced_uuid'] = $recovered_uuids[$reference['referenced_uuid']];
}
$current_reference = $this->Object->ObjectReference->find('first', array(
'conditions' => [
'ObjectReference.object_id' => $object_id,
'ObjectReference.referenced_uuid' => $reference['referenced_uuid'],
'ObjectReference.relationship_type' => $reference['relationship_type'],
'ObjectReference.event_id' => $id,
'ObjectReference.deleted' => 0,
],
'recursive' => -1,
'fields' => ['ObjectReference.id'],
));
if (!empty($current_reference)) {
$current_reference = $this->Object->ObjectReference->hasAny([
'ObjectReference.object_id' => $object_id,
'ObjectReference.referenced_uuid' => $reference['referenced_uuid'],
'ObjectReference.relationship_type' => $reference['relationship_type'],
'ObjectReference.event_id' => $id,
'ObjectReference.deleted' => 0,
]);
if ($current_reference) {
continue; // Reference already exists, skip.
}
list($referenced_id, $referenced_uuid, $referenced_type) = $this->Object->ObjectReference->getReferencedInfo(

View File

@ -77,10 +77,6 @@ class EventBlocklist extends AppModel
*/
public function isBlocked($eventUuid)
{
$result = $this->find('first', [
'conditions' => ['event_uuid' => $eventUuid],
'fields' => ['id']
]);
return !empty($result);
return $this->hasAny(['event_uuid' => $eventUuid]);
}
}

View File

@ -29,24 +29,6 @@ class EventDelegation extends AppModel
'SharingGroup'
);
public function attachTagToEvent($event_id, $tag_id)
{
$existingAssociation = $this->find('first', array(
'recursive' => -1,
'conditions' => array(
'tag_id' => $tag_id,
'event_id' => $event_id
)
));
if (empty($existingAssociation)) {
$this->create();
if (!$this->save(array('event_id' => $event_id, 'tag_id' => $tag_id))) {
return false;
}
}
return true;
}
public function transferEvent($delegation, $user)
{
$event = $this->Event->fetchEvent($user, array('eventid' => $delegation['EventDelegation']['event_id']));

View File

@ -92,16 +92,20 @@ class EventTag extends AppModel
return $result;
}
public function attachTagToEvent($event_id, $tag, &$nothingToChange = false)
/**
* @param int $event_id
* @param int $tagId
* @param bool $nothingToChange
* @return bool
* @throws Exception
*/
public function attachTagToEvent($event_id, array $tag, &$nothingToChange = false)
{
$existingAssociation = $this->find('first', array(
'recursive' => -1,
'conditions' => array(
'tag_id' => $tag['id'],
'event_id' => $event_id
)
));
if (empty($existingAssociation)) {
$existingAssociation = $this->hasAny([
'tag_id' => $tag['id'],
'event_id' => $event_id,
]);
if (!$existingAssociation) {
$this->create();
if (!$this->save(array('event_id' => $event_id, 'tag_id' => $tag['id'], 'local' => !empty($tag['local'])))) {
return false;
@ -112,17 +116,24 @@ class EventTag extends AppModel
return true;
}
/**
* @param int $event_id
* @param int $tag_id
* @param bool $nothingToChange
* @return bool
*/
public function detachTagFromEvent($event_id, $tag_id, &$nothingToChange = false)
{
$existingAssociation = $this->find('first', array(
'recursive' => -1,
'fields' => ['id'],
'conditions' => array(
'tag_id' => $tag_id,
'event_id' => $event_id
)
));
if (!empty($existingAssociation)) {
if ($existingAssociation) {
$result = $this->delete($existingAssociation['EventTag']['id']);
if ($result) {
return true;

View File

@ -217,11 +217,7 @@ class Organisation extends AppModel
// We want to create a new organisation for pushed data, even if the same org name exists
// Alter the name if the name is already taken by a random string
if (isset($uuid)) {
$existingOrgByName = $this->find('first', array(
'recursive' => -1,
'conditions' => array('name' => $name),
'fields' => ['id'],
));
$existingOrgByName = $this->hasAny(['name' => $name]);
if ($existingOrgByName) {
$organisation['name'] = $organisation['name'] . '_' . mt_rand(0, 9999);
}