我开始了一个新项目,想在Zend框架(1.11)中使用Doctrine 2。
我在Bootstrap &
这是我创建的第一个模型:
<?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教程,但没有一个有多大帮助。
* UPDATE *
下面是我在bootstrap中的Doctrine init:
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;
}
下面是doctrine的配置(在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"
假设你的学说设置是正确的,Zend_Registry::get('em');
返回entitymanager,那么这里是Zend框架集成学说ORM的正确语法:
$group=$em->getRepository ( 'Entities'Post' )->find (1));
或
$group=$em->getRepository ( 'Entities'Post' )->findOneByUserid (1));
如果你正在寻找一本关于理论和ZF的有价值的书,那么"简单的PHP网站与Zend框架"是一个很好的开始。
如果问题仍然存在,则尝试更改
/** @Id @Column(type="integer") @GeneratedValue */
/**
* @Id @Column(type="integer")
* @GeneratedValue(strategy="AUTO")
*/
查看这里的注释文档。PS:如果您有关于ZF和Doctrine的问题,请输入您从那时起使用的ZF-Doctrine集成方法。