2012-05-11 3 views
2

현재 터미널에서 명령을 실행하여 CRON 작업을 수행하려고합니다. 그러나 다음과 같은 오류가 발생합니다.Symfony2에서 명령 행을 통해 CRON 작업을 실행할 수 없습니다.

PHP Fatal error: Call to a member function has() on a non-object in /MyProject/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php on line 161 

이것은 내 명령 파일의 코드입니다.

namespace MyProject\UtilityBundle\Command; 
use Symfony\Component\Console\Command\Command; 
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 projectOngoingCommand extends Command 
    { 
     protected function configure() 
     { 
      $this 
       ->setName('projectOngoingEstimation:submit') 
       ->setDescription('Submit Ongoing Project Estimation') 

       ; 
     } 

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

      ; 
      $projectController= new \MyProject\ProjectBundle\Controller\DefaultController(); 


      $msg = $projectController->updateMonthlyOngoingAllocation(); 


      $output->writeln($msg); 
     } 
    } 

이것은 기본 컨트롤러의 코드입니다.

// cron job code 
    public function updateMonthlyOngoingAllocation() { 

       $em = $this->getDoctrine()->getEntityManager(); 
     $project = $this->getDoctrine()->getRepository('MyProjectEntityBundle:Project') 
        ->getAllOngoingProjectList(); 
     return "hello"; 
     } 

이 방법은 명령 sudo php app/console projectOngoingEstimation:submit

를 사용하여 성공적이라고하지만 그것은 매우 첫 번째 줄에 오류가 발생합니다. 즉

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

컨트롤러 내에서 다른 동작 방법으로 함수를 호출하려고하면 제대로 작동합니다.

답변

2

여기 올바른 전략을 사용한다고 생각하지 않습니다. 명령에서 컨트롤러를 호출하려고하면 오류 메시지에 따라 좋은 생각이 아닌 것 같습니다.

서비스를 만들어 컨트롤러와 명령 내에서이 서비스를 호출해야합니다. 클래스를 확장하는 경우 (명령에서 컨트롤러에서

  • $this->get('project_manager')->updateMonthlyOngoingAllocation()
  • 과 :
    class ProjectManager 
    { 
        private $em; 
    
        public function __construct(EntityManager $em) { 
         $this->em = $em; 
        } 
    
        public function updateMonthlyOngoingAllocation() { 
         $project = $this->em->getRepository('MyProjectEntityBundle:Project') 
           ->getAllOngoingProjectList(); 
         return "hello"; 
        }  
    } 
    

    다음 config.yml 지금

    services: 
        project_manager: 
         class: MyBundle\Manager\ProjectManager 
         arguments: ["@doctrine.orm.entity_manager"] 
    

    이 서비스를 호출 할 수 있습니다

    Command 대신 ContainerAwareCommand에서) $this->getContainer()->get('project_manager')->updateMonthlyOngoingAllocation()
0

다음과 같이하십시오. 콘솔에 컨테이너를 인식 할 수 있으므로 아무 것도 주입 할 필요가 없습니다.

public function updateMonthlyOngoingAllocation() { 
        $project = $this->getContainer() 
          ->get('doctrine') 
          ->getRepository('MyProjectEntityBundle:Project') 
          ->getAllOngoingProjectList(); 
      return "hello"; 
      } 
관련 문제