Entity Repository

0

I have the tables:

Operário(id,identidade(string));
Intervenção (data(date), hora(time), duracao(integer));
Avaria (data(date), descricao(string), impedimento(boolean));
Prevista (contagem(integer), duracao(integer), fazer(string), tempo(integer), ficha(string));

And I have the repository done.

class IntervencaoRepository extends EntityRepository {
    public function getRecentIntervencao($number = 30) {
        $dql = "SELECT b, e, r FROM Bug b JOIN b.engineer e JOIN b.reporter r ORDER BY b.created DESC";
        $query = $this->getEntityManager()->createQuery($dql);
        $query->setMaxResults($number);
        return $query->getResult();
    }

    public function getRecentIntervencaoArray($number = 30) {
        $dql = "SELECT b, e, r, p FROM Bug b JOIN b.engineer e ".
               "JOIN b.reporter r JOIN b.products p ORDER BY b.created DESC";
        $query = $this->getEntityManager()->createQuery($dql);
        $query->setMaxResults($number);
        return $query->getArrayResult();
    }

    public function getUsersIntervencao($userId, $number = 15) {
        $dql = "SELECT b, e, r FROM Bug b JOIN b.engineer e JOIN b.reporter r ".
               "WHERE b.status = 'OPEN' AND e.id = ?1 OR r.id = ?1 ORDER BY b.created DESC";
        return $this->getEntityManager()->createQuery($dql) ->setParameter(1, $userId) ->setMaxResults($number) ->getResult();
    }

    public function getOpenIntervencaoByProduct() {
        $dql = "SELECT p.id, p.name, count(b.id) AS openBugs FROM Bug b ".
               "JOIN b.products p WHERE b.status = 'OPEN' GROUP BY p.id";
        return $this->getEntityManager()->createQuery($dql)->getScalarResult();
    }
}

And I wanted in% w /% to call parameters / variables.

    
asked by anonymous 10.02.2015 / 16:19

1 answer

0

In your Intervencao entity you must declare the repository you just created for it:

<?php

namespace RoqSys\Control\ManutencaoBundle\Entity;

use use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="IntervencaoRepository")
 */
class Intervencao
{
    // resto da classe
}

Then just use EntityManager of Doctrine to access your repository and its methods.

<?php

namespace RoqSys\Control\ManutencaoBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class IntervencaoController extends Controller
{
    public function qualquerMetodoAction()
    {
        $intervencao = $this->getDoctrine()->getRepository('RoqSysControlManutencaoBundle:Intervencao')->getRecentIntervencao();
        // $intervencao = $this->getDoctrine()->getRepository('RoqSysControlManutencaoBundle:Intervencao')->getRecentIntervencaoArray();
        // $intervencao = $this->getDoctrine()->getRepository('RoqSysControlManutencaoBundle:Intervencao')->getUsersIntervencao($this->getUser()->getId());
        // $intervencao = $this->getDoctrine()->getRepository('RoqSysControlManutencaoBundle:Intervencao')-> getOpenIntervencaoByProduct();

        return $intervencao;
    }
}

PS: Then put the namespaces of your classes so that I can supplement the answer.

    
10.02.2015 / 17:19