. * * The interactive user interfaces in modified source and object code versions * of this program must display Appropriate Legal Notices, as required under * Section 5 of the GNU Affero General Public License version 3. * * In accordance with Section 7(b) of the GNU Affero General Public License version 3, * these Appropriate Legal Notices must retain the display of the "EspoCRM" word. ************************************************************************/ namespace Espo\Core\Select\Helpers; use Espo\ORM\Defs; use Espo\ORM\Query\Part\Condition; use Espo\ORM\Query\Part\Expression; use Espo\ORM\Query\Part\WhereItem; use Espo\ORM\Query\SelectBuilder as QueryBuilder; /** * @since 8.5.0 */ class RelationQueryHelper { public function __construct( private Defs $defs, ) {} /** * @param string|string[] $id */ public function prepareMiddleWhere(string $entityType, string $link, string|array $id): WhereItem { $relationDefs = $this->defs ->getEntity($entityType) ->getRelation($link); $middleEntityType = ucfirst($relationDefs->getRelationshipName()); $key1 = $relationDefs->getMidKey(); $key2 = $relationDefs->getForeignMidKey(); $subQuery = QueryBuilder::create() ->select('id') ->from($entityType) ->leftJoin($middleEntityType, 'm', [ "m.$key1:" => 'id', 'm.deleted' => false, ]) ->where(["m.$key2" => $id]) ->build(); return Condition::in( Expression::column('id'), $subQuery ); } public function prepareAssignedUsersWhere(string $entityType, string $userId): WhereItem { return $this->prepareMiddleWhere($entityType, 'assignedUsers', $userId); } }