2017-05-17 1 views
0

내 프로젝트의 중요성은 두 가지 방법으로 두 데이터베이스간에 데이터를 동기화하는 것으로 구성됩니다.Symfony 3 프로젝트의 Crons/Job

  • 인터페이스에서. (사용자가 동기화를 시작하기 위해 버튼을 클릭)
  • 자동으로 cron 스크립트에서.

새 컨트롤러 'synchronizeAction'을 (를) 지정된 컨트롤러에 추가했습니다. 라우트와 뷰를 관리했으며 정상적으로 작동합니다. 문제는 cron :

  1. 내 cron 스크립트를 넣어야하는 위치는 무엇입니까?
  2. 내가 만든 cron의 컨트롤러에서 'synchronizeAction'메서드를 사용하는 방법은 무엇입니까?

서비스를 생성하여 사용했습니다.

<?xml version="1.0" ?> 
<container xmlns="..."> 
    <services> 
     <service id="soc_ref.synchro_clients" class="SocRefBundle\Services \SynchroClient">  
     </service> 
    </services> 
</container> 

내가 서비스를 생성/SynchroClient.php

class SynchroClient 
{ 

    public function __construct() 
    { 

    } 

    public function synchronize() { 
     $ref_db = $this->getDoctrine()->getManager(); 
     $other_db = $this->getDoctrine()->getManager('other'); 

     $sql = ' SELECT active, .... '; 
     $statement = $other_db->getConnection()->prepare($sql); 
     $statement->execute(); 
     $result = $statement->fetchAll(); 

     foreach ($result as $entity) { 
      $client = new Client(); 
      $client->setActive($entity['active']); 

      $ref_db->persist($client); 
     } 
     $sql_truncate = ' TRUNCATE TABLE client'; 
     $statement = $ref_db->getConnection()->prepare($sql_truncate); 
     $statement->execute(); 
     $ref_db->flush(); 

     return new Response('1'); 
    } 

} 

은 어떻게 EntityManager를 주입/사용할 수 있습니까? $ this-> getDoctrine() -> getManager(); 컨트롤러에서만 액세스 할 수 있습니다. 난 그냥 심포니 developement에 시작 해요

, 저를 도와 주셔서 감사합니다 :) 나는이 작업을 수행해야한다면

답변

3

은, 처음에는 서비스에 비즈니스 로직을 이동합니다. 명령을 작성하고 컨트롤러 및 명령에서 서비스 기능을 호출하십시오. 그런 다음 생성 된 명령을 호출하도록 cron을 구성하십시오.

+0

답장을 보내 주셔서 감사합니다.하지만 그렇게하려고했지만 문제가있었습니다. 제 대답을 보시고 어떻게해야하는지 말해 주시겠습니까? – user199320

+0

ContainerAwareCommand를 만들어야합니다. 여기에 더 많은 정보가 있습니다 : http://symfony.com/doc/current/console.html#getting-services-from-the-service-container 일단 작성된 DI 컨테이너에서 $ this-> getContainer() -> get ('service_name') – Carlos