简体   繁体   English

布局zf2中的动态内容

[英]dynamic content in layout zf2

I'm developing an application with zf2,with doctrin2 in it, but I have a problem. 我正在使用zf2开发一个应用程序,其中包含doctrin2,但是我遇到了问题。 on each page of the application must insert a block containing news, so the data retrieved from the database. 在应用程序的每个页面上必须插入一个包含新闻的块,以便从数据库中检索数据。 To avoid repeating it in every action I thought I'd put this block in the layout but I do not know how to access the database within the layout. 为了避免在每个动作中都重复它,我以为我会将这个块放在布局中,但是我不知道如何访问布局中的数据库。

For this I created a view helper but I do not know how to access the entity manager to create an instance of NativeQuery 为此,我创建了一个视图助手,但是我不知道如何访问实体管理器来创建NativeQuery的实例。

My goal was mainly to continue to divide the presentation from logic, but I have not found much information on how to solve this problem correctly. 我的目标主要是继续将演示文稿与逻辑分开,但是我没有找到太多有关如何正确解决此问题的信息。

We carry the code I used to create the helper 我们附带了我用来创建助手的代码

use Zend\View\Helper\AbstractHelper;
use Doctrine\ORM\EntityManager as em;
use Doctrine\ORM\NativeQuery as nq;
use Doctrine\ORM\Query\ResultSetMapping as ResultSetMapping;
use Doctrine\ORM\Query\ResultSetMappingBuilder as ResultSetMappingBuilder;
use Admin\Entity\SiteNews;



class Newshelper extends AbstractHelper {
    public function __invoke($str, $find)
    {
        $rsm = new ResultSetMapping();
        $rsm->addEntityResult('Admin\Entity\Sitenews', 'u');
        $rsm->addFieldResult('u', 'id_news', 'idNews');
        $rsm->addFieldResult('u', 'autore', 'autore');
        $rsm->addFieldResult('u', 'news', 'news');

        //$nq = new nq("SELECT * FROM site_news ORDER BY rand() LIMIT 10",$rsm);
        $nativeQuery = $em->createNativeQuery("SELECT * FROM site_news ORDER BY rand() LIMIT 10",$rsm);
        $news = $nativeQuery->getResult();
        $out = "";
        foreach($news as $c){
            $out .="<li data-author=\"" . $c->getAutore() . "\">" . $c->getNews() . "</li>";
        }
        return $out;
    }
} 

The helper works fine, but I do not know how to access the entity manager. 助手工作正常,但是我不知道如何访问实体管理器。 Which is the best way to implement this? 哪个是实现此目标的最佳方法?

ok add other information here. 好的,在这里添加其他信息。 call my helper in layout file in this way 通过这种方式在布局文件中呼叫我的助手

$this->newshelper($sm)

and add your code but i've got errors 并添加您的代码,但我有错误

Notice: Undefined variable: sm in www\\httpdocs\\ciro\\module\\Application\\view\\layout\\layout.phtml on >line 96 注意:未定义的变量:>第96行的www \\ httpdocs \\ ciro \\ module \\ Application \\ view \\ layout \\ layout.phtml中的sm

Warning Missing argument 1 for Application\\View\\Helper\\newshelper::__construct(), called in >D:\\www\\httpdocs\\ciro\\vendor\\zendframework\\zendframework\\library\\Zend\\ServiceManager\\AbstractPluginMa>nager.php on line 170 and defined in >www\\httpdocs\\ciro\\module\\Application\\src\\Application\\View\\Helper\\newshelper.php on line 24 警告在第170行的> D:\\ www \\ httpdocs \\ ciro \\ vendor \\ zendframework \\ zendframework \\ library \\ Zend \\ ServiceManager \\ AbstractPluginMa> nager.php上调用的Application \\ View \\ Helper \\ newshelper :: __ construct()缺少参数1并在第24行的> www \\ httpdocs \\ ciro \\ module \\ Application \\ src \\ Application \\ View \\ Helper \\ newshelper.php中定义

Notice Undefined variable: sm in >www\\httpdocs\\ciro\\module\\Application\\src\\Application\\View\\Helper\\newshelper.php on line 25 注意未定义的变量:第25行的> www \\ httpdocs \\ ciro \\ module \\ Application \\ src \\ Application \\ View \\ Helper \\ newshelper.php中的sm

Fatal error Call to a member function getServiceLocator() on a non-object in >\\www\\httpdocs\\ciro\\module\\Application\\src\\Application\\View\\Helper\\newshelper.php on line 25 致命错误在第25行的> \\ www \\ httpdocs \\ ciro \\ module \\ Application \\ src \\ Application \\ View \\ Helper \\ newshelper.php中的非对象上调用成员函数getServiceLocator()

I also tried to insert this code in the module but without result 我也尝试将此代码插入模块,但没有结果

$sm = $e->getApplication()->getServiceManager();

for access to entitymanager in view helper you should pass service locator to view helper. 要在视图帮助程序中访问entitymanager,您应该将服务定位器传递给视图帮助程序。 you can pass service or service loacator in Module.php . 您可以在Module.php中传递服务或服务定位器。

public function getViewHelperConfig() {
    return array(
        'factories' => array(
            'myviewhelper' => function ($sm) {
                return new View\Helper\MyViewHelper($sm);
            },
        )
    );
}

and in view helper 和视图助手

public function __construct($sm) {
    $entitymanager = $sm->getServiceLocator()->get('doctrine.entitymanager.orm_default');
    }

good luck 祝好运

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM