我目前有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 add函數的代碼。我想添加一個特定的圖像,我希望圖像的'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和屬於關聯)是正確的之前設置
$this->data['Image']['user_id'] = $this->Auth->user('id')
那麼一切你所需要的只是用「白水」,而不是「保存」,希望你能理解我(: – sukinsan我不知道如何實現它,關係正常工作 – user448402