如何使用CakePHP将数据保存在不同的表上?
我有三个表,分别名为如何使用CakePHP将数据保存在不同的表上?,php,mysql,database,cakephp,Php,Mysql,Database,Cakephp,我有三个表,分别名为places、images和place\u images。我想拯救世界 表images和places到表place\u images的ID。我正在使用 CakePHP作为我的框架 我是新的PHP编程和蛋糕框架 以下是我保存图像的代码: for($img = 0; $img < count($this->request->data['Place']['img']); $img++){ $file = $this->request->data
places
、images
和place\u images
。我想拯救世界
表images
和places
到表place\u images
的ID。我正在使用
CakePHP作为我的框架
我是新的PHP编程和蛋糕框架
以下是我保存图像的代码:
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
for($img=0;$imgrequest->data['Place']['img']);$img++){
$file=$this->request->data['Place']['img'];
如果($file[$img]['tmp_name'])){
如果($file[$img]['error']==UPLOAD\u ERR\u OK){
$new_file=$s3place->upload($file[$img]['tmp_name']);
$image\u data=数组(
'filename'=>$new\u文件,
'元数据'=>'',
'title'=>$this->request->data['Place']['img_title'][$img],
'description'=>$this->request->data['Place']['img_desc'][$img],
“拇指准备就绪”=>1
);
//pr($image_data)。退出;
if(UserPerm::places\u attribute()){
$image\u data['source\u url']=$this->request->data['Place']['img\u source'][$img];
$image\u data['source\u name']=$this->request->data['Place']['img\u source\u name'][$img];
}
应用程序::使用('Image','model');
$Image=新图像();
$is_new_file=false;
如果(isset($old_data['Image']['id'])&&&$old_data['Image']['id'])){
$image_data['id']=$old_data['image']['id'];
$Image->id=$old_data['Image']['id'];
}否则{
$Image->create();
$is_new_file=true;
}
如果($Image->save($Image\u data)){
如果($是新文件){
$this->request->data['Place']['image_id']=$image->getLastInsertID();
}否则{
$this->request->data['Place']['image\u id']=$old\u data['image']['id'];
}
//删除S3中的旧图像
/*如果(isset($old_data['Image']['id'])&&&$old_data['Image']['id'])){
$s3place->del($old_data['Image']['filename']);
} */
}否则{
$this->Session->setFlash(uuu('图像无法保存。请重试')。$err,'flash_error');
返回;
}
}否则{
$this->Session->setFlash(uuu('无法上载图像。请重试')。$err,'flash_error');
返回;
}
}}//循环结束
下面详细介绍了这一点,特别是
一旦您的关联设置正确,并且按照本书的说明进行保存,它将自动保存所有字段。感谢您的快速响应。我会看看你的建议。您好,先生,我已经按照[Associations:Linking Models Together]中的说明进行了操作,但我仍然无法将数据保存到我的表中。我遗漏了什么吗?很可能是的。您描述的内容是常见的,并有文档记录。尝试正常的测试/修复-即让它简单地工作,然后扩展。谢谢Dave。我通过使用递归函数使它工作。干杯