Започнах нов проект и исках да използвам Doctrine 2 в Zend Framework (1.11).
Конфигурирах всичко в рамките на Bootstrap & Config, което изглеждаше добре.
Ето първия ми модел, който създадох за употреба:
<?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;
}
А ето и контролера:
<?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);
}
}
Когато се опитам да осъществя достъп до модела Entities\Post
, той просто показва грешка, като казва, че не съществува. Сигурен съм, че това е проблем с конвенцията за именуване на Zend, но опитах няколко различни неща и изглежда нищо не работи.
Някакви идеи? И аз прегледах всички уроци за Doctrine 2/Zend, които можах да намеря, и никой от тях не помогна много.
* АКТУАЛИЗАЦИЯ *
Ето моята Doctrine init в 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;
}
А ето и конфигурацията за доктрина (в 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"
Както можете да видите, той търси моделите в APPLICATION_PATH "/models"