Espo 5.2.x auto-increment value in hooks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cavefish
    Junior Member
    • Nov 2016
    • 10

    Espo 5.2.x auto-increment value in hooks

    Since version 5.2 I'm unable to get value of auto-increment fields in beforeSave() and afterSave() hooks on first save. Editing existing entries works fine.
    Is there some kind of fundamental change regarding auto-increment fields since this version or am I doing something wrong?

    Thanks
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    in beforeSave hook it coudn't be possible, in afterSave it possible. Even if field is not set, you can load this entity with this field

    Comment

    • umar
      Junior Member
      • Aug 2017
      • 4

      #3
      hi tanya, can you please provide a bit more detail, how to load this entity, and get the value in afterSave hook.

      Comment

      • tanya
        Senior Member
        • Jun 2014
        • 4308

        #4
        After save entity has this field.

        share with me your code, please

        Comment

        • umar
          Junior Member
          • Aug 2017
          • 4

          #5
          public function afterSave(Entity $entity, array $options = array()){
          if(empty($entity->get('serialNumber'))){
          $prefix = "Test-";
          $user = $this->getEntityManager()->getRepository('User')->get($entity->get('assignedUserId'));
          if(!empty($user->get('name'))){
          $prefix .= $user->get('firstName')[0].$user->get('lastName')[0]."-".$entity->get('number');
          }

          $pdo = $this->getEntityManager()->getPDO();
          $sql = "update test set serial_number = '{$prefix}'";
          $sth = $pdo->prepare($sql);
          $sth->execute();
          }
          }

          Comment


          • umar
            umar commented
            Editing a comment
            $entity->get('number') this number field is the one with type auto-increment.
        • umar
          Junior Member
          • Aug 2017
          • 4

          #6
          tanya did you get a chance to check this ?

          Comment

          • murray99
            Member
            • Jan 2017
            • 57

            #7
            very late on, but Auto Increment does not seem to be set in an Entity event afterSave and so I had to check if it was null and then re-load from the database if it was in order to use an auto-increment in a hook.

            Comment

            Working...