Bläddra i källkod

Refactoring

feature/ticket
Fab 3 år sedan
förälder
incheckning
3d0bc24c97
2 ändrade filer med 97 tillägg och 108 borttagningar
  1. +6
    -21
      Controller/Admin/AbstractCrudController.php
  2. +91
    -87
      Repository/AbstractRepository.php

+ 6
- 21
Controller/Admin/AbstractCrudController.php Visa fil

@@ -44,19 +44,19 @@ use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Twig\Environment;

abstract class AbstractCrudController extends EaAbstractCrudController
{
protected $session;
protected $request;
protected $em;

public function __construct(SessionInterface $session, RequestStack $request, EntityManager $em, Environment $twig)
public function __construct(SessionInterface $session, RequestStack $request, EntityManager $em)
{
$this->session = $session;
$this->request = $request;
$this->em = $em;
$this->twig = $twig;
}

public function configureActions(Actions $actions): Actions
@@ -305,7 +305,7 @@ abstract class AbstractCrudController extends EaAbstractCrudController
->getForm();

$entityManager = $this->getDoctrine()->getManagerForClass($this->getEntityFqcn());
dump($entityManager);
$repository = $entityManager->getRepository($this->getEntityFqcn());

$sortableForm->handleRequest($context->getRequest());
@@ -374,7 +374,7 @@ abstract class AbstractCrudController extends EaAbstractCrudController
$filters
);

dump(get_defined_vars());
if ($this->isInstanceOf(TreeInterface::class)) {
$entityId = $searchDto->getRequest()->get('entityId');
if ($entityId !== null) {
@@ -395,23 +395,8 @@ abstract class AbstractCrudController extends EaAbstractCrudController
FieldCollection $fields,
FilterCollection $filters
): QueryBuilder {
$queryBuilder = parent::createIndexQueryBuilder(
$searchDto,
$entityDto,
$fields,
$filters
);

if ($this->isInstanceOf(TreeInterface::class)) {
$entityId = $searchDto->getRequest()->get('entityId');
if ($entityId !== null) {
$queryBuilder->andWhere('entity.parent = :entityId');
$queryBuilder->setParameter('entityId', $searchDto->getRequest()->get('entityId'));
} else {
$queryBuilder->andWhere('entity.parent IS NULL');
}
}

$queryBuilder = $this->createIndexQueryBuilder($searchDto,$entityDto,$fields,$filters);

return $queryBuilder;
}

+ 91
- 87
Repository/AbstractRepository.php Visa fil

@@ -9,130 +9,134 @@ use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Gedmo\Translatable\TranslatableListener;
use Lc\CaracoleBundle\Doctrine\Extension\FilterMerchantInterface;
use Lc\SovBundle\Doctrine\Extension\StatusInterface;

abstract class AbstractRepository extends EntityRepository implements ServiceEntityRepositoryInterface,
AbstractRepositoryInterface
{

protected $defaultLocale;
protected $defaultLocale;

public function setDefaultLocale($locale)
{
$this->defaultLocale = $locale;
}
public function setDefaultLocale($locale)
{
$this->defaultLocale = $locale;
}

public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct($entityManager, $entityManager->getClassMetadata($this->getInterfaceClass()));
}
public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct($entityManager, $entityManager->getClassMetadata($this->getInterfaceClass()));
}

public function findOneByOldUrl($url)
{
$qb = $this->createQueryBuilder('entity')
->where('entity.oldUrls LIKE :oldUrl')
->andWhere('entity.status = 1')
->setParameter(':oldUrl', '%'.$url.'%');
public function findOneByOldUrl($url)
{
$qb = $this->createQueryBuilder('entity')
->where('entity.oldUrls LIKE :oldUrl')
->andWhere('entity.status = 1')
->setParameter(':oldUrl', '%'.$url.'%');

return $qb->getQuery()->getOneOrNullResult();
}
return $qb->getQuery()->getOneOrNullResult();
}

public function getOneOrNullResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
{
return $this->getTranslatedQuery($qb, $locale)->getOneOrNullResult($hydrationMode);
}
public function getOneOrNullResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
{
return $this->getTranslatedQuery($qb, $locale)->getOneOrNullResult($hydrationMode);
}


