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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:30:36+00:00 2026-06-05T17:30:36+00:00

I am trying to get cakePHP to update a record in the database as

  • 0

I am trying to get cakePHP to update a record in the database as appose to creating a new one. My script does a check with “FIRST”, and if it does not return false, it should create a new record, else it should update the existing one based on the ID I am specifying. I have below deliberately added the $this->Asset->id = 62; to try force an update, but it does not work. Any help? (Also, if you can understand what it’s suppose to do, please feel free to guide me with perhaps better code 🙂 Very new to cake:

 public function index() {

        if($this->request->is("post")) {
            $aid = $this->request->data['Asset']['asset_identifier'];
            $type = $this->request->data['Asset']['asset_type_id'];
            $asset = $this->Asset->find("first",array("fields" => array("Asset.id", "Asset.user_id", "Asset.status_id", "Asset.financial_id", "Asset.insurance_id"),"conditions" => array("Asset.asset_identifier" => $aid, "Asset.asset_type_id" => $type)));
            if($asset != false) {
                $uid = $this->Auth->user("id");
                // UPDATE, depends on info:
                $this->Asset->id = $asset['Asset']['id'];
                foreach(array("financial_id", "insurance_id", "user_id") as $value) {
                    if($asset['Asset'][$value] != 0) {
                        $emails[] = $asset['Asset'][$value];
                    }
                }
                if(isset($emails) && count($emails)) {
                    $this->request->data['Asset']['status_id'] = 1;
                    $email = new CakeEmail();
                    $email->from(array('noreply@assetchase.co.za' => 'Assetchase.co.za'));
                    $email->subject('Assetchase.co.za result notification.');
                    foreach($emails as $value) {
                        $user = $this->User->find("first",array("fields" => array("username"),"conditions" => array("id" => $value)));
                        $email->to($user['User']['username']);
                        $email->send('A new notification, booyah!');
                        // Send an email with the username.
                    }
                }
                if($this->Auth->user("user_type_id") == 2) {
                    $this->request->data['Asset']['user_id'] = $uid;
                } elseif($this->Auth->user("user_type_id") == 3) {
                    $this->request->data['Asset']['financial_id'] = $uid;
                } elseif($this->Auth->user("user_type_id") == 4) {
                    $this->request->data['Asset']['insurance_id'] = $uid;
                }
            }
            $this->Asset->id = 62;
            if($this->Asset->saveAll($this->request->data)) {
                $this->Session->setFlash("A new asset has been loaded",'success');
                $this->redirect(array("controller" => "asset", "action" => "thankyou", "guest"));
            } else {
                $this->Session->setFlash('An error has occured.','default',array('class'=>'error'));
            }
        }
        $assetTypes = $this->Asset->AssetType->find('list');
        $this->set("assetTypes", $assetTypes);

    }
  • 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-05T17:30:37+00:00Added an answer on June 5, 2026 at 5:30 pm

    When you call $this->Asset->saveAll($this->request->data) you are telling Cake to take the POST data and try to save it. It does not look at $this->Asset where you set the ID to 62 at all. You can verify that by inspecting the output of var_dump($this->request->data). Try something like $this->request->data['Asset']['id'] = 62 instead.

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

Sidebar

Related Questions

I'm trying to get this loop to save a new record to the database
Trying to get CakePHP work with subfolders for Controllers, Views, or Models is not
I'm trying to get mongodb to work with cakephp. I'm not sure on where
I'm trying to get a CakePHP application to work. For this, I've set up
I am using cakephp. I am trying to use httpsocket to get information. <?php
I am using cakephp. I am trying get data from facebook using file_get_contents. I
I have a CakePHP application where I am trying to get a count of
I'm trying to get started with Django, and have previously worked with CakePHP, and
I've got a cakephp app that I'm trying to get to serve up the
I'm trying to load , edit and save a record with CakePHP 2.0 but

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.