0
我試圖讀取從學說實體註釋元數據實例化一個自定義庫的目的:獲取主義實體元數據
$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
我缺少什麼?
UPDATE:
我已經試過如下:
$userRepository = $entityManager->getRepository('\My\Entity\User');
但我發現了同樣的錯誤。
更新2:
我已經設置了bootstrap.php
和cli-config.php
文件作爲教義教程[1]中描述的,我現在可以運行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