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

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

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

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

Q&A

解決済

1回答

1181閲覧

CakePHPで作成したアプリケーションを表示したページの上部表示されたエラーの原因がわかりません

haksen

総合スコア23

CakePHP

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

0グッド

0クリップ

投稿2022/04/05 14:09

編集2022/04/06 10:41

CakePHPで作成したアプリケーションで表示したページの上部に
以下のようなエラーが表示されているのですが、原因がわからず困っています。

Notice (1024): Undefined property: PurchaseOrdersController::$PurchaseOrders in C:\xampp\htdocs\test\src\Controller\PurchaseOrdersController.php on line 21 [CORE\src\Controller\Controller.php, line 329]

環境
PHP 8.1.2
CakePHP 4.3.7
mySQL 8.0.28
Apache/2.4.52 (Win64)

手順
1)mysqlでテーブル定義
テーブル:purchase_order
テスト用に1件データを作成
2)CakePHP/bakeでスケルトン作成
bin\cake bake all purchase_orders
3)ブラウザで該当ページを表示

indexとして表示するレコードの一覧は正しく表示されているので
スケルトンの内容に問題無いように思います。

なぜエラーとして表示されているのか、
どうすれば解消できるのか教えていただけませんか。

試したこと
同じ構造を持つテーブル(orders)を作成して同様にスケルトンを作成したのですが
こちらはエラーは表示されませんでした。

イメージ説明

PHP

1コード 2<?php 3declare(strict_types=1); 4 5namespace App\Controller; 6 7/** 8 * PurchaseOrders Controller 9 * 10 * @property \App\Model\Table\PurchaseOrdersTable $PurchaseOrders 11 * @method \App\Model\Entity\PurchaseOrder[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = []) 12 */ 13class PurchaseOrdersController extends AppController 14{ 15 /** 16 * Index method 17 * 18 * @return \Cake\Http\Response|null|void Renders view 19 */ 20 public function index() 21 { 22 $purchaseOrders = $this->paginate($this->PurchaseOrders); 23 24 $this->set(compact('purchaseOrders')); 25 } 26 27 /** 28 * View method 29 * 30 * @param string|null $id Purchase Order id. 31 * @return \Cake\Http\Response|null|void Renders view 32 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 33 */ 34 public function view($id = null) 35 { 36 $purchaseOrder = $this->PurchaseOrders->get($id, [ 37 'contain' => [], 38 ]); 39 40 $this->set(compact('purchaseOrder')); 41 } 42 43 /** 44 * Add method 45 * 46 * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise. 47 */ 48 public function add() 49 { 50 $purchaseOrder = $this->PurchaseOrders->newEmptyEntity(); 51 if ($this->request->is('post')) { 52 $purchaseOrder = $this->PurchaseOrders->patchEntity($purchaseOrder, $this->request->getData()); 53 if ($this->PurchaseOrders->save($purchaseOrder)) { 54 $this->Flash->success(__('The purchase order has been saved.')); 55 56 return $this->redirect(['action' => 'index']); 57 } 58 $this->Flash->error(__('The purchase order could not be saved. Please, try again.')); 59 } 60 $this->set(compact('purchaseOrder')); 61 } 62 63 /** 64 * Edit method 65 * 66 * @param string|null $id Purchase Order id. 67 * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise. 68 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 69 */ 70 public function edit($id = null) 71 { 72 $purchaseOrder = $this->PurchaseOrders->get($id, [ 73 'contain' => [], 74 ]); 75 if ($this->request->is(['patch', 'post', 'put'])) { 76 $purchaseOrder = $this->PurchaseOrders->patchEntity($purchaseOrder, $this->request->getData()); 77 if ($this->PurchaseOrders->save($purchaseOrder)) { 78 $this->Flash->success(__('The purchase order has been saved.')); 79 80 return $this->redirect(['action' => 'index']); 81 } 82 $this->Flash->error(__('The purchase order could not be saved. Please, try again.')); 83 } 84 $this->set(compact('purchaseOrder')); 85 } 86 87 /** 88 * Delete method 89 * 90 * @param string|null $id Purchase Order id. 91 * @return \Cake\Http\Response|null|void Redirects to index. 92 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 93 */ 94 public function delete($id = null) 95 { 96 $this->request->allowMethod(['post', 'delete']); 97 $purchaseOrder = $this->PurchaseOrders->get($id); 98 if ($this->PurchaseOrders->delete($purchaseOrder)) { 99 $this->Flash->success(__('The purchase order has been deleted.')); 100 } else { 101 $this->Flash->error(__('The purchase order could not be deleted. Please, try again.')); 102 } 103 104 return $this->redirect(['action' => 'index']); 105 } 106}

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2022/04/05 14:11

エラーメッセージにある21行目付近のコードを、10行くらい前から質問文に追記していただくことはできますか?
haksen

2022/04/11 08:33

PurchaseOrdersControllerのソースPurchaseOrdersController.phpを追記しましたindex()メソッドのあたりになります。
guest

回答1

0

自己解決

http://localhost/test/purchase-orders/index で正しく表示されました

投稿2022/05/21 02:40

haksen

総合スコア23

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問