在symfony中扩展/自定义doctrine的实体存储库

R01*_*010 0 entity doctrine symfony1 repository

我需要symfony中的doctrine帮助,我想扩展/自定义一个实体存储库...所以我在bundle的Respository文件夹中创建了一个类(它不存在),但我不知道还有什么从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;
        }
    }
}

?>
Run Code Online (Sandbox Code Playgroud)

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

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

Mic*_*ael 6

这个问题很陈旧,但是如果您或查看此问题的人还不知道答案,那么您需要告诉Doctrine为该实体使用自定义存储库类.

Symfony 2 Doctrine文档中的示例(使用注释;如果您愿意,您当然可以使用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
{
    //...
}
Run Code Online (Sandbox Code Playgroud)