▼テンプレート
[EC-CUBE] 4.0.2
[レンタルサーバ] ローカルホスト
[OS] MacOS
[PHP] PHP 7.3.1
[データベース] MySQL、5.7.25
[WEBサーバ] MAMP
[ブラウザ] Chrome、バージョン
[導入プラグインの有無] 無し
[カスタマイズの有無] 無し
[現象]
プラグインでDBにMyPluginEvent.phpからデータを保存したい状態でしたが、解決したので共有しておきます。
nanasess さんに教えてもらったのですが、
コンストラクタインジェクションで、 `EntityManager` を定義してEntityManagerを使えるようにしてDBにデータを保存する方法で解決しました。
/** * My constructor. * * @param EntityManagerInterface $entityManager * @param MyRepository $MyRepository */ public function __construct( EntityManagerInterface $entityManager, MyRepository $myRepository ) { $this->myRepository = $myRepository; $My = $this->MyRepository->find(1); if (!$this->myRepository || !$My) { $My = new My(); $My ->setMyFlag(true); $entityManager->persist($My); $entityManager->flush($My); } }