0

我在从我的记录集合 ($arrRoleResources) 中加载第 5 条记录时遇到问题,在我运行它之后,它工作正常:-

    $em = Zend_Controller_Front::getInstance()->getParam('bootstrap')->getResource('doctrine');

    $arrRoleResources = $em->getRepository("AJFIT\Entities\UserRoleResources")->findAll();

当我通过这个: -

    foreach($arrRoleResources as $roleResource) {
        self::$_objAcl->allow($roleResource->getRoleFk()->getName(),$roleResource->getResourcesFk()->getModule() . '::' . $roleResource->getResourcesFk()->getController() . '::' . $roleResource->getResourcesFk()->getAction());  
    }

在第 5 次迭代中,它将相关记录类之一从实体更改为正确且正确的代理,但是在进入加载函数时单步执行代理 (AJFITEntityUserRoleResourcesProxy) 后:-

private function _load()
{
    if (!$this->__isInitialized__ && $this->_entityPersister) {
        $this->__isInitialized__ = true;
        if ($this->_entityPersister->load($this->_identifier, $this) === null) {
            throw new \Doctrine\ORM\EntityNotFoundException();
        }
        unset($this->_entityPersister, $this->_identifier);
    }
}

它抛出 EntityNotFoundException。

当我在第 581 行的 BasicEntityPersister.php 中逐步执行 $this->_entityPersister->load() 函数时:-

    $entities = $hydrator->hydrateAll($stmt, $this->_rsm, $hints);

$entities 返回 null,我不确定为什么。

这是我的配置: -

    Root
    |-----application
    |-----library
            |-----AJFIT
            |       |-----Entities (namespaces = AJFIT\Entities)
            |       |        |-----UserResources.php
            |       |        |-----UserRoleResources.php
            |       |        |-----UserRoles.php
            |       |-----Proxies  (namespaces = AJFIT\Proxies) <-auto generated
            |                |-----AJFITEntitiesUserResources.php
            |                |-----AJFITEntitiesUserRoleResources.php
            |                |-----AJFITEntitiesUserRoles.php
            |-----Doctrine
            |-----Zend
            |-----ZendX

我的应用程序配置

    [production]    

    autoloadernamespaces[] = "AJFIT"
    autoloadernamespaces[] = "Doctrine"

    phpSettings.display_startup_errors = 1
    phpSettings.display_errors = 1

    includePaths.library = APPLICATION_PATH "/../library"

    bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
    bootstrap.class = "Bootstrap"

    resources.frontController.moduleDirectory = APPLICATION_PATH "/modules"
    resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
    resources.frontController.params.displayExceptions = 1
    resources.frontController.baseurl = "/"

    resources.layout.layout = "layout"
    resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"

    resources.view.encoding = "UTF-8"
    resources.view.basePath = APPLICATION_PATH "/views/"

    ; ------------------------------------------------------------------------------
    ; Doctrine Database Configuration
    ; ------------------------------------------------------------------------------

    doctrine.conn.host = '127.0.0.1'
    doctrine.conn.user = 'ajfit'
    doctrine.conn.pass = '*****'
    doctrine.conn.driv = 'pdo_mysql'
    doctrine.conn.dbname = 'ajfit'
    doctrine.path.entities = APPLICATION_PATH "../../library/AJFIT/Entities"

我的引导程序:-

 /**
 * Register namespace Default_
 * @return Zend_Application_Module_Autoloader
 */
protected function _initAutoload()
{
    $autoloader = new \Doctrine\Common\ClassLoader('Zend');
    $autoloader->setNamespaceSeparator('_'); 
    $autoloader->register(); 

    return $autoloader;
}

/**
 * Initialize Doctrine
 * @return Doctrine_Manager
 */
public function _initDoctrine() {
    $this->bootstrap('autoload'); 

    // include and register Doctrine's class loader
    require_once(APPLICATION_PATH . '/../library/Doctrine/Common/ClassLoader.php');

    $classLoader = new \Doctrine\Common\ClassLoader(
        'Doctrine', 
        APPLICATION_PATH . '/../library/Doctrine'
    );
    $classLoader->register();

    $classLoader = new \Doctrine\Common\ClassLoader(
        'Symfony', 
        APPLICATION_PATH . '/../library/Doctrine/Symfony'
    );
    $classLoader->register();

    $classLoader = new \Doctrine\Common\ClassLoader(
        'AJFIT', 
        APPLICATION_PATH . '/../library/AJFIT/'
    );
    $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;
    //$cache = new \Doctrine\Common\Cache\ApcCache;
    $config->setMetadataCacheImpl($cache);
    $config->setQueryCacheImpl($cache);

    // choosing the driver for our database schema
    // we'll use annotations

    $driver = $config->newDefaultAnnotationDriver(
        APPLICATION_PATH . '/../library/AJFIT/Entities'
    );

    //$driver = new Doctrine\ORM\Mapping\Driver\XmlDriver(
    //        APPLICATION_PATH . '/../library/AJFIT/Mappings/XML');

    //$driver = new Doctrine\ORM\Mapping\Driver\YamlDriver(
    //        APPLICATION_PATH . '/../library/AJFIT/Mappings/YML');

    $config->setMetadataDriverImpl($driver);

    // set the proxy dir and set some options
    $config->setProxyDir(APPLICATION_PATH . '/../library/AJFIT/Proxies');
    $config->setAutoGenerateProxyClasses(true); 
    $config->setProxyNamespace('AJFIT\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;
}

请有人帮忙,因为我已经为此工作了几个星期,但我似乎没有任何进展。

感谢您的时间

安德鲁

4

2 回答 2

0

它不会直接回答您的问题,但如果我是您,我会为 ZF 和 Doctrine2 使用“Bisna”胶水:https ://github.com/ralphschindler/NOLASnowball

一个好的教程视频:http ://www.zendcasts.com/unit-testing-doctrine-2-entities/2011/02/

标题可能听起来令人困惑,但视频很好地解释了如何集成 ZF 和 Doctrine2。

这种“胶水”对我来说总是很完美,我认为它也可以解决你的问题。

于 2011-09-30T19:37:03.783 回答
0

感谢您的帮助,我已经将教义 2.1.2 和 zend 1.11.11 粘合在一起,没有任何问题,我发现我收到此错误的原因是由于数据库中的关联实体为空导致了正确的错误。

但是,我遇到了一个奇怪的问题,关联实体是代理类,它的方法总是返回 null。我希望有人能对这个主题有所了解,因为 iut 让我发疯。

我正在调用此代码:-

  $arrRoleResources = $em->getRepository("AJFIT\Entity\UserRoleResources")->findAll();

  foreach($arrRoleResources as $roleResource) {

         $name = $roleResource->getRoleFk()->getName();
  }

UserRoleResources 实体:-

namespace AJFIT\Entity;

/**
* UserRoleResources
*
* @Table(name="user_role_resources")
* * @Entity(repositoryClass="AJFIT\Repository\UserRoleResources")
*/
class UserRoleResources
{

   /**
   * @var UserRoles
   *
   * @ManyToOne(targetEntity="UserRoles")
   * @JoinColumn(name="role_fk", referencedColumnName="pk")
   * 
   */

   private $roleFk;

   /**
   * Get roleFk
   *
   * @return UserRoles $roleFk
   */

   public function getRoleFk()
   {
       return $this->roleFk;
   }
}

用户角色实体:-

namespace AJFIT\Entity;

/**
 * UserRoles
 *
 * @Table(name="user_roles")
 * * @Entity(repositoryClass="AJFIT\Repository\UserRoles")
 */
class UserRoles
{
    /**
    * @var string $name
    *
    * @Column(name="name", type="string", length=255)
    */
    private $name;

    /**
    * @var integer $pk
    *
    * @Column(name="pk", type="integer")
    * @Id
    * @GeneratedValue(strategy="IDENTITY")
    */
    private $pk;

    /**
    * Get name
    *
    * @return string $name
    */
    public function getName()
    {
        return $this->name;
    }
}

我遵循了 zf-boilerplate 预编译示例,如果需要,我可以发布我的配置。谢谢

:-)

于 2011-10-18T07:23:11.973 回答