質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

Q&A

解決済

1回答

769閲覧

CakePHPで編集フォームが作成されない

hikaru215

総合スコア13

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

0グッド

0クリップ

投稿2019/02/08 09:21

前提・実現したいこと

CakePHP3.7.3(PHP7.3.1)で、スケジュールアプリを作っています。
「schedules/edit/13」のようにedit テンプレートへ飛んだ際に、Formに該当するidの情報が表示されず空欄になってしまいます。

公式(https://book.cakephp.org/3.0/ja/views/helpers/form.html)を見ると、
「FormHelper は、追加 または 編集 のフォームを作成するかどうかを自動的に検出するために、 Entity オブジェクトを使用します。 提供されたエンティティーが「新しくない」場合は、 編集 フォームとして作成されます。」とあります。

なぜ編集フォームが作成されていないのかが分かりません。お助けください。
編集画面

該当のソースコード

php

1//SchedulesController.php 2 3<?php 4namespace App\Controller; 5 6use App\Controller\AppController; 7use Cake\ORM\TableRegistry; 8use Cake\Error\Debugger; 9 10/** 11 * Schedules Controller 12*/ 13class SchedulesController extends AppController 14{ 15 public $autoRender= true; 16 17 public function initialize() 18 { 19 parent::initialize(); 20 21 $this->viewBuilder()->enableAutoLayout(true); 22 $this->loadComponent('Calendar'); 23 } 24 /** 25 * Index method 26 * 27 * @return \Cake\Http\Response|void 28 */ 29 public function index($scope='week', $year=false, $month=false, $day=false) 30 { 31 if(!$year) $year = date('Y'); 32 if(!$month) $month = date('m'); 33 if(!$day) $day = date('d'); 34 $current = sprintf("%d/%02d/%02d", $year, $month, $day); 35 $this->getRequest()->getSession()->write('SCHEDULE_INDEX_CONDITION', compact('scope', 'current')); 36 37 $times = $this->Calendar->scopeToTimes($scope, $year, $month, $day); 38 $schedules = $this->findByTimes($times); 39 $this->set(compact('schedules', 'scope', 'times', 'current')); 40 } 41 42 /** 43 * View method 44 * 45 * @param string|null $id Schedule id. 46 * @return \Cake\Http\Response|void 47 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 48 */ 49 public function view($id = null) 50 { 51 $schedule = $this->Schedules->get($id, [ 52 'contain' => [] 53 ]); 54 55 $this->set('schedule', $schedule); 56 } 57 58 /** 59 * Add method 60 * 61 * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. 62 */ 63 public function add() 64 { 65 $schedule = $this->Schedules->newEntity(); 66 if ($this->request->is('post')) { 67 $schedule = $this->Schedules->newEntity($this->request->getData()); 68 Debugger::dump($schedule, 10); 69 70 if ($this->Schedules->save($schedule)) { 71 $this->Flash->success(__('The schedule has been saved.')); 72 73 return $this->redirect(['action' => 'index']); 74 } 75 $this->Flash->error(__('The schedule could not be saved. Please, try again.')); 76 } 77 $this->set(compact('schedule')); 78 } 79 80 public function edit($id = null) 81 { 82 $schedule = $this->Schedules->get($id, ['contain' => []]); 83 if($this->request->is(['patch', 'post', 'put'])){ 84 $schedule = $this->Schedules->patchEntity($schedule, $this->request->getData()); 85 Debugger::dump($schedule,10); 86 87 if($this->Schedules->save($schedule)){ 88 $this->Flash->success(__('The schedule has been updated.')); 89 return $this->redirect(['action' => 'index']); 90 }else{ 91 $this->Flash->error(__('The shcedule could not be update. Please, try again.')); 92 } 93 } 94 $this->set(compact('schedule')); 95 $this->set('_serialize', ['schedule']); 96 } 97 98 public function delete($id = null) 99 { 100 //$this->request->allowMethod(['post', 'delete']); 101 $schedule = $this->Schedules->get($id); 102 Debugger::dump($schedule); 103 if($this->Schedules->delete($schedule)){ 104 $this->Flash->success(__('The {0} article has been deleted.', $schedule->title)); 105 return $this->redirect(['action' => 'index']); 106 }else{ 107 $this->Flash->error(__('The schedule could not be deleted. Please, try again.')); 108 } 109 } 110 111 public function findByTimes($times) 112 { 113 extract($times); 114 $from = date("Y-n-j H:i:s", $from_time); 115 $to = date("Y-n-j H:i:s", $to_time); 116 117 $record = $this->Schedules->find() 118 ->where(['OR' => [ ['StartDate BETWEEN :from AND :to'], ['EndDate BETWEEN :from AND :to']] ]) 119 ->bind(':from', $from) 120 ->bind(':to', $to) 121 ->order(['StartDate'=>'ASC']); 122 123 return $record; 124 } 125 126 public function redirect($url, $status = null, $exit = true) { 127 if(is_array($url) && $url['action'] == 'index') { 128 $prev = $this->getRequest()->getSession()->read('SCHEDULE_INDEX_CONDITION'); 129 if(!empty($prev)) { 130 extract($prev); 131 $url['action'] .= "/$scope"; 132 $url[] = $current; 133 } 134 } 135 return parent::redirect($url, $status, $exit); 136 } 137 138}

php

1// edit.ctp 2<?= $this->AppForm->create('Schedule') ?> 3<?= $this->AppForm->hidden('id') ?> 4<?= $this->AppForm->input('StartDate', ['type'=>'datetime']) ?> 5<?= $this->AppForm->input('EndDate', ['type'=>'datetime']) ?> 6<?= $this->AppForm->input('title') ?> 7<?= $this->AppForm->input('contents', ['rows'=>'3']) ?> 8 9<tr><td colspan="2"> 10<?= $this->AppForm->submit(__('Save', true), ['div'=>false]) ?> 11<?= $this->AppForm->delete_button('calendarnote/schedules/delete/'.$this->request->data('id')) ?> 12 13</td></tr> 14<?= $this->AppForm->end() ?> 15

php

1// AppFormHelper.php 2<?php 3namespace App\View\Helper; 4 5 6use Cake\View\Helper\FormHelper; 7use Cake\View\View; 8use Cake\Error\Debugger; 9 10/** 11 * AppForm helper 12 */ 13class AppFormHelper extends FormHelper 14{ 15 16 /** 17 * Default configuration. 18 * 19 * @var array 20 */ 21 22 23 function input($fieldName, $options = array()) { 24 $label = null; 25 if(!empty($options['label'])) { 26 $label = $options['label']; 27 } 28 $options['label'] = false; 29 $out = ''; 30 $out .= '<tr>'; 31 $out .= '<th>'; 32 $out .= parent::label($fieldName, $label); 33 $out .= '</th>'; 34 $out .= '<td>'; 35 $out .= parent::input($fieldName, $options); 36 $out .= '</td>'; 37 $out .= '</tr>'; 38 39 return $out; 40 } 41 42 /** 43 * @example of use 44 * echo $appForm->create('Schedule'); 45 * @example of output 46 * <form > 47 * <tabel class="form"> 48 * 49 */ 50 function create($model = null, $options = array()) { 51 $out = parent::create($model, $options); 52 $out .= '<table class="form">'; 53 return $out; 54 } 55 56 /** 57 * @example of use 58 * echo $appForm->end(); 59 * @example of output 60 * </table> 61 * </form> 62 * 63 */ 64 function end($options = null) { 65 $out = '</table>'; 66 $out .= parent::end(); 67 return $out; 68 } 69 70 function delete_button($url){ 71 $out = $this->button(__('Delete', true), ['onclick'=>'javascript:delete_submit(\''.__('Are you sure you want to delete', true).'\', \''.$url.'\');']); 72 $script = ' 73 function delete_submit(confirm, url){ 74 if(window.confirm(confirm)){ 75 location.href = url; 76 } 77 } 78 '; 79 $this->Html->scriptStart(['block'=>true]); 80 echo $script; 81 $this->Html->ScriptEnd(); 82 return $out; 83 } 84} 85

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

PHP

1<?= $this->AppForm->create('Schedule', ['valueSources' => 'schedule']) ?>

投稿2019/02/10 07:03

kunai

総合スコア5405

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

hikaru215

2019/02/11 11:35

回答ありがとうございます(_ _) valueSourcesを付けてみたのですが変化なしでした。。 もう少し調査してみたいと思います。
hikaru215

2019/02/12 02:24 編集

自己解決しました。 <?= $this->AppForm->create($schedule) ?>
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問