Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think I have solved my own issue. If someone else has a more elegant way of doing achieving these results, feel free to explain. In order to modify all of my queries, I have created a custom EntityManager and custom EntityRepository.</p> <p>In my custom EntityManager, I have overwritten 2 methods. create() and getRepository()</p> <pre><code>public static function create($conn, Configuration $config, EventManager $eventManager = null) { if ( ! $config-&gt;getMetadataDriverImpl()) { throw ORMException::missingMappingDriverImpl(); } switch (true) { case (is_array($conn)): $conn = \Doctrine\DBAL\DriverManager::getConnection( $conn, $config, ($eventManager ?: new EventManager()) ); break; case ($conn instanceof Connection): if ($eventManager !== null &amp;&amp; $conn-&gt;getEventManager() !== $eventManager) { throw ORMException::mismatchedEventManager(); } break; default: throw new \InvalidArgumentException("Invalid argument: " . $conn); } return new MyCustomEntityManager($conn, $config, $conn-&gt;getEventManager()); } </code></pre> <p>The only thing that is changed in this method is that I am returning my own EntityManger(<strong>MyCustomEntityManager</strong>). Then, I overlaid the getRepository method as follows:</p> <pre><code>public function getRepository($entityName) { $entityName = ltrim($entityName, '\\'); if (isset($this-&gt;repositories[$entityName])) { return $this-&gt;repositories[$entityName]; } $metadata = $this-&gt;getClassMetadata($entityName); $repositoryClassName = $metadata-&gt;customRepositoryClassName; if ($repositoryClassName === null) { $repositoryClassName = "Acme\DemoBundle\Doctrine\ORM\MyCustomEntityRepository"; } $repository = new $repositoryClassName($this, $metadata); $this-&gt;repositories[$entityName] = $repository; return $repository; } </code></pre> <p>Here, I have only modified one line as well. Instead of relying on the DBAL Configuration to retreive the default $repositoryClassName, I have specified my own default repository <strong>Acme\DemoBundle\Doctrine\ORM\MyCustomEntityRepository</strong>.</p> <p>Once you have created your own custom EntityRepository, the sky is the limit. You can inject services into the repository(I currently use JMS Di annotations, described below), or perform custom actions against a QueryBuilder in the createQueryBuilder method, like so:</p> <pre><code>use JMS\DiExtraBundle\Annotation as DI; class MyCustomEntityRepository extends EntityRepository { private $myService; public function createQueryBuilder($alias) { $queryBuilder = parent::createQueryBuilder($alias); /** INSERT CUSTOM CODE HERE **/ return $queryBuilder; } /** * @DI\InjectParams({ * "myService" = @DI\Inject("my_service_id") * }) */ public function setMyService(MyServiceInterface $myService) { $this-&gt;myService = $myService; } } </code></pre> <p>Once you have created your own EntityRepository, you should have all of your repositories that need this custom functionality extend MyCustomEntityRepository. You could even take it a step further and create your own QueryBuilder to further extend this.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload