Hi there!

First of all, I strongly suggest you to just use DoctrineORMModule. You can
read a tutorial about it at
http://marco-pivetta.com/doctrine-orm-zf2-tutorial/

So far, it looks like your EntityManager is `null` (reading your
description of the error).


Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


On 6 February 2013 09:57, eokorie [via Zend Framework Community] <
ml-node+s634137n465903...@n4.nabble.com> wrote:

> Hello,
>
> I am starting off with ZF2.. I have managed to integrate doctrine into the
> framework... In order to avoid having to load the doctrine entity manager
> in every controller, I have gotten it to load on a onBoostrap event, even
> though another way I could have done it would be do load the entity in a
> base controller and get all other controllers in my modules to extend that
> controller....
>
> However, I want to load the Doctrine Entity in a base service of thus
> making all service extend the base service.
>
> So far, this is what I have set up in my base service file:
>
>
> [pre] ** /**
> * Created by JetBrains PhpStorm.
> * User: emmanuel.okorie
> * Date: 29/01/13
> * Time: 09:02
> * To change this template use File | Settings | File Templates.
> */
>
>
> namespace CPortal\Service;
>
>
> use CPortal\ServiceManager\EntityManagerAwareInterface;
> use Doctrine\ORM\EntityManager;
> use Zend\ServiceManager\ServiceManager;
> use Zend\ServiceManager\ServiceLocatorAwareInterface;
> use Zend\ServiceManager\ServiceLocatorInterface;
>
>
> abstract class BaseService implements ServiceLocatorAwareInterface,
> EntityManagerAwareInterface
> {
>
> /**
> * @var Doctrine\ORM\EntityManager
> */
> public $em = null;
>
>
> /**
> * @var ServiceManager\ServiceLocatorInterFace
> */
> protected $serviceLocator = null;
>
>
> /**
> * @var \Doctrine\ORM\EntityManager
> */
> protected $entityManager = null;
>
> /**
> * @param \Doctrine\ORM\EntityManager $manager
> * @return AbstractService
> */
> public function setEntityManager(EntityManager $manager)
> {
> $this->entityManager = $manager;
> return $this;
> }
>
> /**
> * @return \Doctrine\ORM\EntityManager|null
> */
> public function getEntityManager()
> {
> return $this->entityManager;
> }
>
> /**
> * @param ServiceLocatorInterFace $serviceLocator
> */
> public function setServiceLocator(ServiceLocatorInterFace $serviceLocator)
> {
> $this->serviceLocator = $serviceLocator;
> }
>
> /**
> * @returns ServiceLocatorInterFace
> */
> public function getServiceLocator()
> {
> return $this->serviceLocator;
> }
>
> }
> [/pre]
>
>
> I have a service file which extends the base service and it looks like so:
>
> [pre]
>
> ** /**
> * Created by JetBrains PhpStorm.
> * Date: 31/01/13
> * Time: 09:11
> * To change this template use File | Settings | File Templates.
> */
>
> namespace CPortal\Service;
>
> use CPortal\Service\BaseService;
> use Zend\ServiceManager\ServiceManager;
> use Doctrine\ORM\Mapping as ORM;
> use Doctrine\ORM\Query\Expr\Base;
>
> class Attendees extends BaseService
> {
> protected $entity = '';
>
> /**
> * Get all attendees not deleted
> * @return array
> */
> public function getAttendees()
> {
> //$attendees =
> $this->em->getRepository('\CPortal\Entity\Attendees')->findAll();
>
> $qb = $em->createQueryBuilder();
>
> //$dql = "SELECT w.Id_workshop FROM Workshops\Entity\Workshops w";
>
> $query = $qb->select('a.id_attendee, a.title, a.name')
> ->from('CPortal\Entity\Attendees','a')
> ->setFirstResult(0)
> ->setMaxResults(10);
> $attendees = $query->getQuery()->getResult();
> return $attendees;
> }
>
> }
>
> [/pre]
>
> And finally, the controller that calls the above service looks like so:
>
> [pre]
>
> ** /**
> * Zend Framework (http://framework.zend.com/)
> *
> * @link http://github.com/zendframework/ZendSkeletonApplication for the
> canonical source repository
> * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (
> http://www.zend.com)
> * @license http://framework.zend.com/license/new-bsd New BSD License
> */
>
> namespace Cms\Controller;
>
>
> use CPortal\Controller\BaseController;
> use Doctrine\ORM\Query\Expr\Base;
> use Zend\Config\Config;
> use Zend\Log\Logger;
> use CPortal\Service\Attendees;
> use Zend\View\Model\ViewModel;
>
> class AttendeesController extends BaseController
> {
>
> public function indexAction()
> {
>
> $attendeeService = new Attendees();
> $attendees = $attendeeService->getAttendees();
>
> return new ViewModel(
> array(
> 'attendees' => $attendees
> )
> );
> }
>
> }
>
> [/pre]
>
>
> All required files have been autoloaded through the autoload_classmap
> file. But when I run the code, i get this a "Call to a member function
> createQueryBuilder() on a non-object in XXX" error in the service file that
> extends the base service... Where am I going wrong? Still trying to get my
> head round ZF2...
>
> Thanks
>
> Emmanuel
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://zend-framework-community.634137.n4.nabble.com/Doctrine-2-ZF2-Service-tp4659035.html
>  To start a new topic under Zend Framework, email
> ml-node+s634137n634138...@n4.nabble.com
> To unsubscribe from Zend Framework Community, click 
> here<http://zend-framework-community.634137.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=634137&code=b2NyYW1pdXNAZ21haWwuY29tfDYzNDEzN3wxNzE0OTI1MTk4>
> .
> NAML<http://zend-framework-community.634137.n4.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>




--
View this message in context: 
http://zend-framework-community.634137.n4.nabble.com/Doctrine-2-ZF2-Service-tp4659035p4659036.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to