title: How to update an Entity using Symfony 3.4 Components
body: <p>In defending my question, here are some of the references that I have looked at before posting.</p>
  
  <p><a href="https://stackoverflow.com/questions/45978280/symfony-doctrine-update-query-fail">Symfony Doctrine update query fail</a></p>
  
  <p><a href="https://stackoverflow.com/questions/37205609/doctrine-is-not-a-valid-entity-or-mapped-super-class">Doctrine Is not a valid entity or mapped super class</a></p>
  
  <p><a href="https://stackoverflow.com/questions/20887211/how-to-fix-class-is-not-a-valid-entity-or-mapped-super-class">How to fix class is not a valid entity or mapped super class?</a></p>
  
  <p>And there were more than a half dozen more that I am not listing. The error message that I am getting this.</p>
  
  <pre><code>[18-Apr-2020 12:30:50 America/New_York] PHP Fatal error:  Uncaught Doctrine\ORM\Mapping\MappingException: Class "Doctrine\ORM\QueryBuilder" is not a valid entity or mapped super class. in C:\oerm_dev\www\dev\future5_2\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php:346
  Stack trace:
  #0 C:\oerm_dev\www\dev\future5_2\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\Driver\AnnotationDriver.php(93): Doctrine\ORM\Mapping\MappingException::classIsNotAValidEntityOrMappedSuperClass('Doctrine\\ORM\\Qu...')
  #1 C:\oerm_dev\www\dev\future5_2\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\ClassMetadataFactory.php(151): Doctrine\ORM\Mapping\Driver\AnnotationDriver-&gt;loadMetadataForClass('Doctrine\\ORM\\Qu...', Object(Doctrine\ORM\Mapping\ClassMetadata))
  #2 C:\oerm_dev\www\dev\future5_2\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory.php(332): Doctrine\ORM\Mapping\ClassMetadataFactory-&gt;doLoadMetadata(Object(Doctrine\ORM\Mapping\ClassMetadata), NULL, false, Array)
  #3 C:\oerm_dev\www\dev\future5_2\vendor\doctrine\orm\lib\Doctrine in C:\oerm_dev\www\dev\future5_2\vendor\doctrine\orm\lib\Doctrine\ORM\Mapping\MappingException.php on line 346
  </code></pre>
  
  <p>In troubleshooting the code I know that it is this line. </p>
  
  <pre><code>$this-&gt;_em-&gt;persist($qb);
  </code></pre>
  
  <p>I know it is this line because if I comment out that line and the line below it the code runs through with no additional errors. However, nothing is saved. I have read over</p>
  
  <p><a href="https://www.doctrine-project.org/projects/doctrine-phpcr-odm/en/latest/reference/query-builder.html#the-querybuilder" rel="nofollow noreferrer">https://www.doctrine-project.org/projects/doctrine-phpcr-odm/en/latest/reference/query-builder.html#the-querybuilder</a></p>
  
  <p>While there is a lot of information getting data from but there is not a lot written about UPDATE to.</p>
  
  <p><a href="https://symfony.com/doc/current/doctrine.html#updating-an-object" rel="nofollow noreferrer">https://symfony.com/doc/current/doctrine.html#updating-an-object</a></p>
  
  <p>Now here is the code</p>
  
  <pre><code>namespace OpenEMR\Repositories;
  
  use Doctrine\ORM\EntityRepository;
  use OpenEMR\Entities\FormEncounter;
  
  class FormEncounterRepository extends EntityRepository
  {
      /**
       * @param FormEncounter
       * @param $message
       * @return $response
       */
  
      public function update($message)
      {
          $response = false;
          $enc = $_SESSION['encounter'];
          try {
              $qb = $this-&gt;_em-&gt;getRepository($this-&gt;_entityName)-&gt;createQueryBuilder('fe');
              $qb-&gt;update(FormEncounter::class)
                  -&gt;set('fe.reason', 'text')
                  -&gt;where('fe.encounter', 'num')
                  -&gt;setParameter('text', $message)
                  -&gt;setParameter('num', $enc)
                  -&gt;getQuery();
              $this-&gt;_em-&gt;persist($qb);
              $this-&gt;_em-&gt;flush();
              $response = true;
          } catch (Exception $e) {
              return 'An Error occured during save: ' .$e-&gt;getMessage();
          }
          return $response;
      }
  }
  </code></pre>
  
  <p>My question is why is this error being thrown? I have code very close to this the uses the insert command and it works. The major difference in the two is that the select uses the Query::HYDRATE_ARRAY function. </p>
  
  <p>Ok I get the point. That was some information that I have not run across. I was doing it wrong. </p>
  
  <p>However, the <a href="https://symfony.com/doc/current/doctrine.html#updating-an-object" rel="nofollow noreferrer">https://symfony.com/doc/current/doctrine.html#updating-an-object</a> documentation don't tell you that they are persisting because. Now to stop my whining. </p>
  
  <p>I went back in and changed the controller code thinking as was said to persist there and not the Repository. I added a function. Here is my controller code.</p>
  
  <pre><code> /**
   * @param $message
   */
  public function addAddendum(FormEncounter $message)
  {
      //get current encounter
      $enc = $GLOBALS['encounter'];
      $reason = $this-&gt;repository;
      //get reason from database
      $getReason = $reason-&gt;findOneBy(['encounter' =&gt; $enc]);
      $this-&gt;updatedReason = $getReason-&gt;getReason() ."\r\n ADENDUM: \r\n". $message;
      self::updateReason($this-&gt;updateReason());
  
      return "Finished";
  
  }
  
  /**
   * @param \OpenEMR\Entities\FormEncounter $reason
   */
  private function updateReason(FormEncounter $reason)
  {
      try {
          //get current encounter
          $enc = $GLOBALS['encounter'];
          $aReason = $this-&gt;repository;
          $findReason = $aReason-&gt;findOneBy(['encounter' =&gt; $enc]);
          $this-&gt;save = $findReason-&gt;setReason($reason);
          $this-&gt;entityManager-&gt;persist($this-&gt;save);
          $this-&gt;entityManager-&gt;flush();
      } catch (Exception $e) {
  
      }
  }
  </code></pre>
  
  <p>Now I have a new Error that I can't figure out. </p>
  
  <pre><code>    [18-Apr-2020 20:33:17 America/New_York] PHP Fatal error:  Uncaught Error: Call to a member function persist() on null in C:\oerm_dev\www\dev\future5_2\src\Events\Addendum\AddendumEsign.php:71
  Stack trace:
  #0 C:\oerm_dev\www\dev\future5_2\src\Events\Addendum\AddendumEsign.php(54): OpenEMR\Events\Addendum\AddendumEsign-&gt;updateReason('Test Ribbon, no...')
  #1 C:\oerm_dev\www\dev\future5_2\src\Events\Addendum\addendum_esign_helper.php(38): OpenEMR\Events\Addendum\AddendumEsign-&gt;addAddendum('This is a test ...')
  #2 {main}
    thrown in C:\oerm_dev\www\dev\future5_2\src\Events\Addendum\AddendumEsign.php on line 71
  </code></pre>
  
  <p>So, check my logic. $findReason makes a call to the database to bring back the reason that I am looking to update. On finding that reason that I want to update. $this->save = $findReason->setReason($reason) should save the new reason object that was passed from the addAddendum() method. The error is telling me that line 71 is null when I know I passed it $reason object. </p>