6
votes

Using Symfony 2.7 and Doctrine 2.5, I have

  • an Interface Alsciende\MyBundle\Model\CycleInterface
  • an abstract class Alsciende\MyBundle\Entity\Cycle that implements the interface
  • a final class AppBundle\Entity\Cycle that extends the abstract class and implements the interface
  • a doctrine orm configuration with resolve_target_entities that maps the interface to the final class

This system works well and I was able to create the database and implements some CRUD in AppBundle, manipulating the target entity directly.

However, I now want to manipulate the target entity in MyBundle, through the Interface. I need to get its repository:

$this->getDoctrine()->getRepository('Alsciende\MyBundle\Model\CycleInterface');

But I get the exception

class 'Alsciende\MyBundle\Model\CycleInterface' does not exist

How can I get the repository of the target entity? That is, how can I call ResolveTargetEntityListener directly to get the name of the entity implementing the interface?

edit:

Why do I need that? Very simply, for example, I need a controller that displays a list of all Cycles. The interface defines that each Cycle has an id and a name. I want to display every Cycle with its name and id. In order to do that, I need to access the repository of the actual Cycle entities.

Alsciende/MyBundle/Model/CycleInterface.php

<?php 

namespace Alsciende\MyBundle\Model;

interface CycleInterface 
{
    public function getId();
    public function getName();
}

Alsciende/MyBundle/Controller/CycleController.php

<?php

namespace Alsciende\MyBundle\Controller;

class CycleController extends Controller
{
    public function indexAction()
    {
        $cycles = $this
            ->getDoctrine()
            ->getRepository('Alsciende\MyBundle\Model\CycleInterface')
            ->findAll();

        // return template with list $cycles
        // using only id and name properties
    }
}

It's the same way that FosUserBundle is able to manage the User entities, even though the User class defined in FosUserBundle is an abstract class.

1
Did you read about Doctrine Inheritance? doctrine-orm.readthedocs.org/en/latest/reference/…Jovan Perovic
I did. I applied what's in that documentation.Alsciende
I've read, on another SO question, that you can only query Doctrine by something that can be instantiated... Clearly neither interface nor abstract class cannot be. Maybe someone else could elaborate on this...Jovan Perovic
The ResolveTargetEntityListener lets us use the interface in place of the entity: doctrine-orm.readthedocs.org/en/latest/cookbook/… . The question is, how to use it in that case.Alsciende
What is it exactly what you are trying to achieve? I mean, why do you need to manipualte the target entity and why do you need to get the repository? Just asking as there might be another way of achieving what you need.gvf

1 Answers

-1
votes

How can I get the repository of the target entity?

In app/config/config.yml put:

doctrine: orm: resolve_target_entities: Namespace\InterfaceInterface: Namespace\Entity\TargetEntityImplementing

BUT

Why do I need that? Very simply, for example, I need a controller that displays a list of all Cycles. The interface defines that each Cycle has an id and a name. I want to display every Cycle with its name and id. In order to do that, I need to access the repository of the actual Cycle entities.

It's not a solution in this case, IMO. I would rather used entity with @DiscriminatorColumn configured: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#single-table-inheritance

Parent class will be some kind of interface you're looking for.

I recommend you to "merge" above: create a parent class which will implement such an interface, then map this interface to this class.