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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探