<?php


namespace AutoAdmin\Bundle\BrickBuilder\Repository;


use AutoAdmin\Bundle\BrickBuilder\Entity\Brick;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\Persistence\ManagerRegistry;

/**
 * @method Brick|null find($id, $lockMode = null, $lockVersion = null)
 * @method Brick|null findOneBy(array $criteria, array $orderBy = null)
 * @method Brick[]    findAll()
 * @method Brick[]    findBy(array $criteria, array $orderBy = null, ?int $limit = null, ?int $offset = null)
 */
class BrickRepository extends ServiceEntityRepository
{
    public function __construct(ManagerRegistry $registry)
    {
        parent::__construct($registry, Brick::class);
    }

    public function eagerFind(int $id)
    {
        $qb = $this->createQueryBuilder('_');
        $qb->select(['_', '_children',]);
        $qb->leftJoin('_.children', '_children');

        $qb->andWhere('_.id = :id')->setParameter('id', $id);

        $query = $qb->getQuery();

        $query->setFetchMode(Brick::class, 'children', ClassMetadata::FETCH_EAGER);

        $res = $query->execute();

        if ($res) {
            $res = $res[0];
        }

        return $res;
    }

    // /**
    //  * @return Translation[] Returns an array of Test objects
    //  */
    /*
    public function findByExampleField($value)
    {
        return $this->createQueryBuilder('t')
            ->andWhere('t.exampleField = :val')
            ->setParameter('val', $value)
            ->orderBy('t.id', 'ASC')
            ->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
    */

    /*
    public function findOneBySomeField($value): ?Test
    {
        return $this->createQueryBuilder('t')
            ->andWhere('t.exampleField = :val')
            ->setParameter('val', $value)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }
    */
}
