Hooks : afterRelate call afterUnrelate

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • item
    Active Community Member
    • Mar 2017
    • 1476

    Hooks : afterRelate call afterUnrelate

    Hi,
    Who can see where is my fault ?
    Entity Clinic and Epidemio : No Formula

    Entity Clinic have a "many2many" Epidemio and many "one2many" Epidemio.
    RelationName : epidemios (many2many) and epidemiosA, epidemiosB,.... epidemiosS ... epidemiosZ

    PHP Code:
        public function afterRelate(Entity $entity, array $hookOptionData, array $relationData) : void
        {
    
            $this->log->error( "afterRelate". json_encode($relationData ) );
            $this->log->error( "afterRelate". json_encode($hookOptionData ) );
            
            $relationName = $relationData['relationName'] ?? null;
            $foreignEntity = $relationData['foreignEntity'] ?? null;
            
            $this->log->error('CLINIC-AFTER-RELATE=>' .$entity->getEntityType() );
            $this->log->error('CLINIC-AFTER-RELATE=>' .$foreignEntity->getEntityType() );
            $this->log->error('CLINIC-AFTER-RELATE=>' .$relationName .'=>' .$foreignEntity->getId());
            
            if ( $relationName === 'epidemios') return;
            
            $this->log->error('2-AFTER-RELATE->IN' );
            $this->em->getRDBRepository('Clinic')
                        ->getRelation($entity, 'epidemios' )
                        ->relate($foreignEntity);  
            $this->log->error('3-AFTER-RELATE-> DONE' );              
    
        }
    
        public function afterUnrelate(Entity $entity, array $hookOptionData, array $relationData): void
        {
    
            $this->log->error( "afterUnrelate". json_encode($relationData ) );
            $this->log->error( "afterUnrelate". json_encode($hookOptionData ) );
            
            $relationName = $relationData['relationName'] ?? null;
            $foreignEntity = $relationData['foreignEntity'] ?? null;
    
            $this->log->error('CLINIC-AFTER-UN-RELATE=>' .$entity->getEntityType() );
            $this->log->error('CLINIC-AFTER-UN-RELATE=>' .$foreignEntity->getEntityType() );
            $this->log->error('CLINIC-AFTER-UN-RELATE=>' .$relationName .'=>' .$foreignEntity->getId());
            
            if ( $relationName === 'epidemios') return;
    
            $this->log->error('2-AFTER-UN-RELATE->IN');
            $this->em->getRDBRepository('Clinic')
                    ->getRelation($entity, 'epidemios')
                    ->unRelate($foreignEntity);
            $this->log->error('3-AFTER-UN-RELATE->UN RELATE-> DONE' );
    
        }
    And log : I don't find why afterUnrelate is called ?

    PHP Code:
    
    [2024-08-11 12:49:42] ERROR: afterRelate{"relationName":"epidemiosE","relationData":null,"foreignEntity":{"id":"644d7d61248551f34"},"foreignId":"644d7d61248551f34"}
    [2024-08-11 12:49:42] ERROR: afterRelate{"skipHooks":false}
    [2024-08-11 12:49:42] ERROR: CLINIC-AFTER-RELATE=>Clinic
    [2024-08-11 12:49:42] ERROR: CLINIC-AFTER-RELATE=>Epidemio
    [2024-08-11 12:49:42] ERROR: CLINIC-AFTER-RELATE=>epidemiosE=>644d7d61248551f34
    [2024-08-11 12:49:42] ERROR: 2-AFTER-RELATE->IN
    [2024-08-11 12:49:42] ERROR: afterRelate{"relationName":"epidemios","relationData":null,"foreignEntity":{"id":"644d7d61248551f34"},"foreignId":"644d7d61248551f34"}
    [2024-08-11 12:49:42] ERROR: afterRelate[]
    [2024-08-11 12:49:42] ERROR: CLINIC-AFTER-RELATE=>Clinic
    [2024-08-11 12:49:42] ERROR: CLINIC-AFTER-RELATE=>Epidemio
    [2024-08-11 12:49:42] ERROR: CLINIC-AFTER-RELATE=>epidemios=>644d7d61248551f34
    [2024-08-11 12:49:42] ERROR: 3-AFTER-RELATE-> DONE
    // why this below is called ?
    [2024-08-11 12:49:42] ERROR: afterUnrelate{"relationName":"epidemios","foreignEntity":{"id":"644d7d61248551f34"},"foreignId":"644d7d61248551f34"}
    [2024-08-11 12:49:42] ERROR: afterUnrelate{"skipHooks":false}
    [2024-08-11 12:49:42] ERROR: CLINIC-AFTER-UN-RELATE=>Clinic
    [2024-08-11 12:49:42] ERROR: CLINIC-AFTER-UN-RELATE=>Epidemio
    [2024-08-11 12:49:42] ERROR: CLINIC-AFTER-UN-RELATE=>epidemios=>644d7d61248551f34​ 
    
    I search but not find. In Database, the record is created in 'epidemios' table but with deleted = true.

    Really strange, i have no formula, or other in these 2 entity

    Thanks
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​
  • a.slyzhko
    Member
    • Oct 2023
    • 90

    #2
    Can you show your entityDefs fields and links both of Clinic and Epidemio?

    Comment

    • item
      Active Community Member
      • Mar 2017
      • 1476

      #3
      Hi,
      nothing special :


      Clinic :
      PHP Code:
             "epidemios": {
                  "type": "linkMultiple",
                  "layoutDetailDisabled": false,
                  "layoutMassUpdateDisabled": false,
                  "layoutListDisabled": false,
                  "noLoad": false,
                  "importDisabled": false,
                  "exportDisabled": false,
                  "customizationDisabled": false,
                  "isCustom": true
              },
              "epidemiosA": {
                  "type": "linkMultiple",
                  "layoutDetailDisabled": false,
                  "layoutMassUpdateDisabled": false,
                  "layoutListDisabled": false,
                  "noLoad": false,
                  "importDisabled": false,
                  "exportDisabled": false,
                  "customizationDisabled": false,
                  "isCustom": true,
                  "view": "custom:views/clinic/fields/epidemio-filtered-link-a"
              },
              "epidemiosB": {
                  "type": "linkMultiple",
                  "layoutDetailDisabled": false,
                  "layoutMassUpdateDisabled": false,
                  "layoutListDisabled": false,
                  "noLoad": false,
                  "importDisabled": false,
                  "exportDisabled": false,
                  "customizationDisabled": false,
                  "isCustom": true,
                  "view": "custom:views/clinic/fields/epidemio-filtered-link-b"
              },
      
      ......
      
              "epidemios": {
                  "type": "hasMany",
                  "relationName": "clinicEpidemio",
                  "foreign": "clinics",
                  "entity": "Epidemio",
                  "audited": false,
                  "columnAttributeMap": {
                    "heading": "epidemioHeading"
                  },
                  "isCustom": true
              },
              "epidemiosA": {
                  "type": "hasMany",
                  "relationName": "clinicEpidemioA",
                  "foreign": "clinicsA",
                  "entity": "Epidemio",
                  "audited": false,
                  "isCustom": true
              },
              "epidemiosB": {
                  "type": "hasMany",
                  "relationName": "clinicEpidemioB",
                  "foreign": "clinicsB",
                  "entity": "Epidemio",
                  "audited": false,
                  "isCustom": true
              },​​ 
      

      Epidemio :

      PHP Code:
              "clinics": {
                  "type": "linkMultiple",
                  "layoutDetailDisabled": true,
                  "layoutMassUpdateDisabled": true,
                  "layoutListDisabled": true,
                  "noLoad": true,
                  "importDisabled": true,
                  "exportDisabled": true,
                  "customizationDisabled": true,
                  "isCustom": true
              },
              "clinicsA": {
                  "type": "linkMultiple",
                  "layoutDetailDisabled": true,
                  "layoutMassUpdateDisabled": true,
                  "layoutListDisabled": true,
                  "noLoad": true,
                  "importDisabled": true,
                  "exportDisabled": true,
                  "customizationDisabled": false,
                  "isCustom": true
              },
              "clinicsB": {
                  "type": "linkMultiple",
                  "layoutDetailDisabled": true,
                  "layoutMassUpdateDisabled": true,
                  "layoutListDisabled": true,
                  "noLoad": true,
                  "importDisabled": true,
                  "exportDisabled": true,
                  "customizationDisabled": true,
                  "isCustom": true
              },....
      
              "clinics": {
                  "type": "hasMany",
                  "relationName": "clinicEpidemio",
                  "foreign": "epidemios",
                  "entity": "Clinic",
                  "audited": false,
                  "isCustom": true
              },
              "clinicsA": {
                  "type": "hasMany",
                  "relationName": "clinicEpidemioA",
                  "foreign": "epidemiosA",
                  "entity": "Clinic",
                  "audited": false,
                  "isCustom": true
              },
              "clinicsB": {
                  "type": "hasMany",
                  "relationName": "clinicEpidemioB",
                  "foreign": "epidemiosB",
                  "entity": "Clinic",
                  "audited": true,
                  "isCustom": true
              },
      And strange, this work perfectly :

      PHP Code:
      
             $clinics = $this->em->getRDBRepository('Clinic')
                  ->where([
                      'type' => 'Epidemio',
                      'status' => 'Close',
                      ])
                  ->find();
              // DH DI DP DM IM IP JM JP MA MR
              $rangeAZ = range('A', 'Z');
              $doubleLettre = ['DH', 'DI', 'DP', 'DM', 'IM', 'IP', 'JM', 'JP', 'MA', 'MR' ];
              $ranges = array_merge($rangeAZ, $doubleLettre);
      
              foreach($clinics as $clinic){
                  foreach ($ranges as $letter){
                  
                      $epidemios = $this->em->getRDBRepository('Clinic')
                          ->getRelation($clinic, 'epidemios' .$letter)
                          ->find();
                      if (!$epidemios) continue;
      
                      foreach($epidemios as $epidemio){
                          $this->em->getRDBRepository('Epidemio')
                              ->getRelation($epidemio, 'clinics')
                              ->relate($clinic);
                      }
                      //$this->log->error( 'epidemios'.$letter .' => ' .$epidemios->count() );
                      
                  }
              }
      :s
      If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

      Comment

      Working...