Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to update one field in database using CakePhp?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 275
    Comment on it

    Hi Reader's,
    Welcome to FindNerd, today we are going to discuss how to update one field in database using CakePHP?

    If you want update some records or data in your web application database in CakePHP the you have to update your record based on primary key(Id).

    If you want save a single field value then you have to set ID of the Model just before saveField() and also remember when you are using this method just contain $fieldName not contain name of the Model.

    You can see below method

    1-Use saveField method

    $this->ModelName->id=$id;
    $this->ModelName->saveField("fieldName","value");
    

    2-Use updateAll method

    $this->Model->updateAll(array("fieldName"=>"value"),array("fieldName"=>"condition"));

    you can see below example

    <?php
     //get id from UserProfile Model
      $userProfileInfo = $this->UserProfile->findByUserId($id);
      //check id is exist or not
      if(!empty($userProfileInfo)){
        //update id field
        $this->UserProfile->updateAll($userProfileData, array('UserProfile.id' => $userProfile['UserProfile']['id']));
      } else{
          $data['UserProfile']['user_id'] = $data['User']['id'];
          //creat a profile and save into table
          $this->UserProfile->create();
          $this->UserProfile->save($data['UserProfile']);
        }
      ?>

    In above example UserProfile is a model and we are finding with help of id and checking if is is exit then we are using updateAll for update  UserProfile.id. If id is not exist then we are saving data in UserProfile Model.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: