<?php


namespace AutoAdmin\Bundle\Menu\Repository;

use AutoAdmin\Bundle\Content\Repository\AbstractContentRepository;
use AutoAdmin\Bundle\Menu\Entity\Menu;
use Doctrine\ORM\Mapping\ClassMetadataInfo;

/**
 * @method Menu|null find($id, $lockMode = null, $lockVersion = null)
 * @method Menu|null findOneBy(array $criteria, array $orderBy = null)
 * @method Menu[]    findAll()
 * @method Menu[]    findBy(array $criteria, array $orderBy = null, ?int $limit = null, ?int $offset = null)
 */
class MenuRepository extends AbstractContentRepository
{
    public function contentFindOneBySlug(string $slug)
    {
        $qb = $this->createContentQueryBuilder();
        $qb->andWhere('_.slug = :slug')->setParameter('slug', $slug);
        $qb->leftJoin('_.root', '_root');
        $qb->addSelect('_root');
        $qb->leftJoin('_root.translations', '_root_tr');
        $qb->addSelect('_root_tr');
        $qb->andWhere('_root_tr.locale = :tr_locale')->setParameter('tr_locale', $this->siteContext->getLocale());
        $query = $qb->getQuery();

        $query
            ->setFetchMode(Menu::class, 'root', ClassMetadataInfo::FETCH_EAGER)
//            ->setFetchMode(MenuItem::class, 'children', ClassMetadataInfo::FETCH_EAGER)
//            ->setFetchMode(MenuItem::class, 'translations', ClassMetadataInfo::FETCH_EAGER)
        ;

//        $query
//            ->setCacheable(true)
//            ->enableResultCache()
//        ;

        return $query->getOneOrNullResult();
    }
}
