How to migrate entities with id and their relations with rest api or php code ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tothewine
    Active Community Member
    • Jan 2018
    • 373

    How to migrate entities with id and their relations with rest api or php code ?

    I cannot use sql because of other reasons. I have a chicken/egg problem with relations:

    - I will import all metadata
    - do rebuild

    then, when I import a lead it has the relation with a call but the call is not in the system yet. that cause a problem of missing reference when saving ?
  • yuri
    Member
    • Mar 2014
    • 8440

    #2
    This is not simple question.

    You can add a custom leadId field, and then use formula to convert that leadId to the relation (with formula or workflow).
    If you find EspoCRM good, we would greatly appreciate if you could give the project a star on GitHub. We believe our work truly deserves more recognition. Thanks.

    Comment

    • tothewine
      Active Community Member
      • Jan 2018
      • 373

      #3
      Thanks for the idea, I was thinking that but it sounds hard to do to every single entity type for each relation...

      I did some research .... parent relation data seems to be retained even if fake, but not sure for the rest.

      What do you think of this other method, where one does exporting in PHP code like this:

      PHP Code:
      
      $service->loadAdditionalFields($entity);
      $export = $entity->getValues(); 
      

      And then import like this:

      PHP Code:
      
      $importEntity = $entityManager->getRepository(<scope>)->get();
      $importEntity->set($export); $entityManager->saveEntity($importEntity, ['skipAll']); 
      

      Would this work with all of the possible builtin entities in espo (and advanced pack)? I think it may have problems with password fields of inboundEmail, user and possibly others, but maybe not if the token in config.php is copied to the new instance. Are there other possible problems?


      What are your insight on this procedure ???

      Update: I checked and indeed the relation that i tested (parent) was the only one to be persisted
      Last edited by tothewine; 01-30-2020, 04:56 PM.

      Comment

      • esforim
        Active Community Member
        • Jan 2020
        • 2204

        #4
        For reference, from which CRM are you migrating from?

        Comment

        • tothewine
          Active Community Member
          • Jan 2018
          • 373

          #5
          Originally posted by espcrm
          For reference, from which CRM are you migrating from?
          I try to migrate all entities from an old version of espo to a fresh install while keeping all IDs and relationships

          Comment


          • eymen.elkum
            eymen.elkum commented
            Editing a comment
            I think you have to follow these steps:
            - extract the new & fresh (last version) espocrm to your server
            - copy only the custom folder from the old crm
            - duplicate the database if the same server or export and import to the new server if different
            - not it is the time to run the installer of the new crm
            - in the installer step regarding the database, set the connection to the new database
            - the installer will take care about everything

            Best Regards

          • tothewine
            tothewine commented
            Editing a comment
            Thank you for the involvement. I would normally do the import with sql but there is the problem that the database already contains some data that I want to add to rather than replace and in the future I would like to create a flow of entities between different instances.
            Last edited by tothewine; 01-31-2020, 01:44 PM.
        • tothewine
          Active Community Member
          • Jan 2018
          • 373

          #6
          so, i tried looping the relation property and i am able to build an array that looks complete with all populated relations (at least for Lead)

          Code:
          
          array (9) [
              'emailAddresses' => array (1) [
                  0 => string (17) "5a0f008a3c7f21b44"
              ]
              'phoneNumbers' => array (2) [
                  0 => string (17) "5a0f004e6f43856b8"
                  1 => string (17) "5d762cbc6d6dd8518"
              ]
              'users' => array (4) [
                  0 => string (17) "5a392501e8be70f41"
                  1 => string (17) "5a54c6609886e73dd"
                  2 => string (17) "5b572fe335cd3c9e5"
                  3 => string (17) "5ba31a7452b19d9f2"      
              ]
              'targetLists' => array (5) [
                  0 => string (17) "5b9fe15a1e9c08edb"
                  1 => string (17) "5ba16a52e3bb6a425"
                  2 => string (17) "5bbeece8bdedb9d53"
                  3 => string (17) "5cd7d57fb9e882c09"
                  4 => string (17) "5dd3ea500ac3259c6"
              ]
              'campaignLogRecords' => array (11) [
                  0 => string (17) "5ba023fcb22b77acc"
                  1 => string (17) "5bbf18b55bafc7ac7"
                  2 => string (17) "5beaaee9743c4143e"
                  3 => string (17) "5bf53407099d1a76d"
                  4 => string (17) "5c3dbd7060b3c1a3b"
                  5 => string (17) "5c73e0066111a552a"
                  6 => string (17) "5c8791ec90b85735c"
                  7 => string (17) "5cd1499da81f483b9"
                  8 => string (17) "5d08c250a7263ceb8"
                  9 => string (17) "5d9af7b7ae931451d"
                  10 => string (17) "5ddce32009a680f39"
              ]
              'emails' => array (1) [
                  0 => string (17) "5c482c392cbf647a8"
              ]
              'calls' => array (9) [
                  0 => string (17) "5d131e000cbeaff11"
                  1 => string (17) "5d19c4a4c93d72150"
                  2 => string (17) "5d1ccdf160995f820"
                  3 => string (17) "5d1daa7e5dd29ed85"
                  4 => string (17) "5d22ebe6ce6fc2e7b"
                  5 => string (17) "5d26f85fb79ee63e2"
                  6 => string (17) "5d35d554432d0dc11"
                  7 => string (17) "5d7a58f027760902c"
                  8 => string (17) "5e28340a988b17d82"
              ]
              'teams' => array (1) [
                  0 => string (17) "5a0d9fb4ca099923d"
              ]
              'modifiedBy' => string (17) "5dfb84c9b0329b8b7"
          ]
          About restoring then i think this array should be looped and every id related like this

          PHP Code:
          foreach ($relations as $relationName=>$related) {
          getEntitymanager->getRepository(entity scope)->relate($entity, $relationName, $entity->id);} 
          

          I still have to test this approach, but I have doubts since in the past i was only able to relate two entities from a specific side of the relation and not the other...

          I think two passes may be a necessary limitation.... create entities, save them then repeat import but for relationg. Maybe a winning approach would be to somehow parse metadata and create an insert query but it's likely too much for me, lacking insight in many espo internals (e.g. how to get all the tables involved).

          My main concern remains whether this works with all possible entity repositories (that is what I loop for doing the transfer).
          Last edited by tothewine; 02-02-2020, 02:47 PM.

          Comment

          Working...