[英]How to save data on a different table using CakePHP?
我有3个表,分别命名为places
, images
和place_images
。 我想将表images
和places
的ID保存到表place_images
。 我正在使用CakePHP作为我的框架。
我是PHP编程和Cake框架的新手。
这是我保存图像的代码:
for($img = 0; $img < count($this->request->data['Place']['img']); $img++){
$file = $this->request->data['Place']['img'];
if ($file[$img]['tmp_name']) {
if ($file[$img]['error'] === UPLOAD_ERR_OK) {
$new_file = $s3place->upload($file[$img]['tmp_name']);
$image_data = array(
'filename' => $new_file,
'metadata' => '',
'title' => $this->request->data['Place']['img_title'][$img],
'description' => $this->request->data['Place']['img_desc'][$img],
'thumb_ready' => 1
);
//pr($image_data). exit;
if (UserPerm::places_attribute()) {
$image_data['source_url'] = $this->request->data['Place']['img_source'][$img];
$image_data['source_name'] = $this->request->data['Place']['img_source_name'][$img];
}
App::uses('Image', 'model');
$Image = new Image();
$is_new_file = false;
if (isset($old_data['Image']['id']) && $old_data['Image']['id']) {
$image_data['id'] = $old_data['Image']['id'];
$Image->id = $old_data['Image']['id'];
} else {
$Image->create();
$is_new_file = true;
}
if ($Image->save($image_data)) {
if ($is_new_file) {
$this->request->data['Place']['image_id'] = $Image->getLastInsertID();
} else {
$this->request->data['Place']['image_id'] = $old_data['Image']['id'];
}
// Delete old image in S3
/*if (isset($old_data['Image']['id']) && $old_data['Image']['id']) {
$s3place->del($old_data['Image']['filename']);
} */
} else {
$this->Session->setFlash(__('The image could not be saved. Please, try again.') . $err, 'flash_error');
return;
}
} else {
$this->Session->setFlash(__('The image could not be uploaded. Please, try again.') . $err, 'flash_error');
return;
}
}} // end for loop
在[关联:将模型链接在一起]下的CakePHP书中对此进行了详细介绍-特别是[具有并属于许多人]部分
正确建立关联后,并且按照本书的说明进行保存,它将自动保存所有字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.