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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:43:09+00:00 2026-05-23T00:43:09+00:00

I have an array that is sent to my controller, such as this: $array

  • 0

I have an array that is sent to my controller, such as this:

$array = array(
        [0]=>array(
             [id]=>5,
             [position]=>6 
        ),
       [1]=>array(
             [id]=>8,
             [position]=>2 
        )
);

And I need to save the position of each item using its id. What is the best way to do this in cakePHP? I can only imagine looping an update function or pulling the entire database, changing the correct values, then saving the database. Both ideas seem very bodged.

  • 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-23T00:43:09+00:00Added an answer on May 23, 2026 at 12:43 am

    Ha, Cake magic to the rescue again. You don’t have to tell Cake to save it by id. There’s a big long amazing way Cake does this, but the short and skinny is –

    if your array contains an ‘id’ key, Cake presumes it is the table primary key and generates an UPDATE statement instead of an INSERT. Looks like this:

    UPDATE table as Table SET Table.position = $position WHERE Table.id = $id;
    

    And, Cake knows to iterate for you if you use saveAll() instead of save():

    $this->Model->saveAll($array);
    

    If you have any save callbacks in your model, such as beforeSave(), you have to call them manually before calling saveAll() – they only autofire on save(), not saveAll() or updateAll().

    You’ll want to top your array with the name of your model ($array[‘Model’][0], $array[‘Model’][1], etc). If you need to magically saveAll() with multiple models, you top your array with indexed keys, then model names – like $array[0][‘Model1’], $array[0][‘Model2’]) and Cake knows to save / update the associated data for all the models in each index batch.

    Cake does ALL the legwork for you:

    http://book.cakephp.org/view/1031/Saving-Your-Data – especially the saveAll() entry.

    • Edit – and topping your array with your model name? Cake makes that ridiculously easy, too. Check out Cake’s built-in Set library for all your array / object manipulation needs.

    http://book.cakephp.org/view/1487/Set

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

Sidebar

Related Questions

I am wondering, i have this array that has all the data I need.
I have a array that looks like this Array ( [provider] => Array (
I have an array that contains the RGB colour values for each pixel in
I have an array that looks like this: Array ( [0] => Array (
I'm not sure why this is happening. I have a string array that should
I have a model tied to an array controller. I need to be able
This is sort of an odd request. I have a byte array that I
I'm having this problem, where basically, I have a big array, that I break
I have an array that contains a list of vertices which I copy to
I have an array that looks like: $fields = array( f1 => array(test), f2

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.