【问题标题】:How to save data on a different table using CakePHP?如何使用 CakePHP 将数据保存在不同的表上?
【发布时间】:2013-09-10 12:54:39
【问题描述】:

我有 3 个名为 placesimagesplace_images 的表。我想保存 表 imagesplaces 到表 place_images 的 ID。我在用 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

【问题讨论】:

    标签: php mysql database cakephp


    【解决方案1】:

    这在[Associations: Linking Models Together] 下的CakePHP book 中有详细介绍 - 特别是[Has and Belongs To Many] 上的部分

    一旦您的关联设置正确,并且您按照书中的说明进行保存,它将自动保存所有字段。

    【讨论】:

    • 感谢您的快速回复。我会看看你的建议。
    • 您好,先生,我已按照 [Associations: Linking Models together] 中的说明进行操作,但我仍然无法将数据保存到我的表中。我错过了什么吗?
    • 很可能是的。您所描述的内容很常见并且有记录。尝试正常的测试/修复 - 即让它简单地工作,然后扩展。
    • 感谢戴夫。我设法通过使用递归使其工作。干杯。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 2019-10-09
    • 2015-08-25
    相关资源
    最近更新 更多