Create Record On Update

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krishnapriya
    Senior Member
    • Aug 2017
    • 209

    Create Record On Update

    Hello,

    I need to create new record when we edit and save.

    How can I achieve this. Please help.

    Thanks.
  • tanya
    Senior Member
    • Jun 2014
    • 4308

    #2
    Hello
    Use for this hooks

    Comment

    • krishnapriya
      Senior Member
      • Aug 2017
      • 209

      #3
      Hello,

      $entityManager = $this->getEntityManager();

      $account = $entityManager->getEntity('Test'); // Tried to create new Test record

      $account->set("name", 'hi');

      $entityManager->saveEntity($account); // gives error-<b>Fatal error</b>: Allowed memory size of 268435456 bytes exhausted (tried to allocate 4096 bytes)

      Please help.
      Thnaks


      Comment

      • krishnapriya
        Senior Member
        • Aug 2017
        • 209

        #4

        $account = $entityManager->getEntity('Test') // Here if we give another entity name it works fine

        But giving same entity name as of hook => gives error.

        Please help.

        Comment

        • tanya
          Senior Member
          • Jun 2014
          • 4308

          #5
          I am trying to create a hook for the beforeSave event on Tasks so that it spawns multiple, new tasks for a particular task. Below are the 'barebones' of it

          Do you have Entity with name "Test"?

          Comment

          • krishnapriya
            Senior Member
            • Aug 2017
            • 209

            #6
            Yes Tanya Test entity exists.

            With the if condition it is working now.

            if (!$entity->isNew()) {
            $entityManager = $this->getEntityManager();
            $newhire = $entityManager->getEntity('Test');
            $newhire->set('name', 'Test new hire task1');
            $entityManager->saveEntity($newhire);
            }

            This code creates a new record Test new hire task1 on edit and the current record is updated.

            But I need the current record to remain as unmodified and want to create new record as the modified version.

            How can I achieve this. Please help.
            Thanks.

            Comment

            • tanya
              Senior Member
              • Jun 2014
              • 4308

              #7
              Don't think it's a good idea. Better to create a duplicate of old entity, and not leave current.
              but you need something like this:

              if (!$entity->isNew()) {
              $beforeEntity = $this->getEntityManager()->getEntity($entity->getEntityType(), $entity->id);
              $beforeValues = $beforeEntity->toArray();
              $currentValues = $entity->toArray();

              unset($currentValues['id']);

              $newhire = $this->getEntityManager()->getEntity($entity->getEntityType());
              $newhire->set($currentValues);
              $this->getEntityManager()->saveEntity($newhire);

              $entity->set($beforeValues);
              }

              Comment

              • krishnapriya
                Senior Member
                • Aug 2017
                • 209

                #8
                That really helped.

                Thank you so much Tanya.

                Comment

                • krishnapriya
                  Senior Member
                  • Aug 2017
                  • 209

                  #9
                  Hello,

                  I have one number field 'idNo' in Test entity.

                  When I create $newhire i didnt want to change the idNo field.Or should b same as the original one.

                  So I tried like this.

                  if (!$entity->isNew()) {
                  $beforeEntity = $this->getEntityManager()->getEntity($entity->getEntityType(), $entity->id);
                  $beforeValues = $beforeEntity->toArray();
                  $currentValues = $entity->toArray();

                  unset($currentValues['id']);

                  $newhire = $this->getEntityManager()->getEntity($entity->getEntityType());
                  $newhire->set($currentValues);

                  $idNo=$beforeEntity->get('idNo');
                  $newhire->set('idNo')=$idNo;

                  $this->getEntityManager()->saveEntity($newhire);

                  $entity->set($beforeValues);
                  }



                  But it doesn't work.
                  Please help.
                  Thanks.

                  Comment

                  • krishnapriya
                    Senior Member
                    • Aug 2017
                    • 209

                    #10
                    It is working now with condition statement added in NextNumber hook.

                    Comment

                    • krishnapriya
                      Senior Member
                      • Aug 2017
                      • 209

                      #11
                      Hello,

                      When I update beforeEntity It creates newHire, but to see newHire I need to go back to the list view and then have to click newHire.

                      How can I redirect to the newly created newHire record on clicking save in beforeEntity?

                      Please help.

                      Thanks in advance.

                      Comment

                      • tanya
                        Senior Member
                        • Jun 2014
                        • 4308

                        #12
                        client/src/views/record/detail.js exitAfterCreate
                        override this method in you view (I think you know how to override detail and edit record view)

                        Comment

                        • krishnapriya
                          Senior Member
                          • Aug 2017
                          • 209

                          #13
                          Thanks tanya, it worked. Thank you so much

                          Comment

                          Working...