【问题标题】:In Doctrine 2(.1) - Can I have a (virtual) repository for a MappedSuperclass entity?在 Doctrine 2(.1) - 我可以为 MappedSuperclass 实体创建一个(虚拟)存储库吗?
【发布时间】:2011-10-15 19:48:02
【问题描述】:

我有一个 MappedSuperclass 抽象类 (AbstractImage),其他 2 个实体继承自它(UrlImageUploadImage)。

超类与另一个实体 (Post) 有关联,我需要一种方法来自动从 Post 实体获取和设置 UrlImageUploadImage 的实例(= 意味着我'将在Post 实体上有一个addImage,它将知道给定实体是UploadImage 还是UrlImage 实体)。

同样,由于每个实体都有自己的 id 用于记录,因此我需要一个具有 find 方法的虚拟存储库,该方法将获得 idtypeurl 或 @987654338 @) 并在适当的存储库(UploadImageRepositoryUrlImageRepository)上调用 find 方法。

获得一个普通的存储库(如UrlImageRepository)很容易:

$em->getRepository('UrlImage');

但是我怎样才能在MappedSuperclass 上做到这一点?

以下是有关情况的更多信息:How can I do complex entity associations queries in doctrine 2? (virtual entity)

【问题讨论】:

    标签: inheritance doctrine repository associations doctrine-orm


    【解决方案1】:

    还没有尝试过,但您可以尝试创建 3 个存储库,并让 UploadImageRepositoryUrlImageRepository 扩展第三个(基本、虚拟)存储库。

    顺便说一句。关于 MappedSuperclasses 的一点建议......总是抽象,否则你会遇到各种其他错误。

    【讨论】:

      【解决方案2】:

      仅供参考。可以为 AbstractImage 加载存储库,因此 find 方法适用于每个继承的实体:

      $imageRepository = $entityManager->getRepository('MyBundle:AbstractImage');
      $urlImage = $imageRepository->find(1); //assuming record with id=1 is UrlImage entity
      $uploadImage = $imageRepository->find(2); //assuming record with id=2 is UploadImage entity
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-01
        • 1970-01-01
        • 2022-10-12
        • 2010-10-24
        • 2014-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多