83 lines
2.7 KiB
PHP
83 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Repository;
|
|
|
|
use App\Entity\UsersOrganizations;
|
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
/**
|
|
* @extends ServiceEntityRepository<UsersOrganizations>
|
|
*/
|
|
class UsersOrganizationsRepository extends ServiceEntityRepository
|
|
{
|
|
public function __construct(ManagerRegistry $registry)
|
|
{
|
|
parent::__construct($registry, UsersOrganizations::class);
|
|
}
|
|
|
|
/**
|
|
* Returns active user-organization mappings with joined User and Organization.
|
|
* Only active and non-deleted users and organizations are included.
|
|
*
|
|
* @return UsersOrganizations[]
|
|
*/
|
|
public function findUsersWithOrganization(array $organizationIds = null): array
|
|
{
|
|
$qb = $this->createQueryBuilder('uo')
|
|
->addSelect('u', 'o')
|
|
->leftJoin('uo.users', 'u')
|
|
->leftJoin('uo.organization', 'o')
|
|
->andWhere('u.isActive = :uActive')
|
|
->andWhere('u.isDeleted = :uDeleted')
|
|
->andWhere('o.isActive = :oActive')
|
|
->andWhere('o.isDeleted = :oDeleted')
|
|
->orderBy('o.name', 'ASC')
|
|
->addOrderBy('u.surname', 'ASC')
|
|
->setParameter('uActive', true)
|
|
->setParameter('uDeleted', false)
|
|
->setParameter('oActive', true)
|
|
->setParameter('oDeleted', false);
|
|
if (!empty($organizationIds)) {
|
|
$qb->andWhere('o.id IN (:orgIds)')
|
|
->setParameter('orgIds', $organizationIds);
|
|
}
|
|
|
|
return $qb->getQuery()->getResult();
|
|
}
|
|
|
|
/**
|
|
* Same as above, filtered by a list of organization IDs.
|
|
*
|
|
* @param int[] $organizationIds
|
|
* @return UsersOrganizations[]
|
|
*/
|
|
public function findActiveWithUserAndOrganizationByOrganizationIds(array $organizationIds): array
|
|
{
|
|
if (empty($organizationIds)) {
|
|
return [];
|
|
}
|
|
|
|
$qb = $this->createQueryBuilder('uo')
|
|
->addSelect('u', 'o')
|
|
->leftJoin('uo.users', 'u')
|
|
->leftJoin('uo.organization', 'o')
|
|
->where('uo.isActive = :uoActive')
|
|
->andWhere('u.isActive = :uActive')
|
|
->andWhere('u.isDeleted = :uDeleted')
|
|
->andWhere('o.isActive = :oActive')
|
|
->andWhere('o.isDeleted = :oDeleted')
|
|
->andWhere('o.id IN (:orgIds)')
|
|
->orderBy('o.name', 'ASC')
|
|
->addOrderBy('u.surname', 'ASC')
|
|
->setParameter('uoActive', true)
|
|
->setParameter('uActive', true)
|
|
->setParameter('uDeleted', false)
|
|
->setParameter('oActive', true)
|
|
->setParameter('oDeleted', false)
|
|
->setParameter('orgIds', $organizationIds);
|
|
|
|
return $qb->getQuery()->getResult();
|
|
}
|
|
}
|