Doctrine Common
  1. Doctrine Common
  2. DCOM-235

[GH-317] Implemented an ObjectPersisterInterface for entity/object storage

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None

      Description

      This issue is created automatically through a Github pull request on behalf of iltar:

      Url: https://github.com/doctrine/common/pull/317

      Message:

          1. Why is this useful?
            Instead of using the ```repositoryClass```, we use 'Repository as a Service'. This means that we inject our repository service instead of doing ```$em->getRepository('MyEntity')```, this provides typehinting and autocomplete in IDEs. This introduces a minor inconvenience, we need to inject an ```EntityManagerInterface``` to provide persist/flush. We don't want the entire ```EntityManagerInterface``` capabilities just to store an object.

      Using the ```ObjectPersisterInterface``` we can "hide" the ```EntityManagerInterface``` and only let the code know we have the persist and flush methods available. By default this is implemented on the ```EntityManager```, but is also possible to be mocked or replaced by a custom implementation (rotating entity managers?).

            1. Example
              ``` php

      class MyService
      {
      private $op;
      private $es;

      public function __construct(ObjectPersisterInterface $op, MyEntityService $es)

      { $this->op = $op; $this->es = $es; }

      public function doSomething($value)

      { $entity = $this->es->findByValue($value)->setValue('something'); $this->op->flush(); }

      public function doSomethingElse()

      { $entity = new MyEntity(); $this->op->persist($entity); $this->op->flush(); }

      }

      ```

        Issue Links

          Activity

          Hide
          Doctrine Bot added a comment -

          A related Github Pull-Request [GH-317] was closed:
          https://github.com/doctrine/common/pull/317

          Show
          Doctrine Bot added a comment - A related Github Pull-Request [GH-317] was closed: https://github.com/doctrine/common/pull/317

            People

            • Assignee:
              Benjamin Eberlei
              Reporter:
              Doctrine Bot
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: