File 如何将渲染内容写入文件

File 如何将渲染内容写入文件,file,symfony,File,Symfony,我想获得呈现的内容,例如,$this->render('ProBundle:Default:view.html.twig',array('data'=>$data)),并将其写入文件 我已经创建了一个文件类: <?php namespace Pro\ConvocationBundle\Entity; use Symfony\Component\HttpFoundation\File\File as SymfonyFile; use Symfony\Component\Validator

我想获得呈现的内容,例如,
$this->render('ProBundle:Default:view.html.twig',array('data'=>$data)),并将其写入文件

我已经创建了一个文件类:

<?php

namespace Pro\ConvocationBundle\Entity;

use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
use Symfony\Component\Validator\Constraints;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="file")
 */
class File
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column
     * @Constraints\NotBlank
     */
    private $name;

    /**
     * @var \DateTime
     * @ORM\Column(type="datetime")
     * @Constraints\NotNull
     * @Constraints\DateTime
     */
    private $created;

    /**
     * @var SymfonyFile
     * @Constraints\File(maxSize=1048060)
     */
    private $filesystemFile;

    /**
     * @Constraints\NotNull
     * @ORM\OneToOne(targetEntity="Pro\ConvocationBundle\Entity\Convocation")
     * @ORM\JoinColumn(nullable=false)
     */
    private $convocation;

    function __construct(SymfonyFile $file, $name)
    {
        $this->created = new \DateTime;
        $this->setFilesystemFile($file);
        $this->name = $name;
    }

    function getId()
    {
        return $this->id;
    }

    function getName()
    {
        return $this->name;
    }

    function getCreated()
    {
        return $this->created;
    }

    function setFilesystemFile(SymfonyFile $file)
    {
        $this->filesystemFile = $file;
    }

    function getFilesystemFile()
    {
        return $this->filesystemFile;
    }
}
所以我猜我可以调用服务并创建文件:

$newContent = $this->get('templating')->render('ProConvocationBundle:Default:view.html.twig', array('data' => $data));

$this->get('pro_convocation.convocation_file')->postPersist($newContent);
但我得到了一个错误:

Catchable Fatal Error: Argument 1 passed to Pro\ConvocationBundle\Service\File::postPersist() must be an instance of Doctrine\ORM\Event\LifecycleEventArgs, string given

postPersist
接受事件对象作为参数,而不是要写入的内容。所以我现在对如何创建新文件有点困惑。有什么想法吗?

发生这种情况是因为您试图将ConvocationFile类用作服务。这是一项服务,但不应该这样称呼。这就是为什么该类应该位于捆绑包中的EventListener文件夹中,如Symfony2约定所述:

\src\Pro\ConvocationBundle\EventListener\ConvocationFile.php
如果您想手动调用此服务,只要它是一个事件侦听器,就必须调度一个侦听器可以捕获的事件

因此,您应该尝试以下方法:

<?php

use Doctrine\ORM\Events;
use Doctrine\Common\EventManager;

$arguments = array('my_stuff');

$evm = new EventManager();
$evm->dispatchEvent(Events::postUpdate, $arguments);

因为当您调用
postPersist
时,您正在发送一个呈现的字符串,并且应该是
LifecycleEventArgs
的对象实例。如果您有实体对象,我建议您将对象和
模板化
服务发送到
postPersist
,然后在该函数中执行渲染和传递以保存文件我认为这不会解决您的问题,但您使用的是
$this->render()
。它将返回一个
响应
对象。如果需要字符串,请使用
$this->renderView()
。我假设您不想将响应对象写入该文件。谢谢Francesco,我来看看。
\src\Pro\ConvocationBundle\EventListener\ConvocationFile.php
<?php

use Doctrine\ORM\Events;
use Doctrine\Common\EventManager;

$arguments = array('my_stuff');

$evm = new EventManager();
$evm->dispatchEvent(Events::postUpdate, $arguments);