CakePHP3において、関連テーブルの編集がうまくできずに困っています。
データの取り出しはget($id)でできているのですが、getData()すると値が消えてしまっており編集ができないので、わかる方がいらしたらご教授願いたいです。
以下コード、デバッグ結果
controller
public function edit($id) { $pdfPath = Configure::read('ROOT_PATH') . Configure::read('PHOTO_PATH'); $content = $this->Contents->get($id, [ 'contain' => ['Images'] ]); debug($content); $images = $content['images']; if ($this->request->is(['patch', 'post', 'put'])) { $data = $this->request->getData(); debug($data); exit; $content = $this->Contents->patchEntity($content, $data, ['associated' => ['Images']]); if ($this->Contents->save($content)) { $this->Flash->success(__('記事を追加しました。')); return $this->redirect(['action' => 'index']); } } $this->set(compact('content', 'images')); }
template
<?php $this->Html->css('answer.css', ['block' => true]); ?> <?php $this->Html->script('form.js', ['block' => true]); ?> <section class="content-header"> <h1><?php echo __('編集'); ?></h1> </section> <section class="content"> <?php echo $this->Flash->render(); ?> <?php echo $this->Form->create($content, ['type' => 'file']); ?> <div class="box box-primary"> <div class="box-header" role="button"> <h4 class="box-title"><?php echo '記事編集'; ?></h4> </div> <div class="box-body target-primary collapse in"> <div class="form-group"> <div class="article-form"> <?php echo $this->Form->control('title', ['type' => 'text', 'label' => 'タイトル']); ?> <?php echo $this->Form->control('description', ['type' => 'textarea', 'label' => 'ディスクリプション', 'required' => false]); ?> <?php echo $this->Form->control('key_word', ['type' => 'text', 'label' => 'キーワード', 'required' => false]); ?> <?php echo $this->Form->control('taked_at', ['type' => 'date', 'label' => '撮影日', 'required' => false]); ?> <?php echo $this->Form->control('prefecture', ['type' => 'text', 'label' => '都道府県名', 'required' => false]); ?> </div> </div> </div> </div> <div class="photo-form-container box box-info"> <?php foreach($images as $key => $image): ?> <div class="photo-form box" style="padding: 20px;"> <?php echo $this->Form->control('images..text', ['type' => 'textarea', 'class' => 'editor', 'label' => '本文']); ?> <?php echo $this->Form->control('images..photo', ['type' => 'file', 'label' => '写真']); ?> <p class="delete btn btn-primary" style="margin: 10px 0 10px;">この画像を削除する</p> </div> <?php endforeach; ?> <p class="add btn btn-primary" style="margin: 10px 20px;">画像を追加する</p> </div> <?php echo $this->Form->button('登録', ['type' => 'submit', 'class' => 'btn btn-primary']); ?> <?php echo $this->Form->end(); ?> </section> <?php echo $this->element('editor'); ?>
controllerの$this->Contents->get($id, ['contain' => ['Images']]);のデバッグ結果
object(App\Model\Entity\Content) { 'id' => (int) 25, 'title' => 'asdf', 'description' => 'asdf', 'key_word' => 'dfsa', 'taked_at' => object(Cake\I18n\FrozenDate) { 'time' => '2022-12-23T00:00:00+00:00', 'timezone' => 'UTC', 'fixedNowTime' => false }, 'prefecture' => 'asfd', 'created' => object(Cake\I18n\FrozenTime) { 'time' => '2022-02-27T23:18:21+09:00', 'timezone' => 'Asia/Tokyo', 'fixedNowTime' => false }, 'modified' => object(Cake\I18n\FrozenTime) { 'time' => '2022-02-28T09:40:35+09:00', 'timezone' => 'Asia/Tokyo', 'fixedNowTime' => false }, 'deleted' => null, 'images' => [ (int) 0 => object(App\Model\Entity\Image) { 'id' => (int) 4, 'content_id' => (int) 25, 'text' => '<p>sdfa</p>', 'photo' => '1ff976a931617a11eaf35ac3145884f1298f133483563d2c76563e7f0160ce7720220227231821.png', 'created' => object(Cake\I18n\FrozenTime) { 'time' => '2022-02-27T23:18:21+09:00', 'timezone' => 'Asia/Tokyo', 'fixedNowTime' => false }, 'modified' => object(Cake\I18n\FrozenTime) { 'time' => '2022-02-27T23:18:21+09:00', 'timezone' => 'Asia/Tokyo', 'fixedNowTime' => false }, 'deleted' => null, '[new]' => false, '[accessible]' => [ 'content_id' => true, 'text' => true, 'photo' => true ], '[dirty]' => [], '[original]' => [], '[virtual]' => [], '[hasErrors]' => false, '[errors]' => [], '[invalid]' => [], '[repository]' => 'Images' }, (int) 1 => object(App\Model\Entity\Image) { 'id' => (int) 5, 'content_id' => (int) 25, 'text' => '<p>sdfasdasdasda</p>', 'photo' => '1ff976a931617a11eaf35ac3145884f1298f133483563d2c76563e7f0160ce7720220227231821.png', 'created' => object(Cake\I18n\FrozenTime) { 'time' => '2022-02-27T23:18:21+09:00', 'timezone' => 'Asia/Tokyo', 'fixedNowTime' => false }, 'modified' => object(Cake\I18n\FrozenTime) { 'time' => '2022-02-27T23:18:21+09:00', 'timezone' => 'Asia/Tokyo', 'fixedNowTime' => false }, 'deleted' => null, '[new]' => false, '[accessible]' => [ 'content_id' => true, 'text' => true, 'photo' => true ], '[dirty]' => [], '[original]' => [], '[virtual]' => [], '[hasErrors]' => false, '[errors]' => [], '[invalid]' => [], '[repository]' => 'Images' }, (int) 2 => object(App\Model\Entity\Image) { 'id' => (int) 6, 'content_id' => (int) 25, 'text' => '<p>sdfasdasdaasdasdasdasdassda</p>', 'photo' => '1ff976a931617a11eaf35ac3145884f1298f133483563d2c76563e7f0160ce7720220227231821.png', 'created' => object(Cake\I18n\FrozenTime) { 'time' => '2022-02-27T23:18:21+09:00', 'timezone' => 'Asia/Tokyo', 'fixedNowTime' => false }, 'modified' => object(Cake\I18n\FrozenTime) { 'time' => '2022-02-27T23:18:21+09:00', 'timezone' => 'Asia/Tokyo', 'fixedNowTime' => false }, 'deleted' => null, '[new]' => false, '[accessible]' => [ 'content_id' => true, 'text' => true, 'photo' => true ], '[dirty]' => [], '[original]' => [], '[virtual]' => [], '[hasErrors]' => false, '[errors]' => [], '[invalid]' => [], '[repository]' => 'Images' } ], '[new]' => false, '[accessible]' => [ 'title' => true, 'description' => true, 'key_word' => true, 'taked_at' => true, 'prefecture' => true, 'images' => true ], '[dirty]' => [], '[original]' => [], '[virtual]' => [], '[hasErrors]' => false, '[errors]' => [], '[invalid]' => [], '[repository]' => 'Contents' }
編集画面で何も編集せずに登録した時のgetdataのデバッグ結果
[ 'title' => 'asdf', 'description' => 'asdf', 'key_word' => 'dfsa', 'taked_at' => [ 'year' => '2022', 'month' => '12', 'day' => '23' ], 'prefecture' => 'asfd', 'images' => [ (int) 0 => [ 'text' => '', 'photo' => [ 'tmp_name' => '', 'error' => (int) 4, 'name' => '', 'type' => '', 'size' => (int) 0 ] ], (int) 1 => [ 'text' => '', 'photo' => [ 'tmp_name' => '', 'error' => (int) 4, 'name' => '', 'type' => '', 'size' => (int) 0 ] ], (int) 2 => [ 'text' => '', 'photo' => [ 'tmp_name' => '', 'error' => (int) 4, 'name' => '', 'type' => '', 'size' => (int) 0 ] ] ] ]
ご不明点等ございましたら、コメントにてお教えください。
あなたの回答
tips
プレビュー