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

  • SEARCH
  • Home
  • 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 8114253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:05:48+00:00 2026-06-06T03:05:48+00:00

I normally save new data to the database like this: $this->MyTable->set(array( ‘id’ => $id,

  • 0

I normally save new data to the database like this:

$this->MyTable->set(array(
 'id' => $id,
 'code' => $temp_code,
 'status' => $status,
 'age' => $age,
 'location' => $location,
 'money' => $money
));

$this->MyTable->save();

If the ID already exists in the database I update its corresponding fields like this:

$this->Adtweet->id = $id;
$this->Adtweet->saveField('code', $temp_code);
$this->Adtweet->saveField('status', $status);

Is there a better or ‘proper’ way to do this?

When I attempt to enter an ID that already exists and I use the set function, I get the following SQL integrity error:

(Error: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry ‘150245925’ for key ‘PRIMARY’)

How can I write a function that cleanly handles duplicate entries without returning an error?

  • 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-06-06T03:05:50+00:00Added an answer on June 6, 2026 at 3:05 am

    If you want to save new data, just use Model::save():

    $data = array(
        'ModelName' => array(
            'foo' => $foo
        )
    )
    
    // prepare the model for adding a new entry
    $this->ModelName->create();
    
    // save the data
    $this->ModelName->save($data);
    

    If you want to update your data just use the same method without calling Model::create()

    $data = array(
        'ModelName' => array(
            'id' => $id
            'foo' => $foo
        )
    )
    
    $this->ModelName->save($data);
    

    See also: http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-save-array-data-null-boolean-validate-true-array-fieldlist-array

    Edit:

    I guess this is what you’re looking for:

    $this->ModelName->id = $id;
    if (!$this->ModelName->exists()) {
        $this->ModelName->create();
    }
    
    $this->ModelName->save($data);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying something new, I would normally do this in C# or VB. But
I'm trying to save some data into an array but unfortunately all the data
I am using the below code to export data to csv format. Normally its
Normally, when tabbing through controls on a form, when the focus is set to
Normally with Java Swing you can set the background color of a button with:
Normally my URLs look like the standard: www.example.com/controller/action Now I want to setup my
another newbie question, Right after I save an item to database, I tried to
Normally, if I want to force a link to open in a new tab
I'm new to development and I'm sure this is a silly question but i
I'm new to entity frame work code first. I have simple class called Cat

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.