Reputation: 157
A fonctionnality of my project consists of synchronizing data between two databases in two ways.
I added a new Action 'synchronizeAction' in the specified Controller, I managed routes and views and It's working fine. The problem is with the cron :
I created a service and use it.
<?xml version="1.0" ?>
<container xmlns="...">
<services>
<service id="soc_ref.synchro_clients" class="SocRefBundle\Services \SynchroClient">
</service>
</services>
</container>
And I created 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');
}
}
How can I use / inject the entityManager ? $this->getDoctrine()->getManager(); is accessible only in Controllers.
I'm just beginning the symfony developement, thank you for helping me :)
Upvotes: 0
Views: 180
Reputation: 1431
If I had to do this, first I would move the business logic to a service. Create a command and the call the service function from the controller and the command. Then configure the cron to call the created command.
Upvotes: 3