我正在尝试从Doctrine实体中读取注释元数据,以便实例化自定义存储库:
$userRepository = new \My\Repository\UserRepository( $entityManager, $classMetadata );
要获得$classMetadata
我有以下内容:
我的User
实体:
namespace My\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="My\Repository\UserRepository")
* @ORM\Table(name="users")
*/
class User {
/**
* @ORM\Column(name="user_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
protected $id;
public function getId()
{
return $this->id;
}
public function setId( $id )
{
$this->id = $id;
return $this;
}
}
我的UserRepository
:
namespace My\Repository;
use Doctrine\ORM\EntityRepository;
class UserRepository extends EntityRepository {
}
我的主要剧本:
require '../vendor/autoload.php';
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array('../src/My/Entity');
$isDevMode = TRUE;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'dbname' => 'mydb',
'user' => 'myuser',
'password' => 's3cr3t',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);
$classMetadata = $entityManager->getClassMetadata('\My\Entity\User');
但我一直收到以下错误:
PHP Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Class "My\Entity\User" is not a valid entity or mapped super class.' in vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php:216
我错过了什么?
更新
我尝试了以下内容:
$userRepository = $entityManager->getRepository('\My\Entity\User');
但是我得到了同样的错误。
更新2:
我已按照Doctrine教程[1]中的说明设置了bootstrap.php
和cli-config.php
文件,现在我可以运行bin/doctrine
命令行实用程序。有趣的结果:
bin/doctrine orm:validate-schema
输出:
[Mapping] OK - The mapping files are correct.
[Database] OK - The database schema is in sync with the mapping files.
但是当我这样做时:
bin/doctrine orm:info
输出:
[Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors.
有什么想法吗?
[1] http://docs.doctrine-project.org/en/latest/tutorials/getting-started.html
答案 0 :(得分:0)
我最终设法解决了这个问题。我在一个不同的问题上回答了一个相关的问题:
No mapped Doctrine ORM entities according to the current configuration