奇怪的Symfony2学说结果行为

时间:2013-09-23 13:54:43

标签: symfony doctrine-orm

好的我的问题是,我已经建立了一个服务构建块:

<?php

namespace Kpr\CentarZdravljaBundle\Extensions;


class Buildingblocks
{
    protected $doctrine;

public function __construct($doctrine)
{
    $this->doctrine = $doctrine;
}

public function articleHmostread($limit)
{
    $em = $this->doctrine->getManager ();
    $query = $em->createQuery(
        'SELECT a2.title, a2.id, a2.body, a2.path, c.id as cid, c.name FROM KprCentarZdravljaBundle:Category c JOIN c.articles a2 WHERE c.parent !=3 GROUP BY a2.id ORDER BY a2.readCounter DESC'
    );

    $query->setMaxResults($limit);
    $result = $query->getResult();

    return $result;
}

public function testing()
{
    $em = $this->doctrine->getManager ();
    $query = $em->createQuery(
        'SELECT a2 FROM KprCentarZdravljaBundle:Category c JOIN KprCentarZdravljaBundle:ARTICLE a2 WHERE c.id NOT IN (256) ORDER BY a2.readCounter DESC'
    );
    $result = $query->getResult();

    return $result;
}

public function articleHnewest($limit)
{
    $em = $this->doctrine->getManager ();
    $query = $em->createQuery(
        'SELECT a2.id, a2.title, a2.path, c.id as cid, c.name FROM KprCentarZdravljaBundle:Category c JOIN c.articles a2 WHERE c.parent !=3 GROUP BY a2.id ORDER BY a2.readCounter DESC'
    );
    $query->setMaxResults($limit);
    $result = $query->getResult();
    var_dump($result);
    return $result;
}

public function articlesFromCat($id, $limit)
{
    $em = $this->doctrine->getManager ();
    $query = $em->createQuery(
        'SELECT a2.title, a2.id, c.name FROM KprCentarZdravljaBundle:Category c JOIN c.articles a2  WHERE c.id = ?1 GROUP BY a2.id ORDER BY a2.published DESC'
    )->setParameter(1, $id);
    $query->setMaxResults($limit);
    $result = $query->getResult();

    return $result;
}

public function articleHfeatured($limit)
{
    $em = $this->doctrine->getManager ();
    $query = $em->createQuery(
        'SELECT a, cat FROM KprCentarZdravljaBundle:Article a JOIN a.categories cat WHERE a.featured = ?1 GROUP BY cat.id ORDER BY a.published DESC'
    )->setParameter(1, 1);
    $query->setMaxResults($limit);
    $result = $query->getResult();

    return $result;
}

public function poiHomepage($limit)
{
    $em = $this->doctrine->getManager ();
    $query = $em->createQuery(
        'SELECT p FROM KprCentarZdravljaBundle:Poi p ORDER BY p.published DESC'
    );
    $query->setMaxResults($limit);
    $result = $query->getResult();

    return $result;
}
}

然后我注册了一个全局树枝全局变量:

<?php

namespace Kpr\CentarZdravljaBundle\Twig;
use Kpr\CentarZdravljaBundle\Extensions\Buildingblocks;
use Kpr\CentarZdravljaBundle\Helper\Helper;

class MyRenderExtension extends \Twig_Extension
{
protected $bblocks;

function __construct(Buildingblocks $bblocks) {
    $this->bblocks = $bblocks;
}

public function getGlobals() {
    return array(
        'bblocks' => $this->bblocks,
        'globalimagepath' => 'uploads/documents/'
    );
}

public function getFilters()
{
    return array(
        'pathauto' => new \Twig_Filter_Method($this, 'pathautoFilter'),
        'groupname' => new \Twig_Filter_Method($this, 'groupnameFilter'),
        'teaser' => new \Twig_Filter_Method($this, 'teaserFilter'),
         'htmldecode' => new \Twig_Filter_Method($this, 'htmldecodeFilter', array(
            'is_safe' => array('html')
    )));
}
public function pathautoFilter($title)
{
    $helper = new Helper();

    return $helper->pathauto_cleanstring($title);
}
public function groupnameFilter($title)
{
    $helper = new Helper();

    return $helper->groupname($title);
}
public function htmldecodeFilter($string)
{
    return html_entity_decode($string);
}
public function teaserFilter($text, $len = 120)
{
    if(empty($text)) {
       return "";
    }
    if(strlen($text)<$len) {
        return $text;
    }       
    return preg_match("/(.{1,$len})\s./ms", $text, $match)
       ? $match[1] ."..."
       : substr($text, 0, $len)."...";
}
public function getName()
{
    return 'myrender_extension';
}
}

?>

所以现在我可以使用任何模板中的bblocks twig变量,这是我的索引模板:

{% extends 'KprCentarZdravljaBundle:Main:framework.html.twig' %}
{% block content %}
<!-- Banner 970x250px
    <div class="banner billboard"><a href="http://www.centarzdravlja.net"><img src="img/billboard.jpg" width="970" height="250" alt=""></a></div> -->

    <!-- Breadcrumbs -->
    <section id="breadcrumbs">
        <ul>
            <li><a href="#">Naslovna</a></li>
            <li><a href="#">Hrana i zdravlje</a></li>
            <li><a href="#">Recepti</a></li>
            <li>Utječe li hrana na pojavu neplodnosti, svečešću pojavu današnjice?</li>
        </ul>
    </section>

    <!-- Main -->
    <main id="main" role="main">

        <!-- Left Column -->
        <section id="main-left">

            <!-- Slides -->
            {% include "KprCentarZdravljaBundle:Parts:slider.html.twig" with { articles: bblocks.articleHmostread(5) } %}

            <!-- Articles -->
            <section id="articles">
                <ul>
                    {% include "KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig" with { articles: bblocks.articleHnewest(4), flag: 0 } only %}
                </ul>
            </section>

        </section>

        <!-- Center Column -->
        <section id="main-right">

            <!-- Interests -->
            <section id="interests">
                <h3><a href="#">Zanimljivosti</a></h3>
                <ul>
                    {% include "KprCentarZdravljaBundle:Parts:poiDefaultBlock.html.twig" with { pois: bblocks.poiHomepage(3) } %}
                </ul>
            </section>

        </section>

        <!-- Right Column -->
        <aside class="clear">

        <!-- Tabs -->
        <ul id="tabs">
            <li><a href="#tab1">Preporučamo</a></li>
            <li><a href="#tab2">Najčitanije</a></li>
            <li><a href="#tab3">Najnovije</a></li>
        </ul>
        <section class="tab-container" id="tab1">
            <ul>
                {% include "KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig" with { articles: bblocks.articleHfeatured(4), flag: 2 } %}
            </ul>
        </section>

        <section class="tab-container" id="tab3">
            <ul>
                {% include "KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig" with { articles: bblocks.articleHnewest(4), flag: 2 } %}
            </ul>
        </section>

        <!-- Banner 300x250px -->
        <div class="banner rectangle"><a href="http://www.centarzdravlja.net"><img src="{{ asset('bundles/kprcentarzdravlja/img/rectangle.jpg') }}" width="300" height="250" alt=""></a></div>

        <section id="recommendations">
            <h3>Vaše preporuke</h3>
                <ul>


                </ul>
        </section>

        </aside>

        <div class="clear"></div>

        <!-- Banner 230x90px  -->
        <div class="banner foursquare left"><a href="http://www.centarzdravlja.net"><img src="{{ asset('bundles/kprcentarzdravlja/img/bar-2.jpg') }}" width="230" height="90" alt=""></a></div>

        <!-- Banner 728x90px -->
        <div class="banner leaderboard right"><a href="http://www.centarzdravlja.net"><img src="{{ asset('bundles/kprcentarzdravlja/img/leaderboard-2.jpg') }}" width="728" height="90" alt=""></a></div>

        <div class="clear"></div>

        <!-- Featured -->
        <section id="featured">
            <ul>
                {% include "KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig" with { articles: bblocks.articleHfeatured(4), flag: 1 } %}
            </ul>
        </section>

        <div class="clear"></div>

        <!-- Video -->
        <section id="video" class="section">
            <div class="header blue clear">
                <h3>Video</h3>
                <ul class="blue">
                    <li><a href="#">Najnoviji video sadržaji</a></li>
                    <li><a href="#">Pilates</a></li>
                    <li><a href="#">Fitness</a></li>
                    <li><a href="#">Ostalo</a></li>
                </ul>
                <span class="corner blue"></span>
            </div>
            <section class="content"></section>
        </section>

        <!-- Healthy Living -->
        <section id="healthy-living" class="section">
            <div class="header orange clear">
                <h3>Zdrav život</h3>
                <ul class="orange">
                    <li><a href="#">Prikaži sve rubrike</a></li>
                </ul>
                <span class="corner orange"></span>
            </div>
            <section class="content"></section>
        </section>

        <!-- Food & Health -->
        <section id="food-health" class="section">
            <div class="header green clear">
                <h3>Hrana i zdravlje</h3>
                <ul class="green">
                    <li><a href="#">Zdrava prehrana</a></li>
                    <li><a href="#">Zdravi recepti</a></li>
                    <li><a href="#">Vodič za dijete</a></li>
                </ul>
                <span class="corner green"></span>
            </div>
            <section class="content"></section>
        </section>

        <!-- Trudnica.hr -->
        <section id="pregnancy" class="section">
            <div class="header purple clear">
                <h3>Trudnica.hr</h3>
                <ul class="purple">
                    <li><a href="#">Prije trudnoće</a></li>
                    <li><a href="#">Trudnoća</a></li>
                    <li><a href="#">Porod</a></li>
                    <li><a href="#">Moja beba</a></li>
                    <li><a href="#">Za mame</a></li>
                </ul>
                <span class="corner purple"></span>
            </div>
            <section class="content"></section>
        </section>

        <section id="strip" class="clear">
            <ul>
                <li><a href="http://www.trudnica.hr">Trudnica.hr</a></li>
                <li><a href="http://www.dijabetes.hr">Dijabetes.hr</a></li>
                <li><a href="http://www.centarzdravlja.rs">CentarZdravlja.rs</a></li>
                <li><a href="http://www.vitafest.hr">Vitafest.hr</a></li>
                <li><a href="http://www.vitashop.hr">Vitashop.hr</a></li>
            </ul>
            <a href="#" class="top">vrh stranice</a>
        </section>

    </main>
 {% endblock %}

