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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:10:29+00:00 2026-06-13T19:10:29+00:00

PROBLEM 1: When I try to save() any Yii Model, it updates all fields

  • 0

PROBLEM 1:

When I try to save() any Yii Model, it updates all fields in the row.
The problem is: When I try to save model users, even if has no PASSWORD to update, it get the database value(already hashed) and hash again.
How can I do to YII only update fields that I want?

Code:

$user = Users::model()->findByAttributes(array('username'=>$this->username));
$user->ip = $_SERVER['REMOTE_ADDR'];
$user->save();

Users.php (Model):

public function beforeSave() {
    if (!empty($this->password))
        $this->password=$this->hashPassword($this->password);
    return true;
}

PROBLEM 2:

I have an API that can create USERS.
API Tutorial: http://www.yiiframework.com/wiki/175/how-to-create-a-rest-api/

When I have crypter_password in the database, instead password, I got the error: Parameter password is not allowed for model Users, because the API validate parameters using $model->hasAttribute().
How can I fix the API actionCreate to allow custom parameters?

  • 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-13T19:10:30+00:00Added an answer on June 13, 2026 at 7:10 pm

    Eventhough the other answers listed here are not wrong, they are definitely not really developer friendly and it’s extremely easy to forget to add the attributes to the save line.

    Here is a developer friendly way of working.

    In your model, add the following attribute:

    private $_aAttributesBackup;
    

    In this variable, we will store an exact copy of the current model. To do this, the following afterFind method needs to be added:

    public function afterFind()
    {
        $this->_aAttributesBackup = $this->attributes;
    }
    

    Almost there. At this point, the model will store all of his attributes in the attributesBackup field which makes it easier to compare. To make it easier, we also need a method that will check if the specified attribute has a backup value. We do this by adding the following code into our model:

    public function getOriginalAttribute($sAttribute)
    {
        if ($this->_aAttributesBackup)
        {
            return $this->_aAttributesBackup[$sAttribute];
        }
    
        return NULL;
    }
    

    Now, how about checking if the password has been changed? Simple, by adding the following beforeSave code:

    public function beforeSave()
    {
        if ($this->getOriginalAttribute('password') != $this->password)
        {
            $this->password = sha1($this->password);
        }
        return parent::beforeSave();
    }
    

    Et voila. Now everytime you execute the code $Model->save(); the system will check if the password has been changed, If the password is changed, it will hash it again, if it is not changed, it won’t be hashed again.

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

Sidebar

Related Questions

this is the problem once i try to save data into db with sql
I have some problem with Emit mapper when I try to save in database
I have a problem when I try to persist my model. An exception is
I have a problem that when I try to save an object in the
i am having this problem try to use the cocos2d CCScrollLayer I have added
i have a problem when try to get and parse a remote JSON. require
I have a problem when try to use hashtable with large data. I have
I'm having a problem with TRY...CATCH blocks. Can someone explain why the following code
I have a problem when I try to count how many times a word
There is a problem when I try to request contract on iTunes Connect. On

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.