twitterのクローンサイトを作ってます。
①新規ツイートでログイン中のユーザー情報を上記のtweetsテーブルに格納したいのですが、
「user_id」「acountname」「username」うまく反映されません。。。
【tweetsテーブル】
create table tweets
(
id
int(11) not null,
user_id
int(11) not null,
acountname
varchar(255) not null,
username
varchar(255) not null,
content
varchar(140) not null,
created
datetime not null,
updated
datetime not null
) ENGINE=InnoDB;
コードは下記に記載いたします。
どこか間違ってますか?
TweetsController
php
1public function add() {// 新規ツイート 2if ($this->request->is('post')) { 3$request = $this->request->data['Post']; // $requestにそのデータを格納 4$user = $this->Auth->user(); 5 6$this->Tweet->set($this->data); //postされたら値をセット 7 8if ($this->Tweet->validates()) { 9 10$data = array( //データベースに保存するカラムと値を格納 11'user_id' => $user['id'], 12'acountname' => $user['acountname'], 13'username' => $user['username'], 14'content' => $request['content'] 15); 16 17$this->Tweet->save($this->data); //tweetsテーブルに値を格納 18$this->redirect('index'); 19} 20} 21}
②ツイート編集機能を実装してますが、
Notice (8): Undefined index: Post [APP/Controller/TweetsController.php, line 53]の
エラーが出ます。
こちらも原因が分からず、困ってますのでどなたかお助けください。
TweetsController
php
1public function edit ($id = null) 2 { 3 if ($this->request->is('post')) 4 { 5 $request = $this->request->data['Post']; 6 $data = array( 7 'content' => $request['content'] 8 ); 9 $this->Tweet->save($data); 10 $this->redirect('index'); 11 } 12 else 13 { 14 $data = $this->Tweet->findById($id); 15 } 16 $this->set('data', $data['Post']); 17 } 18 19 public function delete($id) { 20 $this->Tweet->delete($id); // idと合致するもののデータを削除する 21 $this->redirect('index'); // indexにリダイレクトさせる 22 }
edit.ctp
php
1<h2>ツイートの編集</h2> 2<?=$this->Form->create('Tweet', array('action' => 'edit')); ?> 3<?=$this->Form->input('content', array('label' => 'ツイート', 'value' => $data['content'])); ?> 4<?=$this->Form->hidden('id', array('value' => $user['id'])); ?> 5<?=$this->Form->end('編集する'); ?>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。