Improve insertOnDuplicateUpdate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dimyy
    Active Community Member
    • Jun 2018
    • 569

    Improve insertOnDuplicateUpdate

    insertOnDuplicateUpdate is very useful when integrating with external services.
    External entities have their own Id's and in the espo we have

    Code:
    {
     "fields": {
       "externalId": {
         "type": "int"
       }   
     },
     "indexes": {
      "externalIdUnique": {
        "type": "unique",
           "columns": [
           "externalId",
           "deleted"
        ]
      }
    }
    It would be nice if insertOnDuplicateUpdate have full entity support:

    1. Default ID (this code without setting ID don't work, cause PDOException SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value in /application/Espo/ORM/SqlExecutor.php:72)

    PHP Code:
    $em = $this->getEntityManager();
    $e = $em->getEntity('Host');
    
    $id = \Espo\Core\Utils\Util::generateId();
    
    $e->set([
    // 'id' => $id,
    'externalId'=> '999',
    'name' => 'Модифицированный'
    ]);
    
    $s = $em->getMapper('RDB')->insertOnDuplicateUpdate($e, [
    'name'
    ]); 
    
    2. Support all field types, included jsonType. jsonType fullinfo is null after insertOnDuplicateUpdate($e ...

    PHP Code:
    $e->set([
    'fullInfo' => '{"a":"b"}'
    ]); 
    
    3. Support fetched id if duplicate founded.
    Now $e->id is preliminarily generated value, not actual.

    4. Support additional link fields. This code cause exception. But in saveEntity all ok.

    PHP Code:
    set([
     'HostUsersIds' => $hI,
     'HostUsersNames' => $hN,
     'HostUsersColumns' => $hC
    ]); 
    
  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    #2
    Too bad you post this too late, I think v7 also been finalized unless bug appear but I think it feature frozen. Otherwise this would have been good addition.

    Comment


    • yuri
      yuri commented
      Editing a comment
      This is not a thing that is easy to implement. I'm not sure if will be implemented ever. And supporting link-multiple fields is not possible at all.
      Last edited by yuri; 10-01-2021, 06:41 AM.
  • yuri
    Member
    • Mar 2014
    • 8440

    #3
    1. In the context of Mapper an ID should not be set inside the method. The current behavior is right.
    2. Did you try jsonObject type in espo? I don't think that tne problem is with the mapper and is related to insertonduplicateupdate method.
    4. Impossible.
    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


    • dimyy
      dimyy commented
      Editing a comment
      Certainly. in the given piece of code fullInfo is jsonObject

      I try
      $a = [ 'a' => 'b' ]
      1. 'fullInfo' => $a
      2. 'fullInfo' => json_encode($a)
Working...