【问题标题】:Symfony2 command and tcpdf whiteoctoberSymfony2 命令和 tcpdf whiteoctober
【发布时间】:2016-05-26 10:17:05
【问题描述】:

我使用 symfony,并且成功安装了 whiteoctober TCPDF。 如果我在控制器中使用它,它会起作用,我的 PDF 会生成。

但是我想在命令中使用它。 所以我在命令文件夹中有一个名为“CronInvoicesCommand”的命令,当然我有这个错误:

尝试在 C:\wamp\www\OandPlocal\src\OandP\boBundle\Command\CronInvoicesCommand.php 第 187 行中的类“OandP\boBundle\Command\CronInvoicesCommand”上调用方法“get”。您的意思是调用:“getAliases”、“getApplication”、“getDefinition”、“getDescription”、“getHelp”、“getHelper”、“getHelperSet”、“getName”、“getNativeDefinition”、“getProcessedHelp”、“getSynopsis”?

所以我的问题是如何在命令中加载所有这些方法。 非常感谢您的帮助

【问题讨论】:

    标签: symfony command tcpdf


    【解决方案1】:

    您可以使用容器感知命令:

    use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
    
    class MyCommand extends ContainerAwareCommand
    {
        protected function execute(InputInterface $input, OutputInterface $output)
        {
            $em = $this->getContainer()->get('doctrine')->getEntityManager();
            // ...
    

    【讨论】:

    • 我已经用上了...我用 getcontainer() 替换了 get() : $pdf = $this->getContainer("white_october.tcpdf")->create('vertical', PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);但我仍然有一个错误:尝试在类“appDevDebugProjectContainer 上调用方法“create”
    • 很抱歉,它适用于 getContainer()。非常感谢您的帮助。
    • 你能验证任何答案吗?
    • $this->get(******) 适用于 Symfony 2.6 及旧版本
    【解决方案2】:

    可能您正试图从类似于控制器方法 get 的容器中获取服务。在命令中,您可以使用 getContainer() 方法访问容器,因此请尝试替换以下内容:

    $this->get('service_name');
    

    $this->getContainer()->get('service_name');
    

    更多信息here in the doc

    希望有帮助

    【讨论】:

    • 我仍然有一个错误:尝试在类“appDevDebugProjectContainer”上调用方法“create”
    • 很抱歉,它适用于 getContainer()。非常感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-27
    • 2014-02-23
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 2012-07-20
    相关资源
    最近更新 更多