7.0x about value-objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • item
    Active Community Member
    • Mar 2017
    • 1476

    7.0x about value-objects

    https://docs.espocrm.com/development...#value-objects

    Hi,
    i try .. but no luck (certainly because my english)
    how i can "insert" phoneNumber in back-end

    i have try many .. no luck
    nothing is saved in entity

    HTML Code:
    $group = $entity->getPhoneNumberGroup();
    
    if ($data->phone_office && !$group->hasNumber($data->phone_office))
    {
    $phone = PhoneNumber::create($data->phone_office);
    $phone->withType('Office');
    $group->withAdded($phone);
    
    }elseif($data->phone_alternate && !$group->hasNumber($data->phone_alternate))
    {
    $phone = PhoneNumber::create($data->phone_alternate);
    $phone->withType('Home');
    $group->withAdded($phone);
    
    }elseif($data->dio_phone_mobile && !$group->hasNumber($data->dio_phone_mobile))
    {
    $phone = PhoneNumber::create($data->dio_phone_mobile);
    $phone->withType('Mobile');
    $group->withAdded($phone);
    }
    
    
    $valueMap = (new PhoneNumberGroupAttributeExtractor())->extract($group, 'phoneNumberData');
    $GLOBALS['log']->warning('valueMap => ' .json_encode( $valueMap ));
    $GLOBALS['log']->warning('group => ' .json_encode( $group ));
    $GLOBALS['log']->warning('phone => ' .json_encode( $phone ));
    //$entity->setPhoneNumberGroup($group);
    //$entity->setValueObject($group);
    
    $GLOBALS['log']->warning('group => ' .json_encode( $phoneNumberData ));
    $entity->setPhoneNumberGroup($phoneNumberData);
    $em->saveEntity($entity, $options);
    Last edited by item; 11-06-2021, 12:03 AM.
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​
  • yuri
    Member
    • Mar 2014
    • 8451

    #2
    Value objects are immutable. https://en.wikipedia.org/wiki/Value_object.

    Wrong:

    PHP Code:
    $valueObject->withSomething($something); 
    

    Right:

    PHP Code:
    $valueObject = $valueObject->withSomething($something); 
    

    https://docs.espocrm.com/development...#value-objects
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • item
      Active Community Member
      • Mar 2017
      • 1476

      #3
      Thanks Yuri,
      i don't undertand all but with your guidance..

      PHP Code:
      if ($data->dio_phone_mobile)
      {
              $phoneNumberGroup = $entity->getPhoneNumberGroup();
              if (!$phoneNumberGroup->hasNumber($data->dio_phone_mobile))
             {
                     $modifiedPhoneNumberGroup = $phoneNumberGroup
                           ->withAdded( PhoneNumber::create($data->dio_phone_mobile)->withType('Mobile'));
                     $entity->setPhoneNumberGroup($modifiedPhoneNumberGroup);
                     $em->saveEntity($entity);
             }
      } 
      
      Last edited by item; 11-06-2021, 09:04 PM.
      If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

      Comment

      • telecastg
        Active Community Member
        • Jun 2018
        • 907

        #4
        Hello item ,
        Trying to educate myself on what are "value objects" and how they work, ran into this article that might help
        If you already have heard about DDD (Domain-Driven Design), you probably also may have heard about...

        Comment


        • item
          item commented
          Editing a comment
          Hello telecastg,
          as you know.. my english is like a Penguin in Sahara

          i think on what i have understand.. it's for "multi-users/multi-threatding" (for simple explanation).. i can not say more, it's out of my scope

        • telecastg
          telecastg commented
          Editing a comment
          like a Penguin in Sahara
          that's how I feel sometimes, specially if dealing with server issues or git
      Working...