如何在TWIG文件中显示LONGBLOB字段数据

时间:2014-03-27 07:19:05

标签: symfony twig

我在Symfony2工作。我在表格中有一个使用LONGBLOB的字段。所以现在我想显示字段数据。当我显示这个LONGBLOB字段数据时,它显示我的文字就像这样 Resource id #404 但实际上我已经存储了虚拟文本。

这是我的defaultcontrolle文件代码

DefaultController.php

function video_showAction($id)
{
    $em = $this->getDoctrine()->getManager();       
    $video = $em->getRepository('MyBundle:Video')->find($id);

    return $this->render('MyBundle:Default:VideoShow.html.twig', array('qShow' => $video ));
}

VideoShow.html.twig

Description: {{ qShow.description }}
// it will display  "Resource id #404"

如何显示实际数据而不是参考。

3 个答案:

答案 0 :(得分:1)

我找到了解决方案。 我只需要使用stream_get_contents。所以我把它放到了我实体的Getter方法中。 喜欢这个

<强> MyEntity.php

// Getter Function need to be change like this
public function getDescription()
{
    if ($this->description != '')
        return stream_get_contents($this->description);
    return $this->description;
}

现在,当我显示内容时,它会显示资源ID包含的实际数据。

答案 1 :(得分:0)

要使用LONGBLOB冷杉,您需要做以下工作。这个作品在以下链接中详细描述,请检查您是否完成了所有这些工作,然后再次尝试在您的twig文件上显示您的qShow.description。我认为“资源ID#404”错误表示您在使用404 ID号读取资源时遇到问题。

http://symfony2.ylly.fr/add-new-data-type-in-doctrine-2-in-symfony-2-jordscream/

答案 2 :(得分:0)

正如您在自己的回答中提到的,您应该重写您的getter函数。

我提出类似的建议:

private $descriptionAsText = null;

public function getDescription()
{
    if (is_null($this->descriptionAsText))
    {
       $this->descriptionAsText = stream_get_contents($this->description);
    }
    return $this->descriptionAsText;
}

如果您的流可以在实体的同一实例中更改,您最终可以使用:

public function getDescription()
{
    rewind($this->description);
    return stream_get_contents($this->description);
}

我不喜欢您当前的方法,就好像您需要使用{{ qShow.description }}两次或更多时间一样,由于流偏移,您会遇到麻烦。

每次执行stream_get_contents时,您需要rewind资源,因为它会将偏移量放在流的末尾(或指定的长度)。

您可以使用以下代码重现此行为:

<?php

file_put_contents("/tmp/test.txt", "Hello, world!");

$handle = fopen("/tmp/test.txt", "r");
$contents = stream_get_contents($handle);
echo "Contents A = {$contents}\n"; // Contents A = Hello, world!

$contents = stream_get_contents($handle);
echo "Contents B = {$contents}\n"; // Contents B =

fclose($handle);

自从PHP 5.3(我猜)以来就有这种行为,所以如果你在Codepad上尝试这个代码(谁使用5.2.5),你就不会重现。

相关问题