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

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

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

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

Q&A

解決済

1回答

1729閲覧

cakephp データの更新の勉強

maeyoake

総合スコア37

CakePHP

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

0グッド

0クリップ

投稿2016/02/21 05:28

編集2016/02/24 08:26
namespace App\Controller; use App\Controller\AppController; /** * Persons Controller * * @property \App\Model\Table\PersonsTable $Persons */ class PersonsController extends AppController { /** * Index method * * @return \Cake\Network\Response|null */ public function index() { $persons = $this->paginate($this->Persons); $this->set(compact('persons')); $this->set('_serialize', ['persons']); } /** * View method * * @param string|null $id Person id. * @return \Cake\Network\Response|null * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function view($id = null) { $person = $this->Persons->get($id, [ 'contain' => [] ]); $this->set('person', $person); $this->set('_serialize', ['person']); } /** * Add method * * @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise. */ public function add() { $person = $this->Persons->newEntity(); if ($this->request->is('post')) { $person = $this->Persons->patchEntity($person, $this->request->data); if ($this->Persons->save($person)) { $this->Flash->success(__('The person has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('The person could not be saved. Please, try again.')); } } $this->set(compact('person')); $this->set('_serialize', ['person']); } /** * Edit method * * @param string|null $id Person id. * @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise. * @throws \Cake\Network\Exception\NotFoundException When record not found. */ public function edit($id = null) { $person = $this->Persons->get($id, [ 'contain' => [] ]); if ($this->request->is(['patch', 'post', 'put'])) { $person = $this->Persons->patchEntity($person, $this->request->data); if ($this->Persons->save($person)) { $this->Flash->success(__('The person has been saved.')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('The person could not be saved. Please, try again.')); } } $this->set(compact('person')); $this->set('_serialize', ['person']); } /** * Delete method * * @param string|null $id Person id. * @return \Cake\Network\Response|null Redirects to index. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. */ public function delete($id = null) { $this->request->allowMethod(['post', 'delete']); $person = $this->Persons->get($id); if ($this->Persons->delete($person)) { $this->Flash->success(__('The person has been deleted.')); } else { $this->Flash->error(__('The person could not be deleted. Please, try again.')); } return $this->redirect(['action' => 'index']); } } //ここから追加 public function edit($id = null) { $person = $this->Persons->get($id); if ($this->request->is(['post','put'])) { $person = $this->Persons->patchEntity($person, $this->request->data); if ($this->Persons->save($person)) { return $this->redirect(['action' => 'index']); } }else{ $this->set('person',$person); } } }

editが重複しているのはわかっていますが、順番通りにやると、どうしてもこうなってしまいます。

どうすればいいのでしょうか?

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

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

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

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

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

guest

回答1

0

ベストアンサー

もしかして、サンプルソース等が提供されているのでしょうか?
サンプルソースの場合、すでにその章の終わりのソースコードが提供されている場合があるのでeditメソッドがすでにあるのではないでしょうか。
Cannot redeclareは関数が重複した際に出るエラーです。

上のほうのeditが出来上がった後のメソッドのように見えますので、そちらを削除すると良いでしょう。

投稿2016/02/21 05:35

fagai

総合スコア2158

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

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

maeyoake

2016/02/21 05:44

なぜだかわかりませんが、サンプルが既に存在していました。 そして重複している先に書かれていたeditを削除しても エラーが出ます。 質問にその際の画像を記載しました。
fagai

2016/02/21 05:52 編集

間違えました
fagai

2016/02/21 06:44 編集

追記されたエラーは対象のレコードが存在していないからですね。 データがないとeditは出来ないので。。。
maeyoake

2016/02/21 07:29

なぜだかサンプルが勝手に創造されていて、混乱していました。ありがとうございました。 不思議だ。
maeyoake

2016/02/21 08:18

bakeするときにallとしたので、 MVCができたところからでした。 何はともあれ、ありがとうございました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問