1

I'm using Zend Framework 3, after solving issues on Zendframework 3 - Can't get entityManager on Application/Module I was able to successfully retrieved information only with the following:

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

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

But now, in my view, after passing the $brands variable to the ViewModel

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

I just see an error of empty objects in my view like this:

[{},{}]

Am I missing something?

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

PD: I can see my results, like this, if I print them on my controller:

[{"id":1,"createdAt":{"date":"2017-12-15 06:00:56.000000","timezone_type":3,"timezone":"Europe/Berlin"},"name":"Marca 1"},{"id":2,"createdAt":{"date":"2017-12-15 06:00:59.000000","timezone_type":3,"timezone":"Europe/Berlin"},"name":"Marca 2"}]

This is my entire Controller:

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

}
2
  • Instead of using getArrayResult() you should use an hydrator to extract the data from your brand model. It seems that ViewModel isn't compatible with getArrayResult() somehow. You can also use a standard hydrator provided by zf but be careful as they use reflection and can reduce performances. Commented Dec 15, 2017 at 13:54
  • You are using json_encode on your results. you need to use a json serializer, e.g : stackoverflow.com/questions/47335932/… Commented Dec 15, 2017 at 14:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.