cakeのアソシエーションを勉強し始めましたが早速つまづきましたので
ヘルプ求めます!><
以下のようなエラーがでます。(画像も添付します)
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'User.email' in 'field list'
食べログのようなものを練習で作ってます。
各お店に複数のユーザーがレビューを投稿できるようにする機能です。
usersテーブルとreviewsテーブルがあり、これらをアソシエーションで連結させたいです。
(foreignKeyはuser_idです)
usersテーブルにはemail.id共に存在するのですが、
なぜか上記のようなエラーが表示されてしまいます。
少し調べると下記のような記事を発見しました。
http://fr-soft.com/cake/?p=32
これと同じような事象でしょうか?
(cakeではjoinが実行されない?)
実コードは以下に記載いたします。
getListByShopIdが各ショップごとのレビューを表示させる
メソッドになります。
<Review.php>
php
1<?php 2 3class Review extends AppModel { 4 public $belongTo = array( 5 'User' => array( 6 'className' => 'User', 7 ), 8 'Shop' => array( 9 'className' => 'Shop' 10 ) 11 ); 12 13 public function isReview ($shopId, $userId) { 14 $review = $this->getData($shopId, $userId); 15 return !empty($review) ? true : false; 16 } 17 18 public function getData($shopId, $userId) { 19 $options = array( 20 'conditions' => array( 21 'shop_id' => $shopId, 22 'user_id' => $userId 23 ) 24 ); 25 return $this->find('first', $options); 26 } 27 28 public function getReviewCnt($userId) { 29 $options = array( 30 'condition' => array( 31 'user_id' => $userId 32 ) 33 ); 34 35 return $this->find('count', $options); 36 } 37 38 public function getListByShopId($shopId) { 39 $options = array( 40 'fields' => 41 array('Review.id', 'Review.user_id', 'Review.title', 'Review.body', 'Review.score', 'Review.created', 'User.email', 'User.id'), 42 'conditions' => array('Review.shop_id' => $shopId), 43 'recursive' => 2 44 ); 45 return $this->find('all', $options); 46 } 47 48 public function getScoreAvg($shopId) { 49 $options = array( 50 'fields' => 'AVG(score) as avg', 51 'conditions' => array('shop_id' => $shopId), 52 'group' => array('shop_id') 53 ); 54 55 $data = $this->find('first', $options); 56 $score = $scoreAve = 0; 57 if (!empty($data[0]['avg'])) { 58 $score = round($data[0]['avg']); 59 $scoreAve = round($data[0]['avg'], 1); 60 } 61 return array($score, $scoreAve); 62 } 63} 64 65?>
<User.php>
php
1<?php 2 3class User extends AppModel { 4 5 public $hasMany = array( 6 'Review' => array( 7 'className' => 'Review' 8 ) 9 ); 10 11 public $validate = array( 12 'email' => array( 13 'validEmail' => array( 14 'rule' => array('email'), 15 'message' => 'メールアドレスを入力してください' 16 ), 17 18 'emailExists' => array( 19 'rule' => array('isUnique', array('email')), 20 'message' => '既に登録済みです' 21 ) 22 ), 23 24 'password' => array( 25 'match' => array( 26 'rule' => array( 27 'confPassword', 'passwordconf' // confPassword(関数名)の呼び出し 28 ), 29 'message' => 'パスワードが一致しません' 30 ) 31 ), 32 33 'passwordold' => array( 34 'match' => array('rule' => array('oldPassword', 'passwordold'), 35 'message' => '旧パスワードが一致しません' 36 ) 37 ) 38 ); 39 40 public function confPassword($field,$colum) { // $columはどこから出てきたのか 41 if ($field['password'] === $this->data['User'][$colum]) { // $field['password'] = password, $this->data['User'][$colum] = passwordconf 42 $this->data['User']['password'] = Authcomponent::password($field['password']); 43 return true; 44 } 45 } 46 47 public function oldPassword($field, $colum) { // $field = usersテーブルのpassword, $colum = passwordold 48 $passwordold = Authcomponent::password($field[$colum]); // $passwordoldの暗号化 49 $row = $this->findById($this->data['User']['id']);// usersテーブルのidを$rowに格納 50 51 if ($passwordold === $row['User']['password']) { // $passwordoldとusersテーブルのpasswordを照合 52 return true; 53 } 54 } 55} 56 57?>
<Shop.php>
php
1<?php 2 3class Shop extends AppModel { 4 public $validate = array( 5 'name' => array( 6 'rule' => array('notBlank') 7 ), 8 9 'tel' => array( 10 'rule' => array('notBlank') 11 ), 12 13 'addr' => array( 14 'rule' => array('notBlank') 15 ), 16 17 'url' => array( 18 'rule' => array('url'), 19 'message' => '形式が正しくありません' 20 ) 21 ); 22 23 public $hasMany = array( 24 'Review' => array( 25 'className' => 'Review', 26 'order' => 'Review.created DESC' 27 ) 28 ); 29} 30 31?>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/01/05 05:58