public function getResult(QueryBuilder $qb, $locale = null, $hydrationMode = AbstractQuery::HYDRATE_OBJECT)
{
return $this->getTranslatedQuery($qb, $locale)->getResult($hydrationMode);
}
public function getResult(QueryBuilder $qb, $locale = null, $hydrationMode = AbstractQuery::HYDRATE_OBJECT)
{
return $this->getTranslatedQuery($qb, $locale)->getResult($hydrationMode);
}


public function getArrayResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getArrayResult();
}
public function getArrayResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getArrayResult();
}


public function getSingleResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
{
return $this->getTranslatedQuery($qb, $locale)->getSingleResult($hydrationMode);
}
public function getSingleResult(QueryBuilder $qb, $locale = null, $hydrationMode = null)
{
return $this->getTranslatedQuery($qb, $locale)->getSingleResult($hydrationMode);
}


public function getScalarResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getScalarResult();
}
public function getScalarResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getScalarResult();
}


public function getSingleScalarResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getSingleScalarResult();
}
public function getSingleScalarResult(QueryBuilder $qb, $locale = null)
{
return $this->getTranslatedQuery($qb, $locale)->getSingleScalarResult();
}

protected function getTranslatedQuery(QueryBuilder $qb, $locale = null)
{
$locale = null === $locale ? $this->defaultLocale : $locale;
protected function getTranslatedQuery(QueryBuilder $qb, $locale = null)
{
$locale = null === $locale ? $this->defaultLocale : $locale;

$query = $qb->getQuery();
$query = $qb->getQuery();

$query->setHint(
Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
$query->setHint(
Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);

$query->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, $locale);
$query->setHint(TranslatableListener::HINT_TRANSLATABLE_LOCALE, $locale);

return $query;
}
return $query;
}

public function findSimilarSlug($merchant)
{
$qb = $this->createQueryBuilder('entity')
->select('entity.id, COUNT(entity.slug) as niche')
->andWhere('entity.status>=0')
->andWhere('entity.merchant= :merchant')
->setParameter('merchant', $merchant)
->groupBy('entity.slug')
->having('COUNT(entity.slug) >1');

public function findAll()
{
return $this->findBy(array());
}
return $qb->getQuery()->getResult();
}

public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$className = $this->getClassMetadata()->getName();
$entity = new $className;

if($entity instanceof StatusInterface){
if (!isset($criteria['status'])) $criteria['status'] = 1;
if ($criteria['status'] === false) unset($criteria['status']);
}
public function findAll()
{
return $this->findBy(array());
}

return parent::findBy($criteria, $orderBy, $limit, $offset);
public function findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
{
$criteria = $this->setCriteria($criteria);

}
return parent::findBy($criteria, $orderBy, $limit, $offset);
}

public function findOneBy(array $criteria, array $orderBy = null)
{
public function findOneBy(array $criteria, array $orderBy = null)
{
$criteria = $this->setCriteria($criteria);

$className = $this->getClassMetadata()->getName();
$entity = new $className;
return parent::findOneBy($criteria, $orderBy);
}

if($entity instanceof StatusInterface){
if (!isset($criteria['status'])) $criteria['status'] = 1;
if ($criteria['status'] === false) unset($criteria['status']);
}
protected function setCriteria(array $criteria) :array
{
$className = $this->getClassMetadata()->getName();
$entity = new $className;

return parent::findOneBy($criteria, $orderBy);
if ($entity instanceof StatusInterface) {
if (!isset($criteria['status'])) {
$criteria['status'] = 1;
}
if ($criteria['status'] === false) {
unset($criteria['status']);
}
}
return $criteria;
}

public function findSimilarSlug($merchant){
$qb = $this->createQueryBuilder('entity')
->select('entity.id, COUNT(entity.slug) as niche')
->andWhere('entity.status>=0')
->andWhere('entity.merchant= :merchant')
->setParameter('merchant', $merchant)

->groupBy('entity.slug')
->having('COUNT(entity.slug) >1');

return $qb->getQuery()->getResult();
}

}

Laddar…
Avbryt
Spara