Update records in YII

Hello Everyone!!!!

I have one controller and it has all functions that are required for CRUD operations. So in actionUpdate(), I have a special requirement.

There are 8-10 fields in the form for a model. Now what I want is, if any field value is changed or updated then I need to send an email to a particular email address. If all values are same as previous one then mail should not be send. For sending mail, I have a good code and it is working nice.

My question is how to check whether atleast a single field is updated or not??

For e.g.

If USER model has 5 fields like : usr_id,usr_name,usr_add1,usr_zip,usr_mobile

Now while updating any record of that user, if usr_name is changed then also email should be sent. Atleast value of one field should be changed for sending an email.

Thanks in Advance for your help :)

since U already have User model in some variable (probaly $user), U could do:

before assigning new attributes to it, save it in another var:

$oldUser = $user;

than assign attributes to $user and save it;

after that U can do:

$changed = false;

foreach($oldUser->attributes as $key => $value)

{

$newValue = $user->attributes[$key] // get new value


if($value != $newValue)


   $changed = true;   

}

after that U can just check for $changed :

if($changed)

{

send and email;

}

1 Like

It is nice way to search for changed field. But Is there any inbuilt function in YII that will give the direct result. So we dont need to create loop for checking attributes value??

I don’t think there is, maybe U can check for extensions

1 Like

Sure, I will check for the extension if available.

Thanks for your support…