質問 > 管理機能 > プラグインで、一対多のエンティティをデータベースに登録する方法について |
管理機能
フラット表示 | 前のトピック | 次のトピック |
投稿者 | スレッド |
---|---|
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 |