【问题标题】:Symfony2s doctrine:generate:entities doesn't generate repo classesSymfony2s 学说:生成:实体不生成 repo 类
【发布时间】:2013-01-01 23:16:07
【问题描述】:

我选择Symfony2 docs。据说加了

/**
 * @ORM\Entity(repositoryClass="Acme\StoreBundle\Entity\ProductRepository")
 */

在我的实体文件中运行php app/console doctrine:generate:entities Acme 应该会创建ProductRepository 文件。它没有。我不能再澄清了,它只是没有创建那个文件,只是重新创建了以前存在的那些实体文件。

【问题讨论】:

  • 您如何检查文件是否不存在?如果它在 IDE 中,请尝试刷新。该命令没有说明生成存储库文件的任何内容,但无论如何都会生成它们。
  • 在 IDE 和通过 Windows 的资源管理器中。

标签: symfony doctrine


【解决方案1】:

我也有同样的问题

但我在这里找到了答案: http://brentertainment.com/other/docs/book/doctrine/orm.html

如果您在添加 repositoryClass 映射之前已经生成了实体类,则必须自己创建类。幸运的是,这很容易。只需在包的 Repository 目录中创建类,并确保它扩展了 Doctrine\ORM\EntityRepository。创建类后,您可以添加任何方法来查询您的实体。

很简单,我们必须手动完成,因为我们已经运行过一次了

【讨论】:

  • 我正在使用 Symfony 2.6,但似乎不再是这种情况了。我将存储库添加到我的实体上的注释中,并且 Doctrine 生成了存储库,即使我之前生成了没有存储库的实体。
  • 是的,克里斯,事实并非如此:你是对的。对我来说,这一代仍然不起作用。在文档中清楚地遵循了 Tomek 在问题中解释的路径:首先创建实体类(文档示例中的产品);然后,在注释中指定了 repositoryClass 之后,您重新运行命令 dictionary:generate:entities 并且 Doctrine 应该创建一个新文件 ProductRepository.php (当前不存在,因为只有 Product.php 存在,并且是第一次生成的生成命令已启动。
  • 在 symfony 2.7 中你使用这个命令来生成 Entity 和 Repository 类:"php app/console dictionary:generate:entity --entity VisitorData --with-repository"
【解决方案2】:

如果您使用 orm.yml 文件生成实体,您可以定义 repositoryClass,然后再次生成实体:

Acme\StoreBundle\Entity\Product:
type: entity
table: product
...
repositoryClass: Acme\StoreBundle\Entity\ProductRepository
...

然后运行:

php app/console doctrine:generate:entities AcmeStoreBundle

【讨论】:

    【解决方案3】:

    您可以尝试指定特定的捆绑包:

    php app/console doctrine:generate:entities AcmeStoreBundle

    请注意,我有完整的捆绑包名称。

    即使您之前运行过doctrine:generate:entities,这也会有所帮助。

    【讨论】:

      【解决方案4】:

      超级简单的解决方案:

      如果您还没有生成一个实体:

      php app/console doctrine:generate:entity --entity="AppBundle:EntityName" --fields="id:string(255) content:text(100)"
      

      现在将这些注释行修改为您之前生成的实体:

      * @ORM\Table(name="TABLENAME")
      * @ORM\Entity(repositoryClass="AppBundle\Entity\EntityNameRepository")
      

      现在,只需运行:

      php app/console doctrine:generate:entities AppBundle:EntityNameRepository
      

      现在您有了一个实体和存储库。 :)

      【讨论】:

        【解决方案5】:

        要摆脱这个问题并生成 repo 类,您可以临时修改以下文件的末尾symfony\vendor\doctrine\doctrine-bundle\Doctrine\Bundle\DoctrineBundle\Command\generateEntitiesDoctrineCommand.php

        if ($m->customRepositoryClassName 
           && false !== strpos($m->customRepositoryClassName, $metadata->getNamespace())) {
             $repoGenerator->writeEntityRepositoryClass(
                $m->customRepositoryClassName, $metadata->getPath());
        }
        

        使用以下代码:

        if (true) { 
           $output->writeln(
             sprintf('  > AND Repository <comment>%s</comment>', $m->name . "Repository")
           );           
           $repoGenerator->writeEntityRepositoryClass(
             $m->name . "Repository", $metadata->getPath());
        } 
        

        一些解释:在这段代码中,

        • if 条件被简化,使用 'if (true)'(如果你愿意,最终可以完全抑制)
        • $m->customRepositoryClassName 被替换为 $m->name."Repository"
        • 我添加了一些输出,以便在生成 repo 文件时(在终端窗口中)得到很好的通知。

        如果您不使用 'if(true)' 条件,并且想自己检查,您还可以添加一个带有输出的兼性 else 案例,以便在未来:

           else {
               $output->writeln(sprintf('  > NO repository generated for this class'));
            }
        

        修改后,你可以像往常一样重新运行命令:

        php app/console doctrine:generate:entities AcmeStoreBundle
        

        这是一个临时代码,因为问题对我来说直到现在还不是很清楚,我唯一看到的是 它似乎来自 $m->customRepositoryClassName 它返回一个空字符串. 因此,要找到另一个明确的解决方案,一种方法可能是检查元数据对象的方法 customRepositoryClassName...

        【讨论】:

          【解决方案6】:

          基于 Astucieux 的回答:

          if (true) { 
              $fullRepositoryClassName = $name . "\\Repository\\" . $basename . "Repository";
              $output->writeln(
                  sprintf('  > AND Repository <comment>%s</comment>', $fullRepositoryClassName)
              );           
              $repoGenerator->writeEntityRepositoryClass(
                  $fullRepositoryClassName, $metadata->getPath());
          } 
          

          【讨论】:

          • 这应该是什么意思?
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-03-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多