使用Doctrine 2&amp ;;时找不到模型Zend框架

时间:2011-12-10 19:35:14

标签: php zend-framework doctrine

我开始了一个新项目,并希望在Zend Framework(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教程,但没有一个能帮助我们。

*更新*

这是我在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"

中的模型

1 个答案:

答案 0 :(得分:1)

假设您的学说设置正确且Zend_Registry::get('em');返回entitymanager,那么这里是Zend Framework集成学说ORM的正确语法:

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

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

如果您正在寻找有关学说和ZF的宝贵书籍,那么“使用Zend Framework轻松实现PHP网站”是一个很好的起点。

如果问题仍然存在,请尝试更改

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

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

点击此处查看annotation documentation。 PS:如果你有关于ZF和Doctrine的问题,请输入你从那时起使用的ZF-Doctrine集成方法。

相关问题