php - Symfony2/Doctrine 找不到自定义存储库类的映射文件

标签 php symfony doctrine-orm annotations metadata

我喜欢教义,但我在映射/注释方面遇到了一些问题。一开始我使用映射文件。然后我转换成注释。现在我想创建自定义存储库类,所以我按照我在此处阅读的内容进行操作:http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes .

不幸的是现在我有一个错误:

No mapping file found named '\src\Vendor\ProductBundle\Resources\config\doctrine/SynchronizationSettingRepository.orm.yml' for class 'Vendor\ProductBundle\Entity\SynchronizationSettingRepository'. 

当然我没有这个文件,因为我不再使用映射了。 我添加了:

  * @ORM\Entity(repositoryClass="Vendor\ProductBundle\Entity\SynchronizationSettingRepository") 

父实体和再生实体。我已经通过命令 php app/console doctrine:generate:entities VendorProductBundle 重新生成了实体,但仍然没有。规则和条令元数据缓存清晰。

这是一个 YML,我想从中再次生成自定义存储库:

Vendor\ProductBundle\Entity\SynchronizationSetting:
    type: entity
    table: synchronization_setting
    repositoryClass: Vendor\SynchronizationSetting\Entity\SynchronizationSettingRepository
    indexes:
        id_product:
            columns:
                - id_product
    id:
        id:
            type: integer
            nullable: false
            unsigned: true
            comment: ''
            id: true
            generator:
                strategy: IDENTITY
    fields:
        open:
            type: string
            nullable: true
            length: 1
            fixed: true
            comment: ''
            default: '0'
        internet:
            type: string
            nullable: true
            length: 1
            fixed: true
            comment: ''
            default: '0'
    manyToOne:
        idProduct:
            targetEntity: Product
            cascade: {  }
            mappedBy: null
            inversedBy: null
            joinColumns:
                id_product:
                    referencedColumnName: id
            orphanRemoval: false
    lifecycleCallbacks: {  }

这是一个存储库类:

<?php
    // src/Acme/StoreBundle/Entity/ProductRepository.php
    namespace Vendor\ProductBundle\Entity;
    use Doctrine\ORM\EntityRepository;

    class SynchronizationSettingRepository extends EntityRepository
    {
        public function findAllOrderedByName()
        {
            return $this->getEntityManager()
                ->createQuery(
                    'SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC'
                )
                ->getResult();
        }
    }

最佳答案

我认为添加 @ORM\Entity 的效果非常小,因为一旦运行 doctrine:generate,完整的 .php 类文件就会被覆盖:实体。您需要将 repositotyClass 添加到您的 YML 文件中。

如果你永远切换到注释,那些 .yml 文件(实际上是 config 中的整个 doctrine 目录)是无用的,除了用于生成基于注释的实体的中间文件。

另一件事:Doctrine 似乎认为您有一个名称以“Repository”结尾的实体。

你能告诉我们YML文件的内容吗?如果您没有它(如您所说),将无法生成实体。您始终可以直接生成基于注释的实体(不需要 YML 中间体)

关于php - Symfony2/Doctrine 找不到自定义存储库类的映射文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21512176/

相关文章:

PHP jQuery 传递多个单词的值

php - Laravel 5.3 - 清除共享主机中的配置缓存

php - Symfony2-表单错误(对于整个表单,不是单个字段)不显示

php - Doctrine mysql,检查数组是否在其他数组中

php - 如何使用弄乱我的页面的外部 css 样式表?

php - 如何在 wordpress 中按存在的缩略图显示帖子顺序?

symfony - Doctrine实体存储库如何将 'andWhere'添加到所有 'find*'函数中?

php - Symfony:驱动程序发生异常:无法使用 mysql 找到驱动程序

Symfony2 持久化相关对象

sql - 教义2.1,其中外键id = ?,编辑: Fixed in Doctrine 2. 2