簡體   English   中英

如何使用CakePHP將數據保存在其他表上?

[英]How to save data on a different table using CakePHP?

我有3個表,分別命名為placesimagesplace_images 我想將表imagesplaces的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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM