EC-CUBE3.0.16
商品購入時にプラグインにて国情報を保存したいと思っています。
会員で購入する場合は問題なく国情報が保存できたのですが、ゲスト購入の場合に $app['orm.em']->flush(); のタイミングで次のようなエラーが発生してしまいます。
このエラーはどうすれば解決できるでしょうか?
■エラー
ORMInvalidArgumentException in ORMInvalidArgumentException.php line 91:
A new entity was found through the relationship 'Eccube\Entity\Order#Country' that was not configured to cascade persist operations for entity: ニューカレドニア. To solve this issue: Either explicitly call EntityManager#persist() on this unknown entity or configure cascade persist this association in the mapping for example @ManyToOne(..,cascade={"persist"}).
以下、実装したソースを記載します。
app/Plugin/Test/event.yml
Shopping/nonmember.twig:
- [onRenderShoppingNonmember, NORMAL]
front.shopping.nonmember.complete:
- [onFrontShoppingNonmemberComplete, NORMAL]
front.shopping.index.initialize:
- [onFrontShoppingIndexInitialize, NORMAL]
app/Plugin/Test/TestEvent.php
class TestEvent
{
private $sessionKey = 'eccube.plugin.test.shopping.nonmember';
private $app;
public function __construct($app)
{
$this->app = $app;
}
/**
* お客様情報の入力ページに国を表示する.
*/
public function onRenderShoppingNonmember(TemplateEvent $event)
{
$app = $this->app;
$source = $event->getSource();
// 国を表示
$snipet = "<div id="detail_box__country" class="form-group form-inline {% if form.country.vars.errors is not empty %}has-error{% endif %}">{{ form_widget(form.country) }}</div>";
$search = '<div id="detail_box__zip"';
$replace = $snipet.$search;
$source = str_replace($search, $replace, $source);
$event->setSource($source);
}
/**
* お客様情報の入力ページで国をセッションに登録する.
*/
public function onFrontShoppingNonmemberComplete(EventArgs $event)
{
$app = $this->app;
$form = $event['form'];
$Order = $event['Order'];
$Country = $form->get('country')->getData();
$nonMember = $app['session']->get($this->sessionKey);
$nonMember['Customer'] = $app['eccube.service.shopping']->getNonMember('eccube.front.shopping.nonmember');
$nonMember['Country'] = $Country;
$app['session']->set($this->sessionKey, $nonMember);
}
/**
* 購入で国を登録する.
*/
public function onFrontShoppingIndexInitialize(EventArgs $event)
{
$app = $this->app;
$Order = $event['Order'];
if (!$app->isGranted('IS_AUTHENTICATED_FULLY')) {
$nonMember = $app['session']->get($this->sessionKey);
$Customer = $nonMember['Customer'];
$Customer->setCountry($nonMember['Country']);
} else {
$Customer = $app->user();
}
// 標準では注文情報に国が設定されない
$Order->setCountry($Customer->getCountry());
$app['orm.em']->persist($Order);
foreach ($Order->getShippings() as $Shipping) {
// 標準では配送情報に国が設定されない
$CustomerAddress = $this->app['eccube.repository.customer_address']->findOneBy(
array('Customer' => $Customer),
array('id' => 'ASC')
);
if ($CustomerAddress) {
$Shipping->setCountry($CustomerAddress->getCountry());
$app['orm.em']->persist($Shipping);
}
}
$app['orm.em']->flush(); // ★ ここでエラーが発生する
}
}
----------------
EC-CUBE 《プラチナ》ランクパートナー
トエビス株式会社 田中 宏典
EC-CUBEの機能やデザインのカスタマイズ承ります。