Question

I'm developing an application with zf2,with doctrin2 in it, but I have a problem. 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

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

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

Notice Undefined variable: sm in >www\httpdocs\ciro\module\Application\src\Application\View\Helper\newshelper.php on line 25

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

I also tried to insert this code in the module but without result

$sm = $e->getApplication()->getServiceManager();
Was it helpful?

Solution

for access to entitymanager in view helper you should pass service locator to view helper. you can pass service or service loacator in 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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top