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 8577947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:16:31+00:00 2026-06-11T20:16:31+00:00

This is essentially the same question as CakePHP: Is it possible to insert record

  • 0

This is essentially the same question as CakePHP: Is it possible to insert record with pre-defined primary key value?. In CakePHP 1.2 I want to insert a record with a pre-determined id. The id’s value is determined by an external system, but unlike in the linked question I have no control over that system, so the solution to that question is not an option for me.

If the id field of a model is set when calling $model->save(), Cake will always try to update the record. Cake will even set the id to false, if a record with that id does not exist already [1]:

<?php
function save($data = null, $validate = true, $fieldList = array()) {
    // snip
    if (!$this->__exists && $count > 0) {
        $this->id = false;
    }
    // snip
}

Saving the model first and then updating the id manually is also not an option, because this would break referencial integrity (this is an architectural limitation that I also have no control over).

So how can I force CakePHP to insert the record with a specific primary key?

Some background around the problems I’m facing:

The application I’m working on is backed by a Postgres database, so new primary keys are determined by sequences. There is a process where an external system will SELECT nextval('my_model_sequence'), do some work with that id and then pass it to the CakePHP application, where I want to INSERT a my_model record with that id.

There is no way for me to invoke the external process after saving the record. There is also no way for me to modify the external process at all.


[1] http://api12.cakephp.org/view_source/model/#l-1260

  • 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-11T20:16:32+00:00Added an answer on June 11, 2026 at 8:16 pm

    If you want to create a record with specific id just make sure you fill id field

       $this->data['ModelName']['id'] = $your_id;
    

    Also before calling:

        $this->save($this->data);
    

    you should remember to call

    $this->create();
    

    which will reset the model state which let you create new record.
    Finally you should try:

       $this->ModelName->create(); 
       $this->ModelName->save($this->data);
    

    On the other hand as I read your post and trying to imagine your tables structure I recommend you thinking over leaving the id field alone and trying to make this foreing id a Foreign key. Is that an option for you?

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

Sidebar

Related Questions

My question is essentially the same as this one: Polymorphic Association with multiple associations
I essentially have the same goal as in this question: Calling a jQuery plugin
My question is essentially the same as this one: Add a date picker to
My question is essentially the same as this one here . I'm using AnkhSVN
I have looked at this question which is essentially the same but the OP
This is essentially the same question as posted here: Animating a custom property in
This questions involves 2 different implementations of essentially the same code. First, using delegate
This question has been bugging me for a long time now but essentially I'm
This seems like a basic question, but I haven't found any clear answers. Essentially,
This question comes from a range of other questions that all deal with essentially

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.