Entity Repository

Asked

Viewed 41 times

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 got 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 IntervencaoController call the parameters/variables.

1 answer

0


In its entity Intervencao you must declare the repository you have 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 the EntityManager Doctrine to access its repository and its respective 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 complement the answer.

  • Intervencao.php : namespace Roqsys Control Manutencaobundle Entity; Intervencaocontroller : namespace Roqsys Control Manutencaobundle Controller;

  • I don’t know if you need any more, but if you do.

  • I edited the answer. See if it works.

  • why is the text edited?

  • Because I put the namespaces you mentioned and also the repository entities in Controller.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.