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.
Doctrine
by something that can be instantiated... Clearly neitherinterface
norabstract class
cannot be. Maybe someone else could elaborate on this... – Jovan Perovic