php 学说 class 使用名称空间时未找到
php doctrine class not found when using namespaces
我被教义所困getting started。
我先给你看代码。
/bootstrap.php
namespace MyApp;
require 'vendor/autoload.php';
require 'config/slim.php'; //Some config files it doesn't matter
require 'config/dependencies.php'; //Some config files it doesn't matter
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
$doctrineDbConfig = array(
'driver' => 'pdo_mysql',
'user' => $config['db']['user'],
'password' => $config['db']['pass'],
'dbname' => $config['db']['dbname'],
);
$entityManager = EntityManager::create(
$doctrineDbConfig,
Setup::createYAMLMetadataConfiguration(
array(
__DIR__ . "/config"
),
$isDevMode = true
)
);
/cli-config.php
require "bootstrap.php";
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
如你所见,我在讲道理 "my entity config file is under /config dir",这是产品实体的配置文件:
/config/Product.dcm.yml
Product:
type: entity
table: products
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
所以我运行这个命令:
$ vendor/bin/doctrine orm:schema-tool:update --force --dump-sql
我得到了这个:
[Doctrine\Common\Persistence\Mapping\MappingException]
Class 'Product' does not exist
让我给你看 "Product" class:
/src/Entities/Product.php
namespace MyApp; //If I delete this line, It works
class Product
{
protected $id;
protected $name;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
}
当然,我正在使用您在 bootstrap.php 文件中看到的 composer 自动加载器:
/composer.json
"autoload": {
"psr-4": {
"MyApp\": ["src/", "src/Entities/"]
}
}
我正在映射命名空间 "MyApp"。这在我通过 index.php 请求应用程序时工作正常,但在使用命令行时它不起作用。
如果我删除 Product.php class 中的名称空间行,那么它会起作用并且原则会找到 class.
我已经尝试过使用这个映射:
/composer.json
"autoload": {
"psr-4": {
"": ["src/", "src/Entities/"]
}
}
我已经尝试创建一个命名空间别名,在阅读这篇文章后 answer
请问您对此有何建议?
谢谢。
好吧,我找到了解决方案...也许是新手,我不知道。
如果有人可以验证这一点,我将不胜感激。
我刚刚添加:
配置文件名中的完全限定名称如下:
MyApp.Product.dcm.yml
我在配置文件中使用完全限定名称,如下所示:
MyApp\Product:
type: entity
table: products
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
我被教义所困getting started。
我先给你看代码。
/bootstrap.php
namespace MyApp;
require 'vendor/autoload.php';
require 'config/slim.php'; //Some config files it doesn't matter
require 'config/dependencies.php'; //Some config files it doesn't matter
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
$doctrineDbConfig = array(
'driver' => 'pdo_mysql',
'user' => $config['db']['user'],
'password' => $config['db']['pass'],
'dbname' => $config['db']['dbname'],
);
$entityManager = EntityManager::create(
$doctrineDbConfig,
Setup::createYAMLMetadataConfiguration(
array(
__DIR__ . "/config"
),
$isDevMode = true
)
);
/cli-config.php
require "bootstrap.php";
return \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
如你所见,我在讲道理 "my entity config file is under /config dir",这是产品实体的配置文件:
/config/Product.dcm.yml
Product:
type: entity
table: products
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string
所以我运行这个命令:
$ vendor/bin/doctrine orm:schema-tool:update --force --dump-sql
我得到了这个:
[Doctrine\Common\Persistence\Mapping\MappingException]
Class 'Product' does not exist
让我给你看 "Product" class:
/src/Entities/Product.php
namespace MyApp; //If I delete this line, It works
class Product
{
protected $id;
protected $name;
public function getId()
{
return $this->id;
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
}
当然,我正在使用您在 bootstrap.php 文件中看到的 composer 自动加载器:
/composer.json
"autoload": {
"psr-4": {
"MyApp\": ["src/", "src/Entities/"]
}
}
我正在映射命名空间 "MyApp"。这在我通过 index.php 请求应用程序时工作正常,但在使用命令行时它不起作用。
如果我删除 Product.php class 中的名称空间行,那么它会起作用并且原则会找到 class.
我已经尝试过使用这个映射:
/composer.json
"autoload": {
"psr-4": {
"": ["src/", "src/Entities/"]
}
}
我已经尝试创建一个命名空间别名,在阅读这篇文章后 answer
请问您对此有何建议?
谢谢。
好吧,我找到了解决方案...也许是新手,我不知道。 如果有人可以验证这一点,我将不胜感激。
我刚刚添加:
配置文件名中的完全限定名称如下:
MyApp.Product.dcm.yml
我在配置文件中使用完全限定名称,如下所示:
MyApp\Product:
type: entity
table: products
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
type: string