Reputation: 506
I am junior and I am learning Symfony2, my problem is:
I have a repository class with methods and I want call this method in my command class. How can I do that?
This is the repository method:
public function findAllStatusToDo(Library $library = null)
{
$qb = $this->createQueryBuilder('l');
$qb->select('l, COUNT(l) licenses')
->andWhere("l.status = :status")
->setParameter(':status', XMLReport::STATUS_ToDo)
->addGroupBy('l.library')
->addGroupBy('l.isbn');
if ($library instanceof Library) {
$qb->andWhere("l.library = :library")->setParameter(':library', $library->getCode());
}
return $qb->getQuery()->getResult();
}
And this is the command class:
class GenerateXMLCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('reports:generateXML')
->setDescription('Generate XML');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
}
}
Thanks.
Upvotes: 2
Views: 546
Reputation: 8276
In your execute()
command you simply need to get the doctrine
service from the container and then call your repository method like this:
protected function execute(InputInterface $input, OutputInterface $output)
{
$container = $this->getContainer();
$em = $container->get('doctrine')->getManager();
$statusToDo = $em->getRepository('AppBundle:MyRepository')->findAllStatusToDo();
}
Just make sure that you call the proper repository in place of AppBundle:MyRepository
in the above code.
Upvotes: 2