ORM Setting Value in afterSave Not Working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blueprint
    Active Community Member
    • Jan 2019
    • 223

    ORM Setting Value in afterSave Not Working

    I've implemented an 'afterSave' function in one of my custom Repositories but I cannot get a field to be 'set' - I see the value change initially before going back to 'None':

    Code:
    <?php
    namespace Espo\Custom\Repositories;
    use Espo\ORM\Entity;
    class SerialNumber extends \Espo\Core\Templates\Repositories\Base
    {
        protected function afterSave(Entity $entity, array $options = [])
        {
            parent::afterSave($entity, $options);
            // Get the linked part
            $part = $entity->get('part');
            if ($part) {
                // Get the part number
                if ($part->has('partNumber')) {
                    $partNumber = $part->get('partNumber');
                    $serialNumber = $entity->get('serialNumber');
                    $fullPartNumber = $partNumber . '.' . $serialNumber;
                    $entity->set('name', $fullPartNumber);
                    $GLOBALS['log']->warn('Number', [$fullPartNumber]);
                }
            }
        }
    }
    What am I doing wrong in this instance?
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #2
    Hello,
    it's for me nomal ..
    imagine ... beforeSave.. .afterSave.
    if you save so in afterSave... then he become beforeSave .. then loop infinite

    if you save in afterSaver.. use PDO ..

    Regards
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    • blueprint
      Active Community Member
      • Jan 2019
      • 223

      #3
      Originally posted by item
      Hello,
      it's for me nomal ..
      imagine ... beforeSave.. .afterSave.
      if you save so in afterSave... then he become beforeSave .. then loop infinite

      if you save in afterSaver.. use PDO ..

      Regards
      That doesn't seem to be the case at all as `beforeSave` and `afterSave` are only called once (tested and seen using debug statements)

      Comment

      • blueprint
        Active Community Member
        • Jan 2019
        • 223

        #4
        What is also weird is that if I update say the 'description' field then the 'name' field is updated and the correct value is show in the detail view yet if I then refresh the screen, the value then lost and the `name` field resets back to `None`.

        I'm missing something, clearly.

        Comment

        • item
          Active Community Member
          • Mar 2017
          • 1476

          #5
          Hello,
          try .. you will see :

          on beforeSave => no need to write : $entityManager->saveEntity($entity, ['skipHooks' => true]);

          on afterSave =. need : $entityManager->saveEntity($entity, ['skipHooks' => true]); => then you will loop certainly... maybe with option ['skipHooks' => true] . not loop ..
          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

            #6
            Hello, could you explain what is the difference between Repositories and Hooks ?

            Comment

            • item
              Active Community Member
              • Mar 2017
              • 1476

              #7
              Hello telecastg,
              if the question if for me :

              sample :
              on hook you do : entity->set('status' = 'New');

              on repository
              $entity->set('status' = 'Closed'):

              the result will be always 'Closed'

              it's the last layer before modify database. (for me if i am not wrong )

              PHP Code:
                  protected function beforeSave(Entity $entity, array $options = [])
                  {
                      parent::beforeSave($entity, $options);
                      $entity->set('name', $entity->get('accountName') .' - ' .$entity->get('contactName') );
                      if ($entity->isNew()) {
                          $entity->set('createdAt', date("Y-m-d") );
                      }
                  } 
              
              Last edited by item; 02-02-2020, 11:22 PM.
              If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

              Comment


              • telecastg
                telecastg commented
                Editing a comment
                Thanks so much item

                Do you have an opinion of when is better to use Hooks or Repositories ?

                Best Regards
                Last edited by telecastg; 02-03-2020, 12:08 AM.

              • item
                item commented
                Editing a comment
                Hello telecastg,
                with my skill .. i will give a sample :
                hook => user skill => developper skill => formula ... and so

                repositery => the specialist
                i think "encryptData" in database... where i put : here.
                and decryptData -> here too.

                I think repository is only the last think before database.
            • item
              Active Community Member
              • Mar 2017
              • 1476

              #8
              Hello telecastg,
              i have your response about "hooks => repository" ..
              i have reported a bug about acceptanceStatus... and Yuri have resolved the issue in a few "secondes"
              and the bug was on repository of meeting .. and when i have asked if normal or no .. he say : normal it's design.

              So your "design" => repository
              Hook is not design, it's custom

              Regards

              If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

              Comment

              Working...