<?php

namespace App\Location\Repository;

use App\Location\Entity\Province;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;

class ProvinceRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Province::class);
    }

    /** @return Province[] */
    public function findActive(): array
    {
        return $this->createQueryBuilder('p')
            ->where('p.status = 1')
            ->orderBy('p.weight', 'ASC')
            ->addOrderBy('p.name', 'ASC')
            ->getQuery()
            ->getResult();
    }

    public function save(Province $province, bool $flush = true): void
    {
        $this->getEntityManager()->persist($province);
        if ($flush) $this->getEntityManager()->flush();
    }

    public function remove(Province $province, bool $flush = true): void
    {
        $this->getEntityManager()->remove($province);
        if ($flush) $this->getEntityManager()->flush();
    }
}
