Question

So my problem is to use Doctrine 2 with Zend Framework 2. I installed Doctrine correctly and when I tried to use it in the controller I get this error :

C:\wamp2\www\zf2\vendor\zendframework\zendframework\library\Zend\ServiceManager\ServiceManager.php:518

and this :

Zend\Mvc\Controller\PluginManager::get was unable to fetch or create an instance for getObjectManager

This is my controller :

class BlogController extends AbstractActionController
{

    public function blogAction()
    {
        $article = $this->getObjectManager()->getRepository('\Application\Entity\Article')->findAll();

        return new ViewModel(array('article' => $article));
    }
}

This is my view:

<?php if (isset($article)) : ?>
    <?php foreach($article as $articles): ?>
        <div class="col-lg-12 text-center">
            <img class="img-responsive img-border img-full" src="" alt="">
            <h2><?php echo $articles->getTitle(); ?>
                <br>
                <small><?php echo $articles->getDate(); ?></small>
            </h2>
            <p><?php echo $articles->getContent(); ?></p>
            <a href="<?php echo $this->url('article') ?>" class="btn btn-default btn-lg">Read More</a>
            <hr>
        </div>
    <?php endforeach; ?>
<?php endif; ?>

For the configuration I followed this tutorial

Was it helpful?

Solution

Sadly you didn't follow the tutorial, otherwise you'd have noticed that inside the tutorial they define a function getObjectManager() inside the Controller. You don't define this function and therefore the Controller assumes this to be a ControllerPlugin and therefore asks the ControllerPluginManager to create an instance for this plugin. But no such Plugin was ever registered and that's why you see this error.

TL/DR -> do the tutorial step by step. Understand what you're doing, don't blindly copy paste the lines you think are important.

OTHER TIPS

You forgot to implement the getObjectManager for your controller :

protected function getObjectManager()
    {
        if (!$this->_objectManager) {
            $this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }

        return $this->_objectManager;
    }

this method is included at the end of the IndexController in the tutorial you mentionned.

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