开发者

Extend / Customize a doctrine's entity repository in Symfony

开发者 https://www.devze.com 2023-04-05 14:15 出处:网络
I need help with doctrine in Symfony,开发者_运维问答 I\'d like to extend/customize an entity repository... so I made a class in the Repository folder of the bundle (which didn\'t exist) but I don\'t k

I need help with doctrine in Symfony,开发者_运维问答 I'd like to extend/customize an entity repository... so I made a class in the Repository folder of the bundle (which didn't exist) but I don't know what else to do apart of extend the class from EntityRepository.

Here is the code:

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

?>

So here is how it looks in my project:

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


This question is old, but if you or someone viewing this question doesn't know the answer yet, it is that you need to tell Doctrine to use your custom repository class for that entity.

Example from the Symfony 2 Doctrine documentation (using annotations; you can of course use YAML or XML if you prefer):

// 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
{
    //...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消