![](/img/trans.png)
[英]How to deserialize json to entity with relationship in Symfony 3.4?
[英]How to update an Entity using Symfony 3.4 Components
在為我的問題辯護時,這里有一些我在發布之前看過的參考資料。
如何修復 class 不是有效實體或映射的超級 class?
還有超過六個我沒有列出。 我收到此錯誤消息。
[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
在對代碼進行故障排除時,我知道就是這一行。
$this->_em->persist($qb);
我知道是這一行,因為如果我注釋掉該行及其下方的行,則代碼將運行而不會出現其他錯誤。 但是,什么都沒有保存。 我讀過
雖然有很多信息是從中獲取數據的,但關於 UPDATE 的文章並不多。
https://symfony.com/doc/current/doctrine.html#updating-an-object
現在這里是代碼
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;
}
}
我的問題是為什么會拋出這個錯誤? 我的代碼非常接近這個使用插入命令並且它可以工作。 兩者的主要區別在於 select 使用 Query::HYDRATE_ARRAY function。
好的,我明白了。 那是一些我沒有遇到過的信息。 我做錯了。
但是, https://symfony.com/doc/current/doctrine.html#updating-an-object文檔並沒有告訴您它們會持續存在,因為。 現在停止我的抱怨。
我回去並更改了 controller 代碼,因為據說會持續存在而不是存儲庫。 我添加了一個 function。 這是我的 controller 代碼。
/**
* @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) {
}
}
現在我有一個我無法弄清楚的新錯誤。
[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
所以,檢查我的邏輯。 $findReason 調用數據庫以恢復我要更新的原因。 找到我要更新的原因。 $this->save = $findReason->setReason($reason) 應該保存從 addAddendum() 方法傳遞的新原因 object。 當我知道我通過了 $reason object 時,錯誤告訴我第 71 行是 null。
其中一些特定於我正在內部工作的程序。 錯誤消息只是幫助找出問題所在的指南。 在收到錯誤消息之后,就我而言,它變成了找到正確解決方案的旅程。
此代碼的目的是更新表中的字段並保存更新的信息。
這些是類。 首先是 controller class。
namespace OpenEMR\Events\Addendum;
use Doctrine\ORM\Mapping as ORM;
use OpenEMR\Common\Database\Connector;
use OpenEMR\Common\Logging\Logger;
use OpenEMR\Entities\FormEncounter;
use OpenEMR\Repositories\FormEncounterRepository;
use Symfony\Component\Config\Definition\Exception\Exception;
class AddendumEsign
{
/**
* @var
*/
private $entityManager;
private $repository;
private $updatedReason;
/**
* @var string
* @ORM\Column(type="string")
*/
public function __construct()
{
$this->logger = new Logger(FormEncounterRepository::class);
$database = Connector::Instance();
$entityManager = $database->entityManager;
$this->repository = $entityManager->getRepository(FormEncounter::class);
}
/**
* the addAddendum is passed the addendum to be added to the reason
* that is currently in the form_encounter table. This method purpose is to edit
* the currently stored reason
*/
/**
* @param $message
* @return string
*/
public function addAddendum($message)
{
//get current encounter
$enc = $GLOBALS['encounter'];
$reason = $this->repository; //This is my database connector
//get reason from database
$getReason = $reason->findOneBy(['encounter' => $enc]);
$this->updatedReason = $getReason->getReason() ."\r\n ADENDUM: \r\n". $message;
self::setReason($this->updatedReason);
return "Finished";
}
/**
* The purpose of this block is to insert the new reason in to the object.
* Then it is passed to the reposistory to save / update the reason.
*/
/**
* @param $reason
*/
private function setReason($reason)
{
try {
//get current encounter
$enc = $GLOBALS['encounter'];
$aReason = $this->repository;
$findReason = $aReason->findOneBy(['encounter' => $enc]);
//update the reason to the new reason for the addendum
$findReason->setReason($reason);
//call the reposistory to store the object and pass the object
$save = $this->repository;
$updatedReason = $save->update($findReason);
} catch (Exception $e) {
}
return $updatedReason;
}
}
現在存儲庫 class。
namespace OpenEMR\Repositories;
use Doctrine\ORM\EntityRepository;
use OpenEMR\Entities\FormEncounter;
use Symfony\Component\Config\Definition\Exception\Exception;
class FormEncounterRepository extends EntityRepository
{
/**
* @param FormEncounter
* @param $message
* @return $response
*/
public function update($message)
{
$response = false;
try {
//Since it is already an object ready for storing.
//Doctrine can figure out from here to replace into database entry.
//I learned that persist and flush only work in the repository and not in the controller.
$result = $this->_em->persist($message);
$this->_em->flush();
$response = true;
} catch (Exception $e) {
return 'An Error occured during save: ' .$e->getMessage();
}
return $response;
}
}
希望這可以幫助某人尋找可能的答案。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.