I started studying cake association, but I stumbled right away, so
Ask for help!><
The following error occurs:(I will also attach an image.)
Error: SQLSTATE [42S22]: Column not found: 1054 Unknown column 'User.email 'in' field list'
I'm practicing making a food log.
This feature allows multiple users to post reviews to each store.
There is a users table and a reviews table, and I would like to associate them with an association.
(foreignKey is user_id)
Both email.id exist in the users table, but
For some reason, an error similar to the above appears.
After a little research, I found the following article.
http://fr-soft.com/cake/?p=32
Is it similar to this?
(Cake does not join?)
The actual code is listed below.
GetListByShopId displays reviews for each shop
It will be a method.
class Review extensions AppModel {
public$belongTo=array(
'User' = > array(
'className' = > 'User',
),
'Shop' = > array(
'className' = > 'Shop'
)
);
public function isReview($shopId,$userId){
$review=$this->getData($shopId,$userId);
return!empty($review)?true:false;
}
public function getData($shopId,$userId){
$options=array(
'conditions' = > array(
'shop_id' = > $shopId,
'user_id' = > $userId
)
);
return$this->find('first',$options);
}
public function getReviewCnt($userId){
$options=array(
US>'condition' = > array(
'user_id' = > $userId
)
);
return$this->find('count',$options);
}
public function getListByShopId($shopId){
$options=array(
'fields' = >
array('Review.id', 'Review.user_id', 'Review.title', 'Review.body', 'Review.score', 'Review.created', 'User.email', 'User.id'),
'conditions' = > array('Review.shop_id' = > $shopId),
'recursive' = > 2
);
return$this->find('all',$options);
}
public function getScoreAvg($shopId){
$options=array(
'fields' = > 'AVG(score) as avg',
'conditions' = > array('shop_id' = > $shopId),
'group' = > array('shop_id')
);
$data=$this->find('first',$options);
$score=$scoreAve=0;
if(!empty($data[0]['avg'])){
$score=round($data[0]['avg']);
$scoreAve=round($data[0]['avg'],1);
}
return array($score,$scoreAve);
}
}
?>
<?php
class User extensions AppModel {
public$hasMany=array(
'Review' = > array(
'className' = > 'Review'
)
);
public$validate=array(
'email' = > array(
'validEmail' = > array(
'rule' = > array('email',
'message' = > 'Please enter your email address'
),
'emailExists' = > array(
'rule' = > array('isUnique', array('email')),
'message' = > 'Already registered'
)
),
US>'password'=>array(
US>'match'=>array(
US>'rule'=>array(
Call 'confPassword', 'passwordconf'//confPassword (function name)
),
'message' = > 'Passwords do not match'
)
),
'passwordold' = > array(
'match' = > array('rule' = > array('oldPassword', 'passwordold'),
'message' = > 'Old passwords do not match'
)
)
);
Where did public function confPassword($field,$colum){//$colum come from?
if($field['password']===$this->data['User'][$colum]){//$field['password']=password,$this->data['User'][$colum]=passwordconf
$this->data['User']['password'] = Authcomponent::password($field['password']);
return true;
}
}
public function oldPassword($field,$colum) {//$field=users table password,$colum=passwordold
$passwordold = Authcomponent::password($field[$colum]); // $passwordold encryption
$row = $this->findById($this->data['User']['id']); // The id of the users table is stored in $row
if($passwordold===$row['User']['password']) {//$passwordold matches the password in the users table
return true;
}
}
}
?>
<?php
class Shop extensions AppModel {
public$validate=array(
'name' = > array(
US>'rule'=>array('notBlank')
),
US>'tel'=>array(
US>'rule'=>array('notBlank')
),
'addr' = > array(
US>'rule'=>array('notBlank')
),
US>'url'=>array(
'rule' = > array('url'),
'message' = > 'Incorrect format'
)
);
public$hasMany=array(
'Review' = > array(
'className' = > 'Review',
'order' = > 'Review.created DESC'
)
);
}
?>
Review.php says the following, but
public$belongTo=array(...);
Isn't it $belong "s" To?
© 2024 OneMinuteCode. All rights reserved.