Php 获取实体元数据
我试图从条令实体中读取注释元数据,以便实例化自定义存储库:Php 获取实体元数据,php,doctrine-orm,entity,metadata,Php,Doctrine Orm,Entity,Metadata,我试图从条令实体中读取注释元数据,以便实例化自定义存储库: $userRepository = new \My\Repository\UserRepository( $entityManager, $classMetadata ); 要获取$classMetadata,我有以下几点: $userRepository = $entityManager->getRepository('\My\Entity\User'); 我的用户实体: namespace My\Entity; use
$userRepository = new \My\Repository\UserRepository( $entityManager, $classMetadata );
要获取$classMetadata
,我有以下几点:
$userRepository = $entityManager->getRepository('\My\Entity\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:
我已经按照条令教程[1]中所述设置了bootstrap.php
和cli config.php
文件,现在可以运行bin/doctor
命令行实用程序。有趣的结果:
bin/doctrine orm:validate-schema
输出:
[Mapping] OK - The mapping files are correct.
[Database] OK - The database schema is in sync with the mapping files.
[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.
但当我这样做的时候:
bin/doctrine orm:info
输出:
[Mapping] OK - The mapping files are correct.
[Database] OK - The database schema is in sync with the mapping files.
[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] 我最终设法解决了这个问题。我用一个相关问题回答了另一个问题: