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

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

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

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

PHP

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

CakePHP

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

Q&A

解決済

1回答

3654閲覧

Record not found in table "users" with primary key [NULL]が出てログイン中のユーザープロフィール画面に遷移できない

Ryota.I

総合スコア22

MySQL

MySQL(マイエスキューエル)は、TCX DataKonsultAB社などが開発するRDBMS(リレーショナルデータベースの管理システム)です。世界で最も人気の高いシステムで、オープンソースで開発されています。MySQLデータベースサーバは、高速性と信頼性があり、Linux、UNIX、Windowsなどの複数のプラットフォームで動作することができます。

PHP

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

CakePHP

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

0グッド

0クリップ

投稿2019/09/15 04:16

お世話になります。
CakePHP、PHP、MySQLを使用したサイト構築の勉強中の身です。
現在、ログイン中の画面遷移の作りこみをしている最中ですが、
表題のタイトルのエラーの解消方法を公式ドキュメントを含め、
参考となりそうなページのコードを試しましたが改善できません。

解決したいこと:
【現在】現在ログイン中のユーザー名をクリック⇒エラー
【今後】現在ログイン中のユーザー名をクリック⇒ログイン中ユーザーのプロフィール画面へ遷移できるようにする(/my_pt_lesson/src/Template/Users/view.ctpの画面)

下記の画面図と、ソースコードを元に、
どのように手を加えれば解決するのか、
ご教示いただけると幸いです。

※現時点で足りると思われる内容を掲載しましたが、 
不足の点がありましたら、その部分も改めて掲載しますので、
ご指摘、よろしくお願いいたします。

▽現在制作中のトップ画面(ユーザーログイン後)▽
イメージ説明
▽ログイン中のユーザー名をクリックすると...▽
イメージ説明
▽このようなエラー表記がでます▽
イメージ説明
▽また、/my_pt_lesson/src/Template/Ices/index.ctpで構成されているリンク(チユーザー名)をクリックすると...▽
イメージ説明
▽/my_pt_lesson/src/Template/Users/view.ctpで構成されたページへ遷移します▽
イメージ説明

■現在のソースコードは以下の通りです。
※文字数制限の関係上、コメント等、不要な部分は削除してあります。

▽CakePHPのctpファイル▽
/my_pt_lesson/src/Template/Layout/my_layout.ctp(一部抜粋)

<!DOCTYPE html> <html> <head> <?= $this->Html->charset() ?> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> <?= $cakeDescription ?>: <?= $this->fetch('title') ?> </title> <?= $this->Html->meta('icon') ?> <?= $this->Html->css('base.css') ?> <?= $this->Html->css('style.css') ?> <?= $this->fetch('meta') ?> <?= $this->fetch('css') ?> <?= $this->fetch('script') ?> </head> <body> <h1>IceCream Mania</h1> <ul> <?php if ($this->request->getSession()->read('Auth.User')): ?> <li><?= $this->Html->link($this->request->getSession()->read('Auth.User.nickname') . "様", ['controller' => 'Users', 'action' => 'view']) ?></li> <li><?= $this->Html->link('アイスの一覧', ['controller' => 'ices', 'action' => 'index']) ?></li> <li><?= $this->Html->link('ログアウト', ['controller' => 'users', 'action' => 'logout']) ?></li> <?php else: ?> <!--<li><?= $this->Html->link('ログイン', ['controller' => 'users', 'action' => 'login']) ?></li>--> <?php endif ?> </ul> <?= $this->Flash->render() ?> <div class="container clearfix"> <?= $this->fetch('content') ?> </div> <footer> </footer> </body> </html>

/my_pt_lesson/src/Template/Ices/index.ctp(一部抜粋)

<?php <div class="ices index large-9 medium-8 columns content"> <h3><?= __('Ices') ?></h3> <table cellpadding="0" cellspacing="0"> <thead> <tr> <th scope="col"><?= $this->Paginator->sort('id') ?></th> <th scope="col"><?= $this->Paginator->sort('user_id') ?></th> <th scope="col"><?= $this->Paginator->sort('manufacturer') ?></th> <th scope="col"><?= $this->Paginator->sort('ice_name') ?></th> <th scope="col"><?= $this->Paginator->sort('ice_fraver') ?></th> <th scope="col"><?= $this->Paginator->sort('simple_comment') ?></th> <th scope="col"><?= $this->Paginator->sort('price_no_tax') ?></th> <th scope="col"><?= $this->Paginator->sort('buy_year') ?></th> <th scope="col"><?= $this->Paginator->sort('buy_month') ?></th> <th scope="col"><?= $this->Paginator->sort('image_file') ?></th> <th scope="col"><?= $this->Paginator->sort('created') ?></th> <th scope="col"><?= $this->Paginator->sort('modified') ?></th> <th scope="col" class="actions"><?= __('Actions') ?></th> </tr> </thead> <tbody> <?php foreach ($ices as $ices): ?> <tr> <td><?= $this->Number->format($ices->user->id) ?></td> <td><?= $ices->has('user') ? $this->Html->link($ices->user->nickname, ['controller' => 'Users', 'action' => 'view', $ices->user->id]) : '' ?></td> <td><?= h($ices->manufacturer) ?></td> <td><?= h($ices->ice_name) ?></td> <td><?= h($ices->ice_fraver) ?></td> <td><?= h($ices->simple_comment) ?></td> <td><?= $this->Number->format($ices->price_no_tax) ?></td> <td><?= $this->Number->format($ices->buy_year) ?></td> <td><?= $this->Number->format($ices->buy_month) ?></td> <td><?= h($ices->image_file) ?></td> <td><?= h($ices->created) ?></td> <td><?= h($ices->modified) ?></td> <td class="actions"> <?= $this->Html->link(__('View'), ['action' => 'view', $ices->id]) ?> <?= $this->Html->link(__('Edit'), ['action' => 'edit', $ices->id]) ?> <?= $this->Form->postLink(__('Delete'), ['action' => 'delete', $ices->id], ['confirm' => __('Are you sure you want to delete # {0}?', $ices->id)]) ?> </td> </tr> <?php endforeach; ?> </tbody> </table> <div class="paginator"> <ul class="pagination"> <?= $this->Paginator->first('<< ' . __('first')) ?> <?= $this->Paginator->prev('< ' . __('previous')) ?> <?= $this->Paginator->numbers() ?> <?= $this->Paginator->next(__('next') . ' >') ?> <?= $this->Paginator->last(__('last') . ' >>') ?> </ul> <p><?= $this->Paginator->counter(['format' => __('Page {{page}} of {{pages}}, showing {{current}} record(s) out of {{count}} total')]) ?></p> </div> </div>

/my_pt_lesson/src/Template/Users/view.ctp(一部抜粋)

