这适用于控制器,但是,当注入命令时它不起作用。
public function execute(InputInterface $input, OutputInterface $output)
{
$em = $this->getDoctrine()->getManager();
try {
$task = new Task();
$task->setSubject("Test subject");
$task->setCreatedAt(new \DateTime());
$em->persist($task);
$em->flush();
} catch (\Exception $e) {
throw $e; //no error
}
//prints successful here
}
/**
* @param string $entityName
* @return \Doctrine\Common\Persistence\ObjectRepository
*/
protected function getRepository($entityName)
{
return $this->getDoctrine()->getRepository($entityName);
}
protected function getDoctrine()
{
return $this->getContainer()->get('doctrine');
}
cron 成功地将其拾取直到结束,但它不会引发任何错误或保存实体。