我是Symfony的新手.我创建了一个自定义命令,其唯一目的是从系统中擦除演示数据,但我不知道如何执行此操作.
在控制器中我会这样做:
$nodes = $this->getDoctrine() ->getRepository('MyFreelancerPortfolioBundle:TreeNode') ->findAll(); $em = $this->getDoctrine()->getManager(); foreach($nodes as $node) { $em->remove($node); } $em->flush();
从我得到的命令中的execute()函数执行此操作:
Call to undefined method ..... ::getDoctrine();
为了能够访问服务容器,您的命令需要扩展
Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand.
原文链接:https://www.f2er.com/php/138335.html请参阅命令文档章节 – Getting Services from the Container.
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; // ... other use statements class MyCommand extends ContainerAwareCommand { protected function execute(InputInterface $input,OutputInterface $output) { $em = $this->getContainer()->get('doctrine')->getEntityManager(); // ...