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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:59:37+00:00 2026-06-07T18:59:37+00:00

This is probably a simple question but I’m having a hard time trying to

  • 0

This is probably a simple question but I’m having a hard time trying to create an object of the Model. I want to create a temporary variable that is the same as the model so I can update its internal values according to the data I am posting to the action.

My data is successfully posted to the action in the controller.
Currently what I am doing is grabbing all the items in the database and then assigning it the first item from the array. This way I can overwrite existing value, but if the table is empty it won’t work.

I am using cakephp 2.x, I am new to this stuff which will explain why I couldn’t figure this out.

Here is the code from the controller:

public function lcadd()
    {
        if ($this->request->is('post'))
        {
            // Create a new Local Clock object
            $temp = $this->LocalClock->find('all'); // THIS IS THE PROBLEM AREA
            $temp = $temp[0];

            $temp['LocalClock']['name'] = $this->request->data['LocalClock']['Name'];
            debug($temp);  // THIS DEBUG ONLY DISPLAYS SOMETHING IF THE DATABASE TABLE IS NOT EMPTY

            debug($this->request->data);  // THE DATA COMING IN IS CORRECT AND I CAN ACCESS IT WITHOUT A PROBLEM

            if($this->request->data['LocalClock']['Time Zone'] == 0 or $this->request->data['LocalClock']['Time Zone'] == -1)
            {
                //debug('Hello');
                $temp['LocalClock']['auto_offset'] = 1;
            }
            else if($this->request->data['LocalClock']['Time Zone'] == 1)
            {
                $temp['LocalClock']['auto_offset'] = 0;
            }

            // Check the value posted from the DST
            if($this->request->data['LocalClock']['DST Definition'] == -1 or $this->request->data['LocalClock']['DST Definition'] == 0)
            {
                $temp['LocalClock']['in_month'] = 0;
            }
            else if($this->request->data['LocalClock']['DST Definition'] == 2)
            {
                $temp['LocalClock']['in_month'] = $this->request->data['LocalClock']['InMonth'];
            }

            if ($this->LocalClock->save($temp))
            {
                $this->set('localClocks', $this->request->data);
                $this->set('islcAddValid', true);
                $this->set('lcaddValidationErrors', false);
            }
        }
    }

I can’t figure out how to make a simple object from the model. The model is LocalClock, and the controller is LocalClocksController.
The function above is in the LocalClocksController.

Thanks in advance.

  • 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-07T18:59:38+00:00Added an answer on June 7, 2026 at 6:59 pm

    A few things I noticed…

    FIRST ISSUE:

    // Create a new Local Clock object
    $temp = $this->LocalClock->find('all'); // THIS IS THE PROBLEM AREA
    $temp = $temp[0];
    

    That’s not creating a new Local Clock object. The first line returns an array (ie, not an object) containing ALL records in your Local Clock database table. Then, the second line says ‘take the first row of the results, and store it in $temp’. So, now $temp holds an array (still NOT an object), containing the data of the first record in the LocalClock table.

    You actually don’t need an object, though, since the ‘save’ method that you call later on doesn’t accept an object – it just accepts an array of data. You also don’t need to get anything from the database in order to save a new record. So the code above should be replaced with:

    $temp = array(); // Create an empty array. We will populate it as we go, with the data for the new record.
    

    All the stuff in the middle of your method, up until you try to save the record, should work fine as it is.

    SECOND ISSUE:

    if ($this->LocalClock->save($temp))
    

    Before calling ‘save’, you’ll first need to call ‘create’ –

    $this->LocalClock->create(); // prepare the Model to save a new record
    if ($this->LocalClock->save($temp)) // This should now save a new record successfully.
    

    Make sense? Let me know if you have any more questions.

    Also, understanding how to retrieve, manipulate and save data from Models is very crucial to using CakePHP, and if you learn how to do it properly from the beginning, you’ll save yourself countless hours of frustration in future. I’d recommend reading the following pages end-to-end:

    1. http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

    2. http://book.cakephp.org/2.0/en/models/saving-your-data.html

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

Sidebar

Related Questions

This is probably a simple question but I am having lots of trouble getting
This is probably a simple question, but I'm trying to lookup the constant name
I know this is probably a simple question, but I'm still trying to figure
This is probably quite a simple question but very hard to google! I'm loading
This is probably a simple question, but it's hard to Google. What is the
I know this is probably a pretty simple question, but i am trying to
This is probably a very simple question, but at this time I have myself
I know this is probably a really simple question but I'm having a brain
Hey, this is probably a simple question, but I am having trouble running my
This is probably a simple question but I can't seem to figure out how

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.