0
votes

I started a new project and wanted to use Doctrine 2 within the Zend Framework (1.11).

I configured everything within the Bootstrap & Config, which seemed to be fine.

Here is my first model that I created for use:

<?php
namespace Entities;
/**
 * @Entity
 * @Table(name="posts")
 */
class Post
{

    /** @Id @Column(type="integer") @GeneratedValue */
    public $id;

    /** @Column(length=100,nullable=true) */
    public $title;

    /** @Column(length=2000) */
    public $message;

    /** @Column(type="integer") */
    public $userId;

    /** @Column(type="timestamp") */
    public $dateAdded;

}

And here is the controller:

<?php

class CityController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $em = Zend_Registry::get('em');

        $group = $em->find('Entities\Post', 1);
    }


}

When I try accessing the Entities\Post model, it just errors out saying that it doesn't exist. I'm sure it's a Zend naming convention problem, but I tried a few different things, and nothing seemed to work.

Any ideas? And I did look through all the Doctrine 2/Zend tutorials I could find and none of them helped much.

* UPDATE *

Here is my Doctrine init in the bootstrap:

public function _initDoctrine() {
    // include and register Doctrine's class loader
    require_once('Doctrine/Common/ClassLoader.php');
    $classLoader = new \Doctrine\Common\ClassLoader(
        'Doctrine',
        APPLICATION_PATH . '/../library/'
    );
    $classLoader->register();

    // create the Doctrine configuration
    $config = new \Doctrine\ORM\Configuration();

    // setting the cache ( to ArrayCache. Take a look at
    // the Doctrine manual for different options ! )
    $cache = new \Doctrine\Common\Cache\ArrayCache;
    $config->setMetadataCacheImpl($cache);
    $config->setQueryCacheImpl($cache);

    // choosing the driver for our database schema
    // we'll use annotations
    $driver = $config->newDefaultAnnotationDriver(
        APPLICATION_PATH . '/models'
    );
    $config->setMetadataDriverImpl($driver);

    // set the proxy dir and set some options
    $config->setProxyDir(APPLICATION_PATH . '/models/Proxies');
    $config->setAutoGenerateProxyClasses(true);
    $config->setProxyNamespace('App\Proxies');

    // now create the entity manager and use the connection
    // settings we defined in our application.ini
    $connectionSettings = $this->getOption('doctrine');
    $conn = array(
        'driver'    => $connectionSettings['conn']['driv'],
        'user'      => $connectionSettings['conn']['user'],
        'password'  => $connectionSettings['conn']['pass'],
        'dbname'    => $connectionSettings['conn']['dbname'],
        'host'      => $connectionSettings['conn']['host']
    );
    $entityManager = \Doctrine\ORM\EntityManager::create($conn, $config);

    // push the entity manager into our registry for later use
    $registry = Zend_Registry::getInstance();
    $registry->em = $entityManager;

    return $entityManager;
}

And here is the config for doctrine (in application.ini):

doctrine.conn.host = 'localhost'
doctrine.conn.user = '****'
doctrine.conn.pass = '****'
doctrine.conn.driv = 'pdo_mysql'
doctrine.conn.dbname = '****'
doctrine.path.models = APPLICATION_PATH "/models"

As you can see, it's looking for the models in the APPLICATION_PATH "/models"

1

1 Answers

1
votes

Assuming your doctrine setup is correct and Zend_Registry::get('em'); returns entitymanager then here is the correct syntax for Zend Framework integrated doctrine ORM:

$group=$em->getRepository ( 'Entities\Post' )->find (1));

or

$group=$em->getRepository ( 'Entities\Post' )->findOneByUserid (1));

If you are looking for a valuable book for doctrine and ZF,then "Easy PHP Websites with the Zend Framework" is a good place to start.

If problem persists, then try changing

/** @Id @Column(type="integer") @GeneratedValue */

to

/**
 * @Id @Column(type="integer")
 * @GeneratedValue(strategy="AUTO")
 */

check here for annotation documentation. PS:If you have issues regarding ZF and Doctrine, please input which ZF-Doctrine integration method you are using since then.