Home > Back-end >  Update user info using googleapi using php will make the predefine field empty if not updated
Update user info using googleapi using php will make the predefine field empty if not updated

Time:02-05

I'm working to update the user info using googleapi in php.

When I run this code it will make the predefine field which I don't want to update empty. As in this code I'm not setting Title as the title is already set but once I run this code the title will become empty.

How can I resolve it?

$organization = new \Google\Service\Directory\UserOrganization();
        $organization->setPrimary(TRUE);
      //  $organization->setTitle('Lead Developer');
        $organization->setDepartment('Dev');
        $googleUser->setOrganizations([$organization]);
        
        $gsdService->users->update('[email protected]', $googleUser);

CodePudding user response:

The issue is that you are using the user.update method. This method uses HTTP Put methodology and will update everything in the object exactly as you send it. So by not sending a title you are telling the system you want that field to be null.

If you only want to update the files that you send then you should be using users.patch which follows HTTP Patch methodology and only updates the properties that are sent in the user object.

$organization = new \Google\Service\Directory\UserOrganization();
$organization->setPrimary(TRUE);
$organization->setDepartment('Dev');
$googleUser->setOrganizations([$organization]);
    
$gsdService->users->patch('[email protected]', $googleUser);

CodePudding user response:

While the documentation for users.update specifies:

This method supports patch semantics, meaning you only need to include the fields you wish to update. Fields that are not present in the request will be preserved

The documentation for orgunits.update just says:

Updates an organizational unit.

So, you cannot expect orgunits.update to preserve not specified fields and it is not a bug.

You can file a feature request to ask to support patch semantics for orgunits.update.

In the meantime, if you have problems with not empty fields being overwritten - consider as a workaround specifying in your orgunits.update request all fields that you do not want to become empty.

  •  Tags:  
  • Related