Announcement

Collapse
No announcement yet.

ORM Setting Value in afterSave Not Working

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?

  • #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

    Comment


    • #3
      Originally posted by item View Post
      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


      • #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


        • #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 ..

          Comment


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

            Comment


            • #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.

              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.

            • #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

              Comment

              Working...
              X