2011-09-15 46 views
0

我需要關於symfony中doctrine的幫助,我想擴展/定製一個實體庫......所以我在這個包的Res​​pository文件夾中創建了一個類(其中沒有' t存在)但我不知道除了從EntityRepository擴展類以外,還有什麼可做的。在symfony中擴展/自定義一個學說的實體庫

下面是代碼:

<?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

回答

6

這個問題是舊的,但如果你或查看此問題沒有按」不知道答案,那就是你需要告訴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 
{ 
    //... 
}