我目前有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