バージョン選択

フォーラム

メニュー

オンライン状況

33 人のユーザが現在オンラインです。 (25 人のユーザが フォーラム を参照しています。)
登録ユーザ: 0
ゲスト: 33
もっと...

サイト内検索

質問 > 管理機能 > プラグインで、一対多のエンティティをデータベースに登録する方法について

管理機能

新規スレッドを追加する

フラット表示 前のトピック | 次のトピック
投稿者 スレッド
yuy-tank
投稿日時: 2018/1/19 17:01
対応状況: 開発中
一人前
登録日: 2017/11/10
居住地:
投稿: 72
プラグインで、一対多のエンティティをデータベースに登録する方法について
プラグインでエンティティとして、Entity/ChoiceとEntity/ChoiceDetailを作りました。Entity/ChoiceとEntity/ChoiceDetailは一対多の関係にあり、Entity/ChoiceはプロパティにChoiceDetails(\Doctrine\Common\Collections\Collectionのインスタンス)を持ち、これはEntity/ChoiceDetailの配列として機能しています。(本体のEntity/OrderとEntity/OrderDetailと同じです。)
また、Entity/ChoiceとEntity/ChoiceDetailに該当するデータベース上のテーブルは、plg_choiceとplg_choice_detailです。

ちなみに、Entity/ChoiceDetailのプロパティは、
choice_name,
choice_value,
choice_detail_id

のみです。ただ、データベース上では、plg_choice_detailはplg_choiceを参照するためにchoice_idという外部キーを持っています。(一対多の関係であるため)



コントローラーでは、フォームからデータを入力し、Entity/Choiceのインスタンスをそのままデータベースに登録するつもりでした。ところが、フォームから、ChoiceDetails(つまり、複数のEntity/ChoiceDetail)に相当する部分に値(choice_name => "テスト",choice_value => 1)1つだけを入れてサブミットしたところ、次のようなエラーが出ました。

ForeignKeyConstraintViolationException in AbstractMySQLDriver.php line 60:
An exception occurred while executing 'INSERT INTO plg_choice_detail (choice_name, choice_value, choice_detail_id) VALUES (?, ?, ?)' with params ["\u30c6\u30b9\u30c81", "1", null]:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`****データベース名*****`.`plg_choice_detail`, CONSTRAINT `FK_CB044321998666D1` FOREIGN KEY (`choice_id`) REFERENCES `plg_choice` (`choice_id`))

このエラーによると、plg_choice_detailがplg_choiceを参照するために持つ外部キーであるchoice_idにエラーがあるみたいです。また、choice_detail_idがオートインクリメントであるにも関わらず、nullが入れられていることも不可解です。
Entity/ChoiceとEntity/ChoiceDetailを同時にデータベースに登録しようとしているからでしょうか?よろしければ、原因を教えていただきたいです。


ちなみにコントローラーでは、Entity/Choiceのみをデータベースに登録(persist(Choice)およびflush())するため、ChoiceDetailsについては、ymlファイルでcascade['persist']として、自動的にデータベースに登録されるようにしています。
以下に、plg_choice_detailとplg_choiceに該当するdoctrineのymlファイルを以下に示しておきます。





[プラグインコード].Entity.Choice.dcm.yml


[プラグインコード]\Entity\Choice:
type: entity
table: plg_choice
repositoryClass: [プラグインコード]\Repository\ChoiceRepository
id:
id:
type: integer
nullable: false
unsigned: false
id: true
column: choice_id
generator:
strategy: AUTO

fields:
choice_label:
type: text
choice_type:
type: text
choice_blank:
type: text

oneToMany:
ChoiceDetails:
targetEntity: [プラグインコード]\Entity\ChoiceDetail
mappedBy: Choice
orderBy:
id: ASC
cascade: ["persist"]

lifecycleCallbacks: { }










[プラグインコード].Entity.ChoiceDetail.dcm.yml

[プラグインコード]\Entity\ChoiceDetail:
type: entity
table: plg_choice_detail
repositoryClass: [プラグインコード]\Repository\ChoiceDetailRepository
id:
id:
type: integer
nullable: false
unsigned: false
id: true
column: choice_detail_id
generator:
strategy: AUTO

fields:
choice_name:
type: text
choice_value:
type: text

manyToOne:
Choice:
targetEntity: [プラグインコード]\Entity\Choice
inversedBy: ChoiceDetails
joinColumn:
name: choice_detail_id
referencedColumnName: choice_id
nullable: false

lifecycleCallbacks: { }










フラット表示 前のトピック | 次のトピック


題名 投稿者 日時
 » プラグインで、一対多のエンティティをデータベースに登録する方法について yuy-tank 2018/1/19 17:01
     Re: プラグインで、一対多のエンティティをデータベースに登録する方法について tsuji 2018/1/25 14:11
       Re: プラグインで、一対多のエンティティをデータベースに登録する方法について yuy-tank 2018/1/26 17:24
         Re: プラグインで、一対多のエンティティをデータベースに登録する方法について 468 2018/1/26 22:34
           Re: プラグインで、一対多のエンティティをデータベースに登録する方法について yuy-tank 2018/2/2 9:51

 



ログイン


EC-CUBE公式 Amazon Payプラグイン

統計情報

総メンバー数は88,968名です
総投稿数は110,019件です

投稿数ランキング

1
seasoft
7367
2
468
3217
3
AMUAMU
2712
4
nanasess
2314
5
umebius
2085
6
yuh
1819
7
h_tanaka
1646
8
red
1570
9
mcontact
1296
10
tsuji
958
11
fukap
907
12
shutta
835
13
tao_s
799
14 ramrun 789
15 karin 689
16 sumida 641
17
homan
633
18 DELIGHT 572
19
patapata
502
20
flealog
485


ネットショップの壺

EC-CUBEインテグレートパートナー

Copyright© EC-CUBE CO.,LTD. All Rights Reserved.