doure5236 2016-09-18 07:35 采纳率: 0%
浏览 97
已采纳

使用命名空间时找不到php教程类

I got stuck following the doctrine getting started.

Let me show you the code first.

/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);

As you see I am telling doctrine "my entity config file is under /config dir", this is the config file for the product entity:

/config/Product.dcm.yml

Product:
  type: entity
  table: products
  id:
    id:
      type: integer
      generator:
        strategy: AUTO
  fields:
    name:
      type: string

So I run this command:

$ vendor/bin/doctrine orm:schema-tool:update --force --dump-sql

And I got this:

[Doctrine\Common\Persistence\Mapping\MappingException]  
Class 'Product' does not exist         

Let me show you the "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;
    }
}

Of course I am using composer autoloader as you see in the bootstrap.php file:

/composer.json

"autoload": {
    "psr-4": {
        "MyApp\\": ["src/", "src/Entities/"]
    }
}

I am mapping the namespace "MyApp". This is working fine when I request the app through the index.php, but it is not working when using the command line.

If I delete the namespace line in the Product.php class, then It works and doctrine finds the class.

I have already tryed using this mapping :

/composer.json

"autoload": {
    "psr-4": {
        "": ["src/", "src/Entities/"]
    }
}

And I have already tryed creating a namespace alias, after reading this answer

Could you please give some advice on this?

Thank you.

  • 写回答

1条回答 默认 最新

  • dtkf64283 2016-09-18 10:19
    关注

    Well I've found the solution... maybe a newbie one, I don't know. I would appreciate if somebody please could validate this.

    I've just added:

    Fully qualified name in the config file name like this:

    MyApp.Product.dcm.yml
    

    I use the fully qualified name into the config file like this:

    MyApp\Product:
      type: entity
      table: products
      id:
        id:
          type: integer
          generator:
            strategy: AUTO
      fields:
        name:
          type: string
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog