【问题标题】:Symfony 4 : Namespace issue when generating entities with doctrineSymfony 4:使用学说生成实体时的命名空间问题
【发布时间】:2018-02-01 21:28:47
【问题描述】:

我目前正在使用 Symfony 4。 我用 ORM 和注释写了一个实体

#src/Entity/User.php
<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * 
 * User
 * 
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 */
class User
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $email;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $motdepasse;

    /**
     * @ORM\Column(type="string", length=255, unique=true)
     */
    private $username;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $nom;
...

我使用“php bin/console dictionary:generate:entities App\Entity\User”生成实体。不幸的是,它在名称空间“App\Entity”的 php 文件“src/App/Entity/User.php”中生成了一个实体。但是,由于 psr-4 规则,命名空间应该是“App\App\Entity”。如果我添加第二个“应用程序”,我会遇到与存储库和控制台生成相关的问题。如果我让一个“应用程序”,Symfony 认为命名空间是错误的。 这是我的服务配置: config/services.yml 这是我的学说配置: config/packages/doctrine.yml

我已经在各处(文档、教程、stackoverflow 等)寻找了 2 天的答案。与 Symfony 3 相比,Symfony 4 确实发生了变化。 谢谢您的帮助。 (任何链接都可以帮助我)

【问题讨论】:

  • 在 Symfony 4 中使用 "bin/console make:entity User" 可能需要使用 "composer require maker" 安装新的 maker 包,是的,在 S4 中有很多小改动。
  • 看到这个github.com/doctrine/DoctrineBundle/issues/729,我想会回答你的问题。
  • 感谢 Cerad,但很遗憾。我用这个命令创建了我的实体(带有 ORM 注释的实体)。
  • 谢谢 dlondero。我将按照我在此 URL 中找到的内容:创建一个 symfony 3.3 安装原则:映射:从现有数据库中导入复制并粘贴实体文件到我的新 symfony 4 项目中(在每个项目中将“AppBundle”更改为“App” ) 新 symfony 4 项目中的学说:schema:update --force
  • 如果您需要使用现有数据库,这很有意义。如果您这样做只是为了自动创建 getter/setter,您可以使用适当的 IDE 来实现。

标签: php symfony namespaces doctrine entity


【解决方案1】:

Symfony 4 Src 是默认的 App 文件夹。

【讨论】:

    【解决方案2】:

    Composer 还遵守由 COMPOSER_MEMORY_LIMIT 环境变量定义的内存限制。 您可以将其设置为COMPOSER_MEMORY_LIMIT=-1,然后运行:

    composer require doctrine maker
    

    查看https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors

    【讨论】:

      【解决方案3】:

      我找到了解决办法:

      1) 你必须要求学说制定者通过

      composer require doctrine maker
      

      2) 您想通过下面的命令创建实体,但请确保完成第 3 步和第 4 步。然后回到这里。然后您可以转到步骤#5

      php bin/console make:entity YourEntity
      

      3) 转到第 150 行和第 170 行之间的“vendor/doctrine/doctrine-bundle/Mapping/DisconnecteMetadataFacroty.php”。然后在'///'斜线之间添加条件

      #/vendor/doctrine/doctrine-bundle/mapping/disconnectedMetadataFactory.php
      
      private function getBasePathForClass($name, $namespace, $path)
      {
          $namespace = str_replace('\\', '/', $namespace);
          $search = str_replace('\\', '/', $path);
          $destination = str_replace('/'.$namespace, '', $search, $c);
      
          ///
          if ($namespace === 'App/Entity') {
              $destination = str_replace('/Entity', '', $search, $c);
          } else {
              $destination = str_replace('/'.$namespace, '', $search, $c);
          }
          ////
      
      
          if ($c != 1) {
              throw new \RuntimeException(sprintf('Can\'t find base path for "%s" (path: "%s", destination: "%s").', $name, $path, $destination));
          }
      
          return $destination;
      }
      

      4) 转到第 367 行和第 375 行之间的“vendor/doctrine/orm/lib/Doctrine/ORM/Tools/EntityGenerator.php”。然后将注释行替换为以下两行。

      #/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/EntityGenerator.php
      
      /*$path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metadata->name) . $this->extension;*/
          $metaNamePath = substr($metadata->name, 0, 4) === 'App\\' ? substr($metadata->name, 4) : $metadata->name;
          $path = $outputDirectory . '/' . str_replace('\\', DIRECTORY_SEPARATOR, $metaNamePath) . $this->extension;
      

      5) 到这里,你可以通过

      生成getter和setter
      php bin/console doctrine:generate:entities App:YourEntity
      

      【讨论】:

      • 应避免修改供应商代码,包更新时将替换它。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-16
      • 1970-01-01
      • 2017-01-26
      相关资源
      最近更新 更多