Announcement

Collapse
No announcement yet.

Create Record On Update

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

  • Create Record On Update

    Hello,

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

    How can I achieve this. Please help.

    Thanks.

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


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


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


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


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


            • #8
              That really helped.

              Thank you so much Tanya.

              Comment


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


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

                  Comment


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


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


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

                        Comment

                        Working...
                        X