【问题标题】:Not finding models when using Doctrine 2 & Zend Framework使用 Doctrine 2 和 Zend Framework 时找不到模型
【发布时间】:2023-03-15 13:45:01
【问题描述】:

我开始了一个新项目,想在 Zend Framework (1.11) 中使用 Doctrine 2。

我在 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 教程,但没有一个有太大帮助。

* 更新 *

这是我在引导程序中的教义初始化:

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" 中的模型

【问题讨论】:

    标签: php zend-framework doctrine


    【解决方案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 框架的简易 PHP 网站”是一个不错的起点。

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

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

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

    在此处查看annotation documentation。 PS:如果您对 ZF 和 Doctrine 有任何问题,请输入您从那时起使用的 ZF-Doctrine 集成方法。

    【讨论】:

    • 感谢您的回答,但这并不能解决 Entities\Post 未加载的问题。我认为这与 Zend 命名约定有关,但我不能完全确定。
    • @xil3 如果是这样,那么您的学说设置应该有问题。您使用的是哪种学说集成方法?您在 config.ini 中正确设置了学说配置吗?您将实体放在哪里?
    • @xil3 据我所知,您的自动加载器中没有定义实体路径。这是我的:$classLoader = new \Doctrine\Common\ClassLoader ( 'Entities', realpath ( Zend_Registry::get ( 'config' )-&gt;resources-&gt;entityManager-&gt;connection-&gt;entities ), 'loadClass' ); $autoloader-&gt;pushAutoloader ( array ($classLoader, 'loadClass' ), 'Entities' ); 请尝试使用以下集成方法:https://github.com/wjgilmore/z2d2
    • 感谢您的帮助 drfanai - 我刚刚看了一个视频,显然有很多方法可以做到这一点,但我显然在这里遗漏了一些关键配置。它开始变成一个巨大的黑客来让它工作,所以我决定只使用 Symfony2,它从一开始就很有魅力,它使用新的 PHP 5.3 命名空间,ZF 还没有。当 ZF2 发布时我会回到这个问题,但在那之前没有意义。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-07
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    相关资源
    最近更新 更多