Uploaded image for project: 'CiviCRM'
  1. CiviCRM
  2. CRM-21048

Names equal to '0' are erased during a Contact update

    Details

    • Type: Bug
    • Status: Open
    • Priority: Trivial
    • Resolution: Unresolved
    • Affects Version/s: 4.7.18
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Versioning Impact:
      Patch (backwards-compatible bug fixes)
    • Documentation Required?:
      None
    • Funding Source:
      Needs Funding
    • Verified?:
      No

      Description

      Note: This is a very minor bug, but I'm reporting it in case a Mr./Mrs. 0 tries to use Civi. 

      Consider this test:

       

      public function testUpdatingContactWithZero() {
        $contact = civicrm_api3('Contact', 'create', [
          'contact_type' => 'Individual',
          'first_name' => 'Name',
          'last_name' => '0',
          'sequential' => 1
        ])['values'][0];
      
        $this->assertEquals('Name 0', $contact['display_name']);
      
        $contact = civicrm_api3('Contact', 'get', [
          'id' => $contact['id'],
          'sequential' => 1
        ])['values'][0];
      
        $this->assertEquals('Name 0', $contact['display_name']);
      
        civicrm_api3('Contact', 'create', [
          'id' => $contact['id']
        ]);
      
        $contact = civicrm_api3('Contact', 'get', [
          'id' => $contact['id'],
          'sequential' => 1
        ])['values'][0];
      
        $this->assertEquals('Name 0', $contact['display_name']);
      }
      
      

       

      If you run that, you'll see that the last assert will fail. What happens is that, during the update, Civi does some checks to see if it should keep the value from the DB. In a very simplified way this is what it does:

       

      $valueToSave = '';
      $valueFromDB = $this->loadValueFromDB();
      if ($valueFromDB && empty($params['last_name'])) {
        $valueToSave = $valueFromDB;
      }

       

      Value from DB is '0', which in PHP is considered the same as false meaning that the if block will never be executed and the $valueToSave will remain empty.

       

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              davialexandre Davi Tavares Alexandre
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: