【发布时间】:2017-10-17 05:41:52
【问题描述】:
我的项目的一个功能包括以两种方式在两个数据库之间同步数据。
- 从接口。 (用户点击按钮开始同步)
- 从 cron 脚本自动执行。
我在指定的 Controller 中添加了一个新的 Action 'synchronizeAction',我管理了路由和视图,它工作正常。 问题出在 cron 上:
- 我应该将我的 cron 脚本放在哪个位置?
- 如何使用我在该 cron 内的 Controller 中创建的方法“synchronizeAction”?
我创建了一个服务并使用它。
<?xml version="1.0" ?>
<container xmlns="...">
<services>
<service id="soc_ref.synchro_clients" class="SocRefBundle\Services \SynchroClient">
</service>
</services>
</container>
我创建了 Services/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();只能在控制器中访问。
我刚刚开始symfony开发,谢谢你的帮助:)
【问题讨论】: