1

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

<?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 %}

我无法访问您可以在 BuildingsBlock 类 dql 中看到的键“c.name”和“c.id as cid”,因为在 twig 中解析时,我的 $result 变量以某种方式转换为对象列表而不是数组.

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

这是 BuldingBlocks->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"
      }
    }

添加 24.09.2013:

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

{% 仅包含“KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig” 和 {articles:bblocks.articleHnewest(4), flag: 0 } %}

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

4

1 回答 1

0

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

解决这个问题的方法是分别在 twig 中设置变量,然后使用 include 将它们全部传递,如下所示:

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

然后改为

{% 包括“KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig” {文章:bblocks.articleHnewest(4),标志:1}

你把

{% include "KprCentarZdravljaBundle:Parts:articleDefaultBlock.html.twig" with latestArt %}

谢谢。

于 2013-09-24T13:21:26.907 回答