1

This only happens on my production server. On my dev box the command is working as expected.

When I call my method in a controller, I can access the custom repository methods just fine. e.g.:

$em = $this->getDoctrine()->getManager();

$myData = $em->getRepository('AcmeUserBundle:User')->customMethod();

When I request the same repository and method in my command, I get:

Undefined method 'customMethod'. The method name must start with either findBy or findOneBy!

That tells me it's not seeing the repository defined in my User entity.

Here is my command code:

<?php

namespace ACME\UserBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

class myCommand extends ContainerAwareCommand {

    protected function configure()
    {
        $this->setName("user:getMyData");
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {

        $logger = $this->getContainer()->get('logger');
        $logger->info('myCommand called');  
        // I'm also not getting this written to the logfile

        $em = $this->getContainer()->get("doctrine")->getManager();

        $myData = $em->getRepository('ACMEUserBundle:User')->customMethod();

        $output->writeln($myData);            

    }
}

How is retrieving the repository inside of a command different from a controller? I know to use ->getContainer(), but otherwise?

Since the logger isn't logging, is there a problem with getContainer()?

7
  • Your dev box is windows, production is linux? Check the case: ACMEUserBundle:User vs AcmeUserBundle:User
    – Cerad
    Commented Jun 18, 2014 at 19:04
  • Thanks, Cerad. Yes, the dev box is windows and prod is linux. The case being used on both is actually ACME. (My generalizing edit was misleading there.)
    – hipnosis
    Commented Jun 18, 2014 at 19:23
  • 1
    Isn't the service for Doctrine doctrine.orm or doctrine.orm.default_entity_manager? I would try using these instead of just doctrine unless you have renamed it. Commented Jun 18, 2014 at 20:00
  • Well, consider copying in the actual code instead of something else. Difficult to debug code when you don't know what the code is. Make sure your cache is cleared. You can also get add a die(get_class($repo)); to verify what repository class you are getting back. I suspect you have a simple misspelling of the method name.
    – Cerad
    Commented Jun 18, 2014 at 22:32
  • Why don't you get repository object and call from that object.
    – Gara
    Commented Jun 19, 2014 at 3:29

1 Answer 1

0

Looks like you have an incorrect service name for Doctrine's ORM.

Change your service name

$this->get('doctrine')

To

$this->get('doctrine.orm.default_entity_manager')

You must specify the correct service name for the ORM in order to access your repository methods as defined in your configuration. Those repositories are mounted to your ORM and not Doctrine itself.

Documentation on Custom Repositories

1
  • Thanks. I ran into several Command examples using only get('doctrine'). It's still curious to me it ran fine on my local machine, but not on the production server. So, just using 'doctrine' still finds an entity manager, just not the one I needed? I would have expected a 'service not found' or 'entity manager not found' error of some sort.
    – hipnosis
    Commented Jun 19, 2014 at 17:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.