In defending my question, here are some of the references that I have looked at before posting.
Symfony Doctrine update query fail
Doctrine Is not a valid entity or mapped super class
How to fix class is not a valid entity or mapped super class?
And there were more than a half dozen more that I am not listing. The error message that I am getting this.
[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->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->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
In troubleshooting the code I know that it is this line.
$this->_em->persist($qb);
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
While there is a lot of information getting data from but there is not a lot written about UPDATE to.
https://symfony.com/doc/current/doctrine.html#updating-an-object
Now here is the 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->_em->getRepository($this->_entityName)->createQueryBuilder('fe');
$qb->update(FormEncounter::class)
->set('fe.reason', 'text')
->where('fe.encounter', 'num')
->setParameter('text', $message)
->setParameter('num', $enc)
->getQuery();
$this->_em->persist($qb);
$this->_em->flush();
$response = true;
} catch (Exception $e) {
return 'An Error occured during save: ' .$e->getMessage();
}
return $response;
}
}
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.
Ok I get the point. That was some information that I have not run across. I was doing it wrong.
However, the https://symfony.com/doc/current/doctrine.html#updating-an-object documentation don't tell you that they are persisting because. Now to stop my whining.
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.
/**
* @param $message
*/
public function addAddendum(FormEncounter $message)
{
//get current encounter
$enc = $GLOBALS['encounter'];
$reason = $this->repository;
//get reason from database
$getReason = $reason->findOneBy(['encounter' => $enc]);
$this->updatedReason = $getReason->getReason() ."\r\n ADENDUM: \r\n". $message;
self::updateReason($this->updateReason());
return "Finished";
}
/**
* @param \OpenEMR\Entities\FormEncounter $reason
*/
private function updateReason(FormEncounter $reason)
{
try {
//get current encounter
$enc = $GLOBALS['encounter'];
$aReason = $this->repository;
$findReason = $aReason->findOneBy(['encounter' => $enc]);
$this->save = $findReason->setReason($reason);
$this->entityManager->persist($this->save);
$this->entityManager->flush();
} catch (Exception $e) {
}
}
Now I have a new Error that I can't figure out.
[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->updateReason('Test Ribbon, no...')
#1 C:\oerm_dev\www\dev\future5_2\src\Events\Addendum\addendum_esign_helper.php(38): OpenEMR\Events\Addendum\AddendumEsign->addAddendum('This is a test ...')
#2 {main}
thrown in C:\oerm_dev\www\dev\future5_2\src\Events\Addendum\AddendumEsign.php on line 71
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.