原则,命名空间和自动加载实体 [英] Doctrine, namespace and autoload entities

查看:166
本文介绍了原则,命名空间和自动加载实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中使用Doctrine 2。我有一些问题。我阅读文档,但可能我做错了。



我想自动加载实体类。而且文档中的方法不起作用。



我的bootstrap.php

 <?php 

require_oncevendor / autoload.php;
使用Doctrine\ORM\Tools\Setup;
使用Doctrine\ORM\EntityManager;
使用Doctrine\ORM\Mapping\Driver;

$ paths = array(../ Entities);
$ isDevMode = false;

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

//连接配置
$ dbParams = array(
'driver'=>'pdo_mysql',
'user'=>'xxx'
'password'=>'xxx',
'dbname'=>'xxx',
);

$ driver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(new Doctrine\Common\Annotations\AnnotationReader(),array('../ Entities')) ;
$ config = Setup :: createAnnotationMetadataConfiguration($ paths,$ isDevMode);
$ config-> setMetadataDriverImpl($ driver);
$ em = EntityManager :: create($ dbParams,$ config);

我在Entities目录中有我的模型类。我使用教条客户端生成它们。他们看起来不错开始有ids,setter,getter和namespace。



/Entities/ArticleCat.php

 <?php 

命名空间实体;

使用Doctrine\ORM\Mapping作为ORM;

/ **
* ArticleCat
*
* @ ORM\Table(name =article_cat)
* @ ORM\Entity
* /
class ArticleCat
{

我想要的脚本使用原则:

 <?php 
require_once'bootstrap.php'

$ article = $ em-> find('ArticleCat',21);
echo $ article-> getName();

它不工作。只有当我以这种方式使用它时,我才能实现从实体模型中删除命名空间

  ?php 
require_once'bootstrap.php';
require_once'Entities / ArticleCat.php'; //这行添加(手动加载)


$ article = $ em-> find('ArticleCat',21);
echo $ article-> getName();

使用原则和自动加载实体的正确方法是什么?为什么命名空间是一个问题?



我的错误:

 致命错误:未捕获异常'Doctrine\Common\Persistence\Mapping\MappingException'
与消息'类'ArticleCat'不存在'
/ myproject / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping / MappingException.php:96
堆栈跟踪:#0 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php(40):
Doctrine\Common\Persistence\Mapping\MappingException :: nonExistingClass('ArticleCat')
#1 / myproject / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping
/AbstractClassMetadataFactory.php(267):Doctrine\Common\Persistence\Mapping
\RuntimeReflectionService-> getParentClasses('ArticleCat')
#2 / myproject / vendor / doctrine / common / lib / Doctrine / Common / Persistence / Mapping
/AbstractClassMetadataFactory.php(29 7):Doctrine\Common\Persistence\Mapping
\AbstractClassMetadataFactory-> getParentClasses('ArticleCat')
#3 / myproject / vendor / doc in / myproject / vendor / doctrine / common / lib / Doctrine / Common / Persistence
/Mapping/MappingException.php第96行


解决方案

如果您正在使用作曲家,您需要确保您的composer.json中有以下自动加载:

  {
require:{
...
},
autoload:{
psr-0 :{:Entity /}
}
}

解决了我的问题。我不在我的实体类中使用任何命名空间。我的composer.json住在我的公司目录,这里引用的Entity目录是inc / Entity。


I want to use Doctrine 2 in my project. I've some problems with it. I read the docs but probably I'm doing something wrong.

I want to autoload entities classes. And method from the docs is not working.

My bootstrap.php

<?php

require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Mapping\Driver;

$paths = array("../Entities");
$isDevMode = false;

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

// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'xxx',
    'password' => 'xxx',
    'dbname'   => 'xxx',
);

$driver = new Doctrine\ORM\Mapping\Driver\AnnotationDriver(new Doctrine\Common\Annotations\AnnotationReader(),array('../Entities'));
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$config->setMetadataDriverImpl($driver);
$em = EntityManager::create($dbParams, $config);

I've got my model's classes in Entities directory. I've generated them using doctrine client. They looks OK. There are ids, setters, getters and namespace at the beginning.

/Entities/ArticleCat.php

<?php

namespace Entities;

use Doctrine\ORM\Mapping as ORM;

/**
 * ArticleCat
 *
 * @ORM\Table(name="article_cat")
 * @ORM\Entity
 */
class ArticleCat
{

My script where I want to use doctrine:

<?php
  require_once 'bootstrap.php';

  $article = $em->find('ArticleCat', 21);
  echo $article->getName();

It's not working. It works only when I use it this way and I remove namespace from Entity model.

<?php
  require_once 'bootstrap.php';
  require_once 'Entities/ArticleCat.php'; //this line added (manual load)


  $article = $em->find('ArticleCat', 21);
  echo $article->getName();

What's the proper way of using doctrine and autoload entities? Why the namespace is a problem?

My errors:

Fatal error: Uncaught exception 'Doctrine\Common\Persistence\Mapping\MappingException' 
with message 'Class 'ArticleCat' does not exist' in 
/myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:96
Stack trace: #0 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php(40): 
Doctrine\Common\Persistence\Mapping\MappingException::nonExistingClass('ArticleCat') 
#1 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping
   /AbstractClassMetadataFactory.php(267): Doctrine\Common\Persistence\Mapping
   \RuntimeReflectionService->getParentClasses('ArticleCat') 
#2 /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping
   /AbstractClassMetadataFactory.php(297): Doctrine\Common\Persistence\Mapping
   \AbstractClassMetadataFactory->getParentClasses('ArticleCat') 
#3 /myproject/vendor/doc in /myproject/vendor/doctrine/common/lib/Doctrine/Common/Persistence
   /Mapping/MappingException.php on line 96

解决方案

If you are using composer, you want to be sure that you have the following 'autoload' in your composer.json:

{
        "require": {
           ...
        },
        "autoload": {
            "psr-0": {"": "Entity/"}
        }
}

This fixed the problem for me. I'm not using any namespace in my entity class though. My composer.json lives in my inc directory and the Entity directory referenced here is at inc/Entity.

这篇关于原则,命名空间和自动加载实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