但是有一个问题,当我从树枝上调用函数articleHnewest

{% include "KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig" with { articles: bblocks.articleHnewest(4), flag: 0 } only %}

我无法访问密钥&#34; c.name&#34;和&#34; c.id作为cid&#34;你可以看到我进入了BuildingsBlock类的dql,因为不知何故我的$ result变量在twig中被解析时被转换为对象列表而不是数组。

我在twig中传递结果之前尝试了var_dump,我得到了一个数组,但是当我在twig中调试时,由于压倒性的数据,我得到了整个变量列表和浏览器中断。

这是在BuldingBlocks-&gt; articleHnewest(4)类中的var_dump,它是一个数组,你可以看到:

    array(4) {
      [0]=&gt;
      array(5) {
        ["id"]=&gt;
        int(7480)
        ["title"]=&gt;
        string(42) "Što trebate znati o glikemijskom indeksu?"
        ["path"]=&gt;
        string(18) "1365518301_407.jpg"
        ["cid"]=&gt;
        int(266)
        ["name"]=&gt;
        string(18) "Zdravlje općenito"
      }
      [1]=&gt;
      array(5) {
        ["id"]=&gt;
        int(7460)
        ["title"]=&gt;
        string(43) "Najbolje i najgore namirnice za alergičare"
        ["path"]=&gt;
        string(18) "1367243414_727.jpg"
        ["cid"]=&gt;
        int(286)
        ["name"]=&gt;
        string(8) "Alergije"
      }
      [2]=&gt;
      array(5) {
        ["id"]=&gt;
        int(7467)
        ["title"]=&gt;
        string(22) "Zašto volimo đumbir?"
        ["path"]=&gt;
        string(18) "1366634351_784.jpg"
        ["cid"]=&gt;
        int(267)
        ["name"]=&gt;
        string(15) "Biljni lijekovi"
      }
      [3]=&gt;
      array(5) {
        ["id"]=&gt;
        int(7459)
        ["title"]=&gt;
        string(25) "Najzdravija pića svijeta"
        ["path"]=&gt;
        string(18) "1367309468_956.jpg"
        ["cid"]=&gt;
        int(274)
        ["name"]=&gt;
        string(15) "Muško zdravlje"
      }
    }

于2013年9月24日添加:

好的,我刚刚发现问题出在index.html.twig模板中的这部分代码中:

  

{%include&#34; KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig&#34; {articles:bblocks.articleHnewest(4),flag:0} only%}

如果我直接在index.html.twig中从模板articleDefaultBlock.html.twig传输代码,一切正常。由于某些原因,包括模板内的模板转换了文章对象的结果列表,而不是包含我想要的记录数据的数组。

1 个答案:

答案 0 :(得分:0)

好的,我只想知道导致这种行为的原因,当我将多个变量传递给include twig标签时,bblocks.articleHnewest(4)结果被转换为对象列表,所以当我删除第二个变量flag = 0时结果保持原始状态。为什么会发生这种情况我不知道,如果有人知道请分享。

解决这个问题的方法是分别在twig中设置变量,然后将这些变量全部传递给包含这样的包含:

  

{%set newestArt = {articles:bblocks.articleHnewest(4),flag:0}%}

然后

  

{%include“KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig”with {articles:bblocks.articleHnewest(4),flag:1}

你把

  

{%include“KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig”with newestArt%}

感谢。