@@ -447,12 +447,6 @@ public function getAllBySearchCriteria(array $searchCriteria, string $sort = 'st
447447 $ join ->on ('zp_tickets.projectId ' , '= ' , 'rup.projectId ' )
448448 ->where ('rup.userId ' , '= ' , $ userId );
449449 })
450- ->leftJoin ('zp_entity_relationship as er ' , function ($ join ) {
451- $ join ->on ('er.entityAType ' , '= ' , $ this ->connection ->raw ("'Ticket' " ))
452- ->on ('er.entityBType ' , '= ' , $ this ->connection ->raw ("'User' " ))
453- ->on ('er.entityA ' , '= ' , 'zp_tickets.id ' )
454- ->on ('er.relationship ' , '= ' , $ this ->connection ->raw ("' " .EntityRelationshipEnum::Collaborator->value ."' " ));
455- })
456450 ->where (function ($ q ) use ($ clientId ) {
457451 $ q ->whereNotNull ('rup.projectId ' )
458452 ->orWhere ('zp_projects.psettings ' , 'all ' )
@@ -489,7 +483,15 @@ public function getAllBySearchCriteria(array $searchCriteria, string $sort = 'st
489483 $ userIds = explode (', ' , $ searchCriteria ['users ' ]);
490484 $ query ->where (function ($ q ) use ($ userIds ) {
491485 $ q ->whereIn ('zp_tickets.editorId ' , $ userIds )
492- ->orWhereIn ('er.entityB ' , $ userIds );
486+ ->orWhereExists (function ($ subquery ) use ($ userIds ) {
487+ $ subquery ->selectRaw ('1 ' )
488+ ->from ('zp_entity_relationship ' )
489+ ->whereColumn ('zp_entity_relationship.entityA ' , 'zp_tickets.id ' )
490+ ->where ('zp_entity_relationship.entityAType ' , 'Ticket ' )
491+ ->where ('zp_entity_relationship.entityBType ' , 'User ' )
492+ ->where ('zp_entity_relationship.relationship ' , EntityRelationshipEnum::Collaborator->value )
493+ ->whereIn ('zp_entity_relationship.entityB ' , $ userIds );
494+ });
493495 });
494496 }
495497
@@ -672,12 +674,6 @@ public function simpleTicketQuery(?int $userId, ?int $projectId, array $types =
672674 $ join ->on ('requestor.id ' , '= ' , $ this ->connection ->raw ((int ) $ requestorId ));
673675 })
674676 ->leftJoin ('zp_tickets as milestones ' , 'zp_tickets.milestoneid ' , '= ' , 'milestones.id ' )
675- ->leftJoin ('zp_entity_relationship as er ' , function ($ join ) {
676- $ join ->on ('er.entityAType ' , '= ' , $ this ->connection ->raw ("'Ticket' " ))
677- ->on ('er.entityBType ' , '= ' , $ this ->connection ->raw ("'User' " ))
678- ->on ('er.relationship ' , '= ' , $ this ->connection ->raw ("'Collaborator' " ))
679- ->on ('er.entityA ' , '= ' , 'zp_tickets.id ' );
680- })
681677 ->where (function ($ q ) use ($ requestorId , $ clientId ) {
682678 $ q ->whereIn ('zp_tickets.projectId ' , function ($ subquery ) use ($ requestorId ) {
683679 $ subquery ->select ('projectId ' )
@@ -699,7 +695,15 @@ public function simpleTicketQuery(?int $userId, ?int $projectId, array $types =
699695 if (isset ($ userId ) && $ userId > 0 ) {
700696 $ query ->where (function ($ q ) use ($ userId ) {
701697 $ q ->where ('zp_tickets.editorId ' , (string ) $ userId )
702- ->orWhere ('er.entityB ' , $ userId );
698+ ->orWhereExists (function ($ subquery ) use ($ userId ) {
699+ $ subquery ->selectRaw ('1 ' )
700+ ->from ('zp_entity_relationship ' )
701+ ->whereColumn ('zp_entity_relationship.entityA ' , 'zp_tickets.id ' )
702+ ->where ('zp_entity_relationship.entityAType ' , 'Ticket ' )
703+ ->where ('zp_entity_relationship.entityBType ' , 'User ' )
704+ ->where ('zp_entity_relationship.relationship ' , EntityRelationshipEnum::Collaborator->value )
705+ ->where ('zp_entity_relationship.entityB ' , $ userId );
706+ });
703707 });
704708 }
705709
@@ -764,7 +768,18 @@ public function getScheduledTasks(CarbonImmutable $dateFrom, CarbonImmutable $da
764768 ->where ('zp_tickets.type ' , '<> ' , 'milestone ' );
765769
766770 if (isset ($ userId )) {
767- $ query ->where ('zp_tickets.editorId ' , (string ) $ userId );
771+ $ query ->where (function ($ q ) use ($ userId ) {
772+ $ q ->where ('zp_tickets.editorId ' , (string ) $ userId )
773+ ->orWhereExists (function ($ subquery ) use ($ userId ) {
774+ $ subquery ->selectRaw ('1 ' )
775+ ->from ('zp_entity_relationship ' )
776+ ->whereColumn ('zp_entity_relationship.entityA ' , 'zp_tickets.id ' )
777+ ->where ('zp_entity_relationship.entityAType ' , 'Ticket ' )
778+ ->where ('zp_entity_relationship.entityBType ' , 'User ' )
779+ ->where ('zp_entity_relationship.relationship ' , EntityRelationshipEnum::Collaborator->value )
780+ ->where ('zp_entity_relationship.entityB ' , $ userId );
781+ });
782+ });
768783 }
769784
770785 $ query ->where (function ($ q ) use ($ dateFrom , $ dateTo ) {
@@ -1129,7 +1144,18 @@ public function getAllMilestones(array $searchCriteria, string $sort = 'standard
11291144
11301145 if (isset ($ searchCriteria ['users ' ]) && $ searchCriteria ['users ' ] != '' ) {
11311146 $ userIds = explode (', ' , $ searchCriteria ['users ' ]);
1132- $ query ->whereIn ('zp_tickets.editorId ' , $ userIds );
1147+ $ query ->where (function ($ q ) use ($ userIds ) {
1148+ $ q ->whereIn ('zp_tickets.editorId ' , $ userIds )
1149+ ->orWhereExists (function ($ subquery ) use ($ userIds ) {
1150+ $ subquery ->selectRaw ('1 ' )
1151+ ->from ('zp_entity_relationship ' )
1152+ ->whereColumn ('zp_entity_relationship.entityA ' , 'zp_tickets.id ' )
1153+ ->where ('zp_entity_relationship.entityAType ' , 'Ticket ' )
1154+ ->where ('zp_entity_relationship.entityBType ' , 'User ' )
1155+ ->where ('zp_entity_relationship.relationship ' , EntityRelationshipEnum::Collaborator->value )
1156+ ->whereIn ('zp_entity_relationship.entityB ' , $ userIds );
1157+ });
1158+ });
11331159 }
11341160
11351161 if (isset ($ searchCriteria ['milestone ' ]) && $ searchCriteria ['milestone ' ] != '' ) {
@@ -1800,6 +1826,42 @@ public function getCollaborators(int $ticketId): array
18001826 ->toArray ();
18011827 }
18021828
1829+ /**
1830+ * Retrieves collaborators for multiple tickets in a single query.
1831+ *
1832+ * @param array<int, int|string> $ticketIds
1833+ * @return array<int, array<int, int>>
1834+ */
1835+ public function getCollaboratorsByTicketIds (array $ ticketIds ): array
1836+ {
1837+ $ ticketIds = array_values (array_filter (array_map ('intval ' , $ ticketIds )));
1838+
1839+ if (empty ($ ticketIds )) {
1840+ return [];
1841+ }
1842+
1843+ $ rows = $ this ->connection ->table ('zp_entity_relationship ' )
1844+ ->select (['entityA ' , 'entityB ' ])
1845+ ->whereIn ('entityA ' , $ ticketIds )
1846+ ->where ('entityAType ' , 'Ticket ' )
1847+ ->where ('entityBType ' , 'User ' )
1848+ ->where ('relationship ' , EntityRelationshipEnum::Collaborator->value )
1849+ ->orderBy ('entityA ' )
1850+ ->orderBy ('entityB ' )
1851+ ->get ();
1852+
1853+ $ collaboratorsByTicket = [];
1854+
1855+ foreach ($ rows as $ row ) {
1856+ $ ticketId = (int ) $ row ->entityA ;
1857+ $ userId = (int ) $ row ->entityB ;
1858+ $ collaboratorsByTicket [$ ticketId ] ??= [];
1859+ $ collaboratorsByTicket [$ ticketId ][] = $ userId ;
1860+ }
1861+
1862+ return $ collaboratorsByTicket ;
1863+ }
1864+
18031865 /**
18041866 * Removes all collaborators from a ticket.
18051867 *
0 commit comments