Symfony-Bundle
Symfony-Bundle copied to clipboard
Cron bundle is not saving in database from controller
Hello world,
I installed this great bundle which is easy to implement. But still at the moment I have some problems, I would like to save the cron job from my service or controller. But it's not working. Here it is my code :
class InjectionQualificationController extends AbstractController
{
/**
*
* @Symfony\Component\Routing\Annotation\Route("/data_export_update_to_import_fichier", name="data_export_update_to_import_fichier")
*/
public function updateToImportFichier(Request $request, CronHelper $cronHelper){
/*
* Entity Manager for database access
*/
$_em_injectionQualification = $this->getDoctrine()->getRepository(InjectionQualification::class);
$_em = $this->getDoctrine()->getManager();
$injectionQualification = $_em_injectionQualification->findOneBy(array(
"idInjectionQualification" => $request->get('id_injection_qualification')
));
$formInjectionQualificationMappingFieldData = $this->createForm(InjectionQualification_MappingFieldDataType::class, $injectionQualification);
$formInjectionQualificationMappingFieldData->handleRequest($request);
$injectionQualification = $formInjectionQualificationMappingFieldData->getData();
if ($request->isMethod('POST') && $formInjectionQualificationMappingFieldData->isSubmitted() && $formInjectionQualificationMappingFieldData->isValid()) {
$_options = [];
$_options['command_string'] = "php app/console app:injecterQualificationImport";
$_options['command_arguments'] =
$injectionQualification->getIdInjectionQualification()
." ".$formInjectionQualificationMappingFieldData->get('projet')->getViewData()
." ".$formInjectionQualificationMappingFieldData->get('secteur')->getViewData()
." ".$formInjectionQualificationMappingFieldData->get('conseiller')->getViewData()
." ".$this->getUser()->getId()
." ".__DIR__ . '/../../../../public/uploads/injection_qualification/' . $injectionQualification->getPath()
;
$_currentDate = new \DateTime();
$_options['minute_schedule'] = $_currentDate->format('i');
$_options['repit_minute_schedule'] = NULL;
$_options['hour_schedule'] = $_currentDate->format('H');
$_options['repit_hour_schedule'] = NULL;
$_options['day_of_month_schedule'] = $_currentDate->format('d');
$_options['repit_day_of_month_schedule'] = NULL;
$_options['month_schedule'] = $_currentDate->format('n');
$_options['repit_month_schedule'] = NULL;
$_options['day_of_week_schedule'] = $_currentDate->format('w');
$_options['repit_day_of_week_schedule'] = NULL;
$_options['year_schedule'] = $_currentDate->format('Y');
$_options['repit_year_schedule'] = NULL;
$cronHelper->setCronTabSchedule($_options);
$cronHelper->add();
$objExcel = IOFactory::load(__DIR__ . '/../../../../public/uploads/injection_qualification/' . $injectionQualification->getPath());
$nbreOfRows = $objExcel->getActiveSheet()->getHighestRow();
$injectionQualification->setNumberOfRows($nbreOfRows);
$injectionQualification->setFichierImported(true);
$_em->persist($injectionQualification);
$_em->flush();
return
$this->redirect(
$this->generateUrl('data_export_homepage') . '?success_edit_injection_qualification=1&id_injection_qualification='
. $injectionQualification->getIdInjectionQualification()
);
}
/*
* Initial vars for "Monitoring Criteria"
*/
$conseillers = $_em->getRepository('ModelPersistBundle:Conseiller')->findAll();
$utilisateurs = $_em->getRepository('ModelPersistBundle:Utilisateur')->findAll();
$compagnies = $_em->getRepository('ModelPersistBundle:Compagnie')->findAll();
$secteurs = $_em->getRepository('ModelPersistBundle:Secteur')->findAll();
$projets = $_em->getRepository('ModelPersistBundle:Projet')->findAll();
$naturesQualifications = $_em->getRepository('ModelPersistBundle:NatureQualification')->findAll();
$naturesQualifications_Agenda = $_em->getRepository('ModelPersistBundle:NatureQualification')->findBy(array(
"agenda" => 1
));
$naturesComptesRendus = $_em->getRepository('ModelPersistBundle:NatureCompteRendu')->findAll();
$naturesConfirmations = $_em->getRepository('ModelPersistBundle:NatureConfirmation')->findAll();
$naturesConfortationsClients = $_em->getRepository('ModelPersistBundle:NatureConfortationClient')->findAll();
$naturesReclamationsClients = $_em->getRepository('ModelPersistBundle:NatureReclamationClient')->findAll();
$agences = $_em->getRepository('ModelPersistBundle:Agence')->findAll();
return $this->render('@DataExport/Default/index.html.twig', array(
'conseillers' => $conseillers,
'utilisateurs' => $utilisateurs,
'compagnies' => $compagnies,
'secteurs' => $secteurs,
'projets' => $projets,
'naturesQualifications' => $naturesQualifications,
'naturesQualifications_Agenda' => $naturesQualifications_Agenda,
'naturesComptesRendus' => $naturesComptesRendus,
'agences' => $agences,
'naturesConfirmations' => $naturesConfirmations,
'naturesConfortationsClients' => $naturesConfortationsClients,
'naturesReclamationsClients' => $naturesReclamationsClients,
'formInjectionQualificationMappingFieldData' => $formInjectionQualificationMappingFieldData->createView(),
'injectionQualification' => $injectionQualification,
));
}
}
And my service class :
<?php
namespace App\Functionnal\ModelPersistBundle\Service\Cron;
use Cron\Job\ShellJob;
use Cron\Schedule\CrontabSchedule;
use Cron\Cron;
use Cron\Resolver\ArrayResolver;
use Cron\Executor\Executor;
class CronHelper
{
private $job;
private $cronTabSchedule;
private $resolver;
private $executor;
private $cron;
private $options;
public function __construct()
{
$this->job = new ShellJob();
$this->resolver = new ArrayResolver();
$this->executor = new Executor();
$this->cron = new Cron();
}
public function setCronTabSchedule(?array $_options)
{
$this->options = $_options;
$minuteSchedule = $this->options['minute_schedule'];
$repitMinuteSchedule = $this->options['repit_minute_schedule'];
$hourSchedule = $this->options['hour_schedule'];
$repitHourSchedule = $this->options['repit_hour_schedule'];
$dayOfMonthSchedule = $this->options['day_of_month_schedule'];
$repitDayOfMonthSchedule = $this->options['repit_day_of_month_schedule'];
$monthSchedule = $this->options['month_schedule'];
$repitMonthSchedule = $this->options['repit_month_schedule'];
$dayOfWeekSchedule = $this->options['day_of_week_schedule'];
$repitDayOfWeekSchedule = $this->options['repit_day_of_week_schedule'];
$yearSchedule = $this->options['year_schedule'];
$repitYearSchedule = $this->options['repit_year_schedule'];
$stringSchedule = $minuteSchedule . $repitMinuteSchedule . " " . $hourSchedule . $repitHourSchedule . " " . $dayOfMonthSchedule . $repitDayOfMonthSchedule . " " . $monthSchedule . $repitMonthSchedule . " " . $dayOfWeekSchedule . $repitDayOfWeekSchedule . " " . $yearSchedule . $repitYearSchedule;
$this->cronTabSchedule = new CrontabSchedule($stringSchedule);
}
public function add(): void
{
$this->job->setCommand($this->options['command_string'] . " " . $this->options['command_arguments']);
$this->job->setSchedule($this->cronTabSchedule);
//dd( $this->options['command_string'] . " " . $this->options['command_arguments'] );
$this->cron->setExecutor($this->executor);
$this->resolver->addJob($this->job);
$this->cron->setResolver($this->resolver);
$this->cron->run();
}
}
Could you help me please. Thanks