How to have a custom repository not associated with an entity in Symfony

0

I have a file DefaultRepository to place all the general queries there.

Would it be possible to have a custom repository not associated with an entity in Symfony ? I would like to put in it all the queries that do not fit well in other repositories (can refer to an abstract or entity hierarchy).

To use the repository in the controller what changes should I make:

$this->getDoctrine()->getRepositoty (/ * ??? * /) debe reemplazarse?
    
asked by juanitourquiza 07.11.2018 в 17:26
source

1 answer

0

This question is placed in the English version of stackoverflow: link

After several questions I could have the answer I needed, on the same generate a summary, I hope you serve: link

Use symfony 3.3.x. In this way, solve the problem

Service.yml file:

//service.yml
services:
     app.queriesmanager:
          class: AppBundle\Services\QueriesManager
          autowire: true

AppBundle \ Services \ QueriesManager.php file

//AppBundle\Services\QueriesManager.php
<?php

namespace AppBundle\Services;

use Doctrine\ORM\EntityManagerInterface;

class QueriesManager  {
    protected $entityManager;

    public function __construct(EntityManagerInterface $entityManager) {
        $this->entityManager = $entityManager;
    }

    public function searchAdvance($var,$var1,$var2,$var3)
    {
        $dql1 = "";
        $dql2 = "";
        $dql = "SELECT pl
                      FROM BackendBundle:Products p
                      JOIN BackendBundle:Pricelist pl
                      WITH p.peachitemid =  pl.peachitemid
                      WHERE p.hasimages =  1
                      AND p.active = 'FALSE'
                      AND p.imagesamazon1 = 1
                      AND p.imagesamazon2 = 1
                      AND (p.descriptionforsales like '%$var%' OR p.itemid like '%$var%')";

        if ($var1 != "select") {
            $dql1 = " AND p.newcategory='$var1' AND p.newsubcategory='$var2' ";
        }
        if ($var3 == "150") {
            $dql2 = " AND pl.retail < 150";
        }
        if ($var3 == "200") {
            $dql2 = " AND pl.retail > 150 AND pl.retail < 250 ";
        }
        if ($var3 == "250") {
            $dql2 = " AND pl.retail > 250 ";
        }

        $dql1 .= $dql2;
        $dql .= $dql1;
        $query=$this->entityManager->createQuery($dql);
        $query = $query->getResult();
        return $query;

    }
}

DefaultController.php file

//Use the service in controller
$query=$this->get('app.queriesmanager')->searchAdvance($var,$var1,$var2,$var3);

I hope I can help you.

    
answered by 07.11.2018 / 17:26
source