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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:37:40+00:00 2026-06-12T04:37:40+00:00

I have the following code in CakePHP 2: $this->Order->id = 5; $this->Order->saveAll(array( ‘Order’ =>

  • 0

I have the following code in CakePHP 2:

$this->Order->id = 5;
$this->Order->saveAll(array(
    'Order' => array(
        'person_id' => $this->Session->read('Person.id'),
        'amount' => $total,
        'currency_id' => $code                   
    ),
    'Lineitem' => $lineitems  /* a correctly-formatted array */
));

I would expect this to update the row with the Primary Key of 5 in the Order table and then insert the Lineitem rows with an order_id of 5.

However, all it does is create a new row in Order and then use the new id from the new Order record to create the Listitem rows.

Note: I’m only setting the ID as above for debugging purposes and to easily demonstrate this question. In my final code, I’ll be checking to see if there’s already a pending order with the current person_id and doing $this->Order->id = $var; if there is and $this->Order->create(); if there isn’t.

In other words, sometimes I will want it to INSERT (in which case I will issue $this->Order->create(); ) and sometimes I will want it to UPDATE (in which case I will issue $this->Order->id = $var; ). The test case above should produce an UPDATE but it’s producing an INSERT instead.

Any idea what I am doing wrong here?

  • 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-12T04:37:41+00:00Added an answer on June 12, 2026 at 4:37 am

    The array you pass to Model->saveAll() doesnt’t contain the order’s id, so Cake creates a new one. If you wanto to update an existing record, either you set the order id in the passed array, or you retrieve it with a find. The documentation explicitly remarks

    If you want to update a value, rather than create a new one, make sure
    your are passing the primary key field into the data array

    $order = $this->Order->findById(5);
    // ... modify $order if needed
    $this->Order->saveAll(array('Order' => $order, 'LineItem' => $items));
    

    In your case, you may want to use something like the following to be as concise as possible. Model::saveAssociated() is smart enough to create or update depending on the id, but you must provide suitable input. Model::read($fields, $id) initializes the internal $data: for an existing record all fields will be read from the database, but for a nonexistent id, you’ll need to supply the correct data for it to succeed. Assuming an order belongsTo a customer, I supply the customer id if the order doesn’t exist

    // set the internal Model::$data['Order']
    $this->Order->read(null, 5);
    // You may want to supply needed information to create
    // a new order if it doesn't exist, like the customer
    if (! $this->Order->exists()) {
        $this->Order->set(array("Customer" => array("id" => $customer_id)));
    }
    $this->Order->set(array('LineItem' => $items));
    $this->Order->saveAssociated();
    

    As a final note, it seems you are implementing a shopping cart. If that’s the case, maybe it’d be clearer to use a separate ShoppingCart instead of an Order with a finalized flag.

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

Sidebar

Related Questions

I have the following code in a CakePHP Controller: var $searchCondition = array( 'Item.date
In my cakephp form I have following code <p> <?php echo $form->input('option[]',array('size'=>13)); ?> </p>
I have an issue with using the following code in my CakePHP app. In
I have CakePHP Controller code which is throwing up the following error 'Fatal error:
I have the following code in the controller: function add() { if (!empty($this->data)) {
I have the following code to generate some spans using cakephp's helper <div id=numberRow>
I have the following form in cakephp: <div class=quickcontactDisplay> <?php echo $form->create('Enquiry',array('action'=>'add','class'=>'quickcontact')); echo $form->hidden('visitor_id',
In my CakePHP code I have the following code. If i click on the
I have following code to insert data into GiftCouponPayment table and Payment table. This
well, i have this line of code in the tutorial i am following. However,

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.