Zendframework 3 - 使用ViewModel清空对象数组

时间:2017-12-15 13:34:56

标签: php zend-framework doctrine zend-framework3

我使用Zend Framework 3,在解决了Zendframework 3 - Can't get entityManager on Application/Module上的问题后,我只能通过以下方式成功检索到信息:

$qb = $this->entityManager->createQueryBuilder()
                ->select('s')
                ->from('Application\Entity\Marca', 's');

$brands = $qb->getQuery()->getArrayResult();

但是现在,在我看来,将$brands变量传递给ViewModel

之后
return new ViewModel([
'brands' => $brands
]);

我只是在视图中看到空对象的错误,如下所示:

[{},{}]

我错过了什么吗?

print('OBJECTS: '.json_encode($brands));

PD:如果我在控制器上打印它,我可以看到我的结果:

[{" id":1," createdAt":{" date":" 2017-12-15 06:00:56.000000& #34;" timezone_type":3,"时区":"欧/柏林"}"名称":"马卡1"},{" id":2," createdAt":{" date":" 2017-12-15 06:00: 59.000000"" timezone_type":3,"时区":"欧/柏林"}"名称":&#34 ;马卡2"}]

这是我的整个控制器:

<?php
/**
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2016 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Application\Controller;

use Zend\Mail;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;

use Application\Entity\User;
use Application\Entity\Categoria;
use Application\Entity\Producto;
use Application\Entity\Marca;

class TiendaController extends AbstractActionController
{

    /**
     * Session manager.
     * @var Zend\Session\SessionManager
     */
    private $sessionManager;

    /**
     * Mail sender.
     * @var Application\Service\MailSender
     */
    private $mailSender;

    /**
     * Entity manager.
     * @var Doctrine\ORM\EntityManager
     */
    private $entityManager;

    /**
     * Constructor. 
     */
    public function __construct($mailSender, $entityManager, $sessionManager)
    {
        $this->mailSender = $mailSender;
        $this->entityManager = $entityManager;
        $this->sessionManager = $sessionManager;
    }

    public function tiendaAction()
    {
        return new ViewModel();
    }

    public function categoryAction()
    {
        // Checks if this is a Javascript request
        $xmlHttpRequst = $this->getRequest()->isXmlHttpRequest();

        if ($xmlHttpRequst) {
            $titles = [];
            for ($i = 0; $i < 10; $i++) {
                $titles[] = "Lorem ipsum dolor {$i}";
            }

            $view = new JsonModel($titles);
            /**
             * Tell the renderer not to show the layout
             * by setting setTerminal to true
             */
            $view->setTerminal(true); 
        } else {
            //print("MANAGER: ".json_encode($this->entityManager)."\n");
            //print("MAIL: ".json_encode($this->mailSender)."\n");

            $categories = $this->entityManager->getRepository(Categoria::class)->findAll();
            // $brands = $this->entityManager->getRepository(Marca::class)->findAll();
            // $sql = 'SELECT u FROM Application\Entity\Marca u';
            // $brands = $this->entityManager->createQuery($sql)->execute();
            $qb = $this->entityManager->createQueryBuilder()
                ->select('s')
                ->from('Application\Entity\Marca', 's');

            $brands = $qb->getQuery()->getArrayResult();

            print('OBJECTS: '.json_encode($brands));

            return new ViewModel([
                'brands' => $brands
            ]);
        }     

        return $view; 
    }

}

0 个答案:

没有答案