0

I have 3 tables named places, images and place_images. I want to save the IDs of table images and places to the table place_images. I am using CakePHP as my framework.

I am new to PHP programing and Cake framework.

Here is my code which saves the images:

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

1 Answer 1

1

This is covered in detail in the CakePHP book under [Associations: Linking Models Together] - specifically the part on [Has and Belongs To Many]

Once your associations are set up correctly, and you're saving per the book's instructions, it will save all the fields automatically.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for the quick response. I will take a look at your advice.
Hi Sir, I have followed instruction in [Associations: Linking Models Together] - and I still cant save data to my table. am I missing something?
Most likely yes. What you described is common and documented. Try normal testing/fixing - ie get it to work simply, then expand.
thanks Dave. I managed to make it work by using a recursive. cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.