Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6611759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:00:43+00:00 2026-05-25T20:00:43+00:00

I have an image upload that adds the filename to a table called attachments.

  • 0

I have an image upload that adds the filename to a table called attachments. If the id already exists then I want it to update and if not then create a new record. At the moment it creates a new record so I have multiple records forthe one id. The id’s are from a table called Addon’s.

I am not sure how to do this in cakephp.

    if (!empty($this->data)) {
        $this->layout = null;
        //if(empty($this->data['AddOn']['id'])){unset($this->data['AddOn']);}

        // restructure data for uploader plugin // NEED TO GET RID OF THIS ? MOVE IT
        $tmp_file = $this->data['Attachment'][0]['file'];
        $tmp_file['extension'] =  array_reverse(explode('.', $tmp_file['name']));
        $tmp_file['extension'] = $tmp_file['extension'][0];
        $tmp_file['title'] = strtolower(substr($tmp_file['name'],0,(0-strlen('.'.$tmp_file['extension']))));
        $this->data['Attachment'][0]['alternative'] = ucwords(str_replace('_',' ', $tmp_file['title']));

        $previous = $this->AddOn->Attachment->find('first', array('conditions'=> array('model'=>'AddOn', 'foreign_key'=>$id)));
        if( !empty( $previous ) ) {
            $this->AddOn->Attachment->id = $previous[ 'Attachment' ][ 'id' ];
        }

        if ($this->AddOn->save($this->data, array('validate' => 'first'))) {    

            $id = $this->AddOn->Attachment->getLastInsertID();

            $att = $this->AddOn->Attachment->query("SELECT * from attachments WHERE id = ".$id);
            $this->set('attachment',$att[0]['attachments']);

        } else {
            $tmp_file['name'] = 'INVALID FILE TYPE';
        }


        //debug($this->data);
        $this->set('file', $tmp_file);
        $this->RequestHandler->renderAs($this, 'ajax');
        $this->render('../elements/ajax');

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:00:43+00:00Added an answer on May 25, 2026 at 8:00 pm

    save() and saveAll() automatically update an existing row if the id has been set. You can do something like:

    $previous = $this->AddOn->Attachment->find( /* whatever conditions you need */ );
    if( !empty( $previous ) ) {
        $this->AddOn->Attachment->id = $previous[ 'Attachment' ][ 'id' ];
    }
    

    Now the old record will be updated if it exists.

    As a side note, the code after a successful saveAll() doesn’t make much sense: first you’re saving data to the database, then immediately retrieving it again. You can just keep using $this->data that already has the same content.

    And another side note: you should use query() only as a last resort when you can’t use Cake’s other methods. query("SELECT * from attachments WHERE id = ".$id) is a trivial case that can be rewritten as $this->Model->id = $id; $this->Model->read(); or using a simple $this->Model->find() query.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 cucumber scenarios that simulate paperclip image upload. I want to remove
I have an image upload script that batch uploads loads of images. I want
I have an application that uses the Paperclip plugin for image upload. Now that
Let's say i have an image uploader script, i want to prevent the upload
How to do the following: I want to have a link [upload image] which
I have a small python cgi script that accepts an image upload from the
I want to have image button for browse and upload I have textbox using
I have an image upload script, and the thumbnails that its producing are posterized
I have a web application that I upload an image to, the image is
I want to partially update page when i upload or delete the image. What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.