2017-03-28 10 views
0

コンソールアプリケーションを準備し、エンティティマネージャのコンテナを使用してデータベースに接続しています。これは正常に動作します。コードは、私は意志が順番にORMの接続を行い、データベースからいくつかのデータを取得thsi $emailReminder->sendMailToLabManager($tDtlsDiscEntity);を呼び出すとき、問題は、それがエラーを与えているが、今コマンドラインまたはコンソールアプリケーションでコントローラ機能にアクセスできない

protected function execute(InputInterface $input, OutputInterface $output) 
{ 
    // get Doctrine 
    $this->em = $this->getContainer()->get('doctrine.orm.entity_manager'); 
    // find all disc that are not treated by Manager in last week i.e sysdate - 6 
    $pendingApprovals = $this->em->getRepository('GMRestBundle:TDtlsDisc')->getPendingManagerAppoval(); 
    // for each unapproved disc, start sending emails 

    $repository = $this->em->getRepository('GMRestBundle:TDtlsDisc'); 
    $emailReminder = new SubmitDisclosureController();  
    foreach($pendingApprovals as $labManagerReview) {   
     $tDtlsDiscEntity = $repository->findByDiscId($labManagerReview['DISC_ID']); 
     $emailReminder->sendMailToLabManager($tDtlsDiscEntity); 
    } 
} 

を下回っています。このsendMailToLabManagerSubmitDisclosureControllerにあり、コードは以下のとおりです。

public function sendMailToLabManager($tDtlsDiscEntity) 
{ 
    $repository = $this->getDoctrine()->getRepository('GMRestBundle:TXrefDiscSso'); 
    $entityRepository = $this->getDoctrine()->getRepository('GMRestBundle:TDtlsEntity'); 
    $discId = $tDtlsDiscEntity->getDiscId(); 
....... 

コンソールアプリケーションでは、doctrineオブジェクトに別の接続を行うことでDBにアクセスするコントローラを使用できません。同じコントローラーをWebで別のアクションコントローラー経由で呼び出すと、うまく動作します。

Error: Call to a member function has() on null 

このエラーメッセージが表示されます。

+0

$ emailReminder-> setContainer($ this-> getContainer())は、エラーメッセージを先に取得しますが、コントローラメソッドを直接呼び出してはいけません。 EmailReminderをスタンドアロンのサービスにしてから、コマンドとコントローラの両方と共有してください。http://symfony.com/doc/current/service_container.htmlサービスを理解することは、Symfonyフレームワークを効果的に使用するための重要な部分です。 – Cerad

答えて

0

コントローラを使用する代わりに、機能を提供するサービスを作成する必要があります。その後、サービスに依存性注入を介してEntityManagerを挿入する必要があります。

コマンドextends ContainerAwareCommandの場合は、コマンド内で$this->get()を使用して、前に定義したサービスを取得できます。

関連する問題