<!DOCTYPE html> <html> <head> <?= $this->Html->charset() ?> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> <?= $cakeDescription ?>: <?= $this->fetch('title') ?> </title> <?= $this->Html->meta('icon') ?> <?= $this->Html->css('base.css') ?> <?= $this->Html->css('style.css') ?> <?= $this->fetch('meta') ?> <?= $this->fetch('css') ?> <?= $this->fetch('script') ?> </head> <body> <nav class="large-3 medium-4 columns" id="actions-sidebar"> <ul class="side-nav"> <li class="heading"><?= __('Actions') ?></li> <li><?= $this->Html->link(__('Edit User'), ['action' => 'edit', $user->id]) ?> </li> <li><?= $this->Form->postLink(__('Delete User'), ['action' => 'delete', $user->id], ['confirm' => __('Are you sure you want to delete # {0}?', $user->id)]) ?> </li> <li><?= $this->Html->link(__('List Users'), ['action' => 'index']) ?> </li> <li><?= $this->Html->link(__('New User'), ['action' => 'add']) ?> </li> <li><?= $this->Html->link(__('List Ices'), ['controller' => 'Ices', 'action' => 'index']) ?> </li> <li><?= $this->Html->link(__('New Ice'), ['controller' => 'Ices', 'action' => 'add']) ?> </li> </ul> </nav> <div class="users view large-9 medium-8 columns content"> <h2><?= h($user->nickname."さんのプロフィール") ?></h2> <table class="vertical-table"> <tr> <th scope="row"><?= __('Profiel Comment') ?></th> <td><?= h($user->profiel_comment) ?></td> </tr> <tr> <th scope="row"><?= __('Created') ?></th> <td><?= h($user->created) ?></td> </tr> <tr> <th scope="row"><?= __('Modified') ?></th> <td><?= h($user->modified) ?></td> </tr> </table> <div class="related"> <h4><?= __('Related Ices') ?></h4> <?php if (!empty($user->ices)): ?> <table cellpadding="0" cellspacing="0"> <tr> <th scope="col"><?= __('メーカー') ?></th> <th scope="col"><?= __('アイスの名前') ?></th> <th scope="col"><?= __('フレーバー') ?></th> <th scope="col"><?= __('価格(税抜)') ?></th> <th scope="col"><?= __('購入年') ?></th> <th scope="col"><?= __('購入月') ?></th> <th scope="col"><?= __('アイスの画像') ?></th> <th scope="col"><?= __('登録日') ?></th> <th scope="col"><?= __('更新日') ?></th> <th scope="col" class="actions"><?= __('Actions') ?></th> </tr> <?php foreach ($user->ices as $ices): ?> <tr> <td><?= h($ices->manufacturer) ?></td> <td><?= h($ices->ice_name) ?></td> <td><?= h($ices->ice_fraver) ?></td> <td><?= h($ices->price_no_tax) ?></td> <td><?= h($ices->buy_year) ?></td> <td><?= h($ices->buy_month) ?></td> <td><?= h($ices->image_file) ?></td> <td><?= h($ices->created) ?></td> <td><?= h($ices->modified) ?></td> <td class="actions"> <?= $this->Html->link(__('詳細'), ['controller' => 'Ices', 'action' => 'view', $ices->id]) ?> <?= $this->Html->link(__('編集'), ['controller' => 'Ices', 'action' => 'edit', $ices->id]) ?> <?= $this->Form->postLink(__('削除'), ['controller' => 'Ices', 'action' => 'delete', $ices->id], ['confirm' => __('Are you sure you want to delete # {0}?', $ices->id)]) ?> </td> </tr> <?php endforeach; ?> </table> <?php endif; ?> </div> </div>

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

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

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

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

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

guest

回答1

0

ベストアンサー

HTMLヘルパーの配列にidがないからですね。

うまくいっていないほう
layout.php

php

1$this->Html->link($this->request->getSession()->read('Auth.User.nickname') . "様", 2['controller' => 'Users', 'action' => 'view'])

うまくいっているほう
index.ctp

php

1$this->Html->link($ices->user->nickname, 2['controller' => 'Users', 'action' => 'view', $ices->user->id])

投稿2019/09/15 06:43

編集2019/09/15 06:44
millionx

総合スコア50

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

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

Ryota.I

2019/09/15 07:09

millionx様 ありがとうございます。早速試してみたのですが、 /my_pt_lesson/src/Template/Layout/my_layout.ctp に対し、 ['controller' => 'Users', 'action' => 'view', $ices->user->id]) を追記してみましたが、top画面に Notice (8): Undefined variable: ices [APP/Template/Layout/my_layout.ctp, line 46] Notice (8): Trying to get property 'user' of non-object [APP/Template/Layout/my_layout.ctp, line 46] Notice (8): Trying to get property 'id' of non-object [APP/Template/Layout/my_layout.ctp, line 46] 上記3行のエラーが出てしまうようになり、 また、ログイン中のユーザー名をクリックすると、 Record not found in table "users" with primary key [NULL] と、掲載したエラー表示はまだで続ける状況です。 ※自分では、共通のページとして使用しているmy_layoutは無効にして、各ページごとでやりようはないか、確認中ではありますが、他に思いつく解決策があれば、お伺いできると嬉しいです。
millionx

2019/09/15 07:12

IDはなんでもいいわけではなく、ログインユーザーのIDを渡さないといけないです。
Ryota.I

2019/09/15 07:51

['controller' => 'Users', 'action' => 'view', $this->request->getSession()->read('Auth.User.id')])?></li> と、/my_pt_lesson/src/Template/Layout/my_layout.ctp の該当箇所にいれ、見事解決しました。 非常に悩んでいた箇所であったため、助かりました。 また、機会がありましたら、よろしくお願いいたします。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問