현재, 나는 2 개의 데이터베이스를 가지고 있습니다. 이미지 & 사용자. 내 사용자 모델에서cakephp에 프로필 이미지 추가
내가 가진 : 내 이미지 모델에서
var $belongsTo=array(
'Image'=>array('className'=>'Image')
);
나는이 :
var $hasOne =array(
'Users'=>array('className'=>'User')
);
내가 아래에 다음과 달성하고자하는 : 내 사용자 데이터베이스 필드 'image_id'을 가지고 내 이미지 데이터베이스 필드 'id'에 연결됩니다. 예 : 이미지 데이터베이스 ID는 = 1 사용자 데이터베이스 image_id 아래 = 1
내 image_controller의 추가 기능에 대한 코드입니다. 특정 이미지를 추가하고 이미지 'id'를 사용자 데이터베이스 'image_id'에 저장하겠습니다. 이것을 어떻게 성취합니까?
function add() {
$this->layout = "mainLayout";
if (!empty($this->data)) {
$this->Image->create();
if ($this->Image->save($this->data)) {
$user_id = $this->User->getUserInfo($this->Auth->user('id'));
$this->data['User']['image_id'] = $user_id;
$this->Session->setFlash('The image has been saved', true);
$this->redirect(array('controller' => 'Users', 'action' => 'profile'));
} else {
$this->Session->setFlash('The image could not be saved. Please, try again.', true);
}
}
$current_user_pic =$this->User->getUserInfo($this->Auth->user('id'));
$this->set(compact('current_user_pic'));
}
당신의 관계 (hasOne의 및 belongsTo를이) 맞다면
$this->User->Image->save($this->data)
전에$this->data['Image']['user_id'] = $this->Auth->user('id')
을 설정 한 후 모든 것을 당신이 대신 '저장' '시려면 그냥 사용됩니다 필요 당신이 (나를 이해 무슨 희망 : – sukinsan구현 방법을 잘 모르겠습니다. 관계가 올바르게 작동합니다. – user448402