【问题标题】:Extend / Customize a doctrine's entity repository in Symfony在 Symfony 中扩展/自定义一个学说的实体存储库
【发布时间】:2011-09-15 10:10:55
【问题描述】:

我在 Symfony 中需要教义方面的帮助,我想扩展/自定义一个实体存储库...所以我在包的 Repository 文件夹中创建了一个类(它不存在)但我不知道除了从 EntityRepository 扩展类之外还能做什么。

代码如下:

<?php

class InvitadoRepository extends EntityRepository
{
    public function findOneByIdJoinedToCategory($id)
    {
        $query = $this->getEntityManager()
        ->createQuery('
            SELECT p, c FROM AcmeStoreBundle:Product p
            JOIN p.category c
            WHERE p.id = :id'
        )->setParameter('id', $id);

        try {
            return $query->getSingleResult();
        } catch (\Doctrine\ORM\NoResultException $e) {
            return null;
        }
    }
}

?>

这就是它在我的项目中的样子:

http://dl.dropbox.com/u/29094109/respositoryextend.tiff

【问题讨论】:

    标签: symfony1 doctrine repository entity


    【解决方案1】:

    这个问题很老,但是如果你或查看这个问题的人还不知道答案,那就是你需要告诉 Doctrine 为那个实体使用你的自定义存储库类。

    来自Symfony 2 Doctrine documentation 的示例(使用注释;如果您愿意,当然可以使用 YAML 或 XML):

    // src/Acme/StoreBundle/Entity/Product.php
    namespace Acme\StoreBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * @ORM\Entity(repositoryClass="Acme\StoreBundle\Repository\ProductRepository")
     */
    class Product
    {
        //...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-15
      • 2021-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-20
      相关资源
      最近更新 更多