Ced
Ced

Reputation: 199

Repository with symfony2 and Doctrine not working

I'm trying to work with Entity Repository to write my custom functions.

I have an Entity and his Repository generated from yaml file

Yaml file

Bluesys\WeekupBundle\Entity\Event:
  type: entity
  repositoryClass: Bluesys\WeekupBundle\Repository\Event
  fields:
    id:
      id: true
      type: integer
      generator:
        strategy: AUTO
  ...

Entity code automatically generated

namespace Bluesys\WeekupBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* Event
*/
class Event
{
    /**
    * @var integer
    */
    private $id;

    ...

}

Repository code automatically generated I juste wrote the function isHidden

namespace Bluesys\WeekupBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * Event
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class Event extends EntityRepository
{
    /**
     * isHidden
     *
     * @return bool  
     */
    public function isHidden()
    {
       return true;
    }
}

The Controller code

namespace Bluesys\WeekupBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Bluesys\WeekupBundle\Event\Event;

...

class TimelineController extends Controller
{

    public function testAction(){

        $repository = $this->getDoctrine()->getManager()->getRepository('BluesysWeekupBundle:Event');
        $event = $repository->findOneById( 73 );

        return $this->render('BluesysWeekupBundle::test.html.twig', array( 'event' => $event ));
    }

    ...

And the view code

{{ event.isHidden }}

I get this error : Method "isHidden" for object "Bluesys\WeekupBundle\Entity\Event" does not exist in BluesysWeekupBundle::test.html.twig at line 1

Can somebody help me by telling me what is missing ?

Upvotes: 0

Views: 528

Answers (1)

xurshid29
xurshid29

Reputation: 4210

Repository classes are used only for selecting/fetching data. They are not the part of entity/object. If you really want to call isHidden method by repository you can acheive this by passing the whole repository to template (return $this->render('BluesysWeekupBundle::test.html.twig', array( 'event' => $repository ));), but this is very bad idea.

Instead you can put isHidden() method into your entity class and call it as event.isHidden..

Upvotes: 1

Related Questions