小编Xav*_*avi的帖子

Symfony2 - 强制文件下载

我正在尝试在用户点击下载链接时下载文件.

在控制器中:

    $response = new Response();
    $response->headers->set('Content-type', 'application/octect-stream');
    $response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $filename));
    $response->headers->set('Content-Length', filesize($filename));

    return $response;
Run Code Online (Sandbox Code Playgroud)

这是打开保存文件的对话框,但它说文件是0字节.并将其更改为:

        $response = new Response();
        $response->headers->set('Content-type', 'application/octect-stream');
        $response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', $filename));
        $response->headers->set('Content-Length', filesize($filename));
        $response->headers->set('Content-Transfer-Encoding', 'binary');
        $response->setContent(readfile($filename));

        return $response;
Run Code Online (Sandbox Code Playgroud)

我得到了一堆奇怪的字符而不是文件下载对话框.

最后,将"setContent"行切换为:

    $response->setContent(file_get_contents($filename));
Run Code Online (Sandbox Code Playgroud)

它返回一个PHP错误:

致命错误:允许的内存大小......

关于如何实现这一点的任何线索?我之前在PHP中做过(没有MVC),但我不知道通过Symfony2做什么可能会遗漏...

也许解决方案是在PHP.INI中设置memory_limit,但我想这不是最好的做法......

php header download symfony

53
推荐指数
6
解决办法
7万
查看次数

Doctrine2 - 在刷新之前获取实体ID

有没有办法在persist/flush之前获取实体ID?我的意思是:

$entity = new PointData();
$form   = $this->createForm(new PointDataType(), $entity);
Run Code Online (Sandbox Code Playgroud)

如果我此时尝试$ entity-> getId(),它什么都不返回.

我可以通过以下方式获得它:

$em->persist($entity);
$em->flush();
Run Code Online (Sandbox Code Playgroud)

(假设$ em = $ this-> getDoctrine() - > getEntityManager();)

我怎样才能做到这一点?

entity flush persist doctrine-orm

15
推荐指数
2
解决办法
2万
查看次数

Symfony2 - FOSUserBundle - FindUserBy <field_in_custom_UserBundle>

我创建了自己的用户Bundle,扩展了FOSUserBundle.

在我的UserBundle中,我有一个memberID字段,带有setter和getter.

我已经可以使用EntityManager通过memberID找到用户,然后我可以通过UserManager找到用户匹配用该EntityManager查询获得的用户名/ email/...但是...

有没有办法使用UserManager查找UserByMemberID?

谢谢.

findby symfony doctrine-orm fosuserbundle

2
推荐指数
1
解决办法
9170
查看次数

Twig模板:在JOIN函数中添加html

我可以使用以下内容获取与帖子相关的所有标签:

{{ post.tags | join(', ') }}
Run Code Online (Sandbox Code Playgroud)

它会显示:

tag1, tag2, tag3, etc
Run Code Online (Sandbox Code Playgroud)

我怎么能让这些标签成为一个链接,而不仅仅是文本?我的意思是:

<a href="tag1.php">tag1</a>, <a href="tag2.php">tag2</a>, <a href="tag3.php">tag3</a>, etc
Run Code Online (Sandbox Code Playgroud)

我是否被迫使用foreach(),单独显示标签,并手动添加逗号?

谢谢!

arrays templates join symfony twig

2
推荐指数
1
解决办法
1300
查看次数

jQuery:not()+ nth-child

具有以下DOM结构:

<ul>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li class="click_here"></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li class="click_here"></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li></li>
    <li class="click_here"></li>
</ul>
Run Code Online (Sandbox Code Playgroud)

我试图每隔5"li"添加"last"类,但不包括那些有"click_here"类的类.

我发现这不起作用:

$('ul li:not(.click_here)').filter(':nth-child(5n)').addClass('last');
Run Code Online (Sandbox Code Playgroud)

我也尝试了几种方法,比如:

$('ul li').filter(':not(.click_here)').filter(':nth-child(5n)').addClass('last');
$('ul li').not($('.click_here')).filter(':nth-child(5n)').addClass('last')
Run Code Online (Sandbox Code Playgroud)

它似乎没有跳过具有"click_here"类的"li"项目.

所以我最后不得不这样做:

        $.each($('ul li').filter(':not(.click_here)'),function(i,v){
            if((i+1)%5 == 0) $(this).addClass('last');
        });
Run Code Online (Sandbox Code Playgroud)

你有没有人知道我在第一种方法中做错了什么?

非常感谢!

jquery filter css-selectors jquery-selectors

2
推荐指数
1
解决办法
6124
查看次数