PDFTemplate - Grandchild relationship

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • esforim
    Active Community Member
    • Jan 2020
    • 2204

    PDFTemplate - Grandchild relationship

    Reading this thread it seem like it is possible to get the grandchild (not sure if this correct term) of an entity: https://forum.espocrm.com/forum/deve...nd-grand-child

    But that is for Query not for PDF Template Printing.

    Anyway here is an example data:

    PDF Template on Contact
    (For entity Contact)
    Contact Name: Mr John Smith
    Contact Account: EspoCRM
    Account Title: Director

    (For entity Account)
    Account name: EspoCRM
    Relationship: Mr John Smith - Director
    Relationship: Mr Stuart Smith - Engineer
    Relationship: Ms Jane Smith - Finance

    And here is what I'm trying to do.

    When I print John Smith PDF, I also want to know what other contact is in the Account EspoCRM. I tried variation code like so but it doesn't seem to work, only added 3 method below otherwise I tried about 10 variation.

    This would get me the Contact information
    Code:
    {{#each accounts}}
    {{name}} -  {{contactRole}}
    {{/each}}
    Now to try and get "grandchild"

    Test 1
    Code:
    {{#each contacts.accounts}}
    {{name}} -  {{contactRole}}
    {{/each}}
    Test 2
    Code:
    {{#each accounts.contact}}
    {{name}} - {{contactRole}}
    {{/each}}
    Test 3
    Code:
    {{#each accounts}}
    {{name}} - {{contactRole}}
    {{#each contacts.accounts}}
    {{name}} - {{contactRole}}
    {{/each}}
    {{/each}}
    Why not just go into Account and use this code?
    Code:
    {{#each contacts}}
    {{name}} -  {{contactRole}}
    {{/each}}
    Because I want to get Contact information as well. And there is more information I want from Account not just the name and Role.

    If anything not clear please let me know. Appreciate if anyone know on a how-to solve this.
  • item
    Active Community Member
    • Mar 2017
    • 1476

    #2
    Hello,
    look : loadAdditionalFields

    step :
    in service of your entity contact :

    PHP Code:
    
    
    public function getEntitySpecificationList(Entity $entity)
    {
    $pdo = $this->getEntityManager()->getPDO();
    $specificationList = [];
    
    $sql = "
    SELECT DISTINCT `id`, `seconds`, `type`
    FROM `specification`
    WHERE
    `entity_type` = ".$pdo->quote($entity->getEntityType())." AND
    `entity_id` = ".$pdo->quote($entity->id)." AND
    `deleted` = 0
    ORDER BY `seconds` ASC
    ";
    
    $sth = $pdo->prepare($sql);
    $sth->execute();
    $rows = $sth->fetchAll(\PDO::FETCH_ASSOC);
    
    foreach ($rows as $row) {
    $o = new \StdClass();
    $o->id = $row['id'];
    $o->seconds = intval($row['seconds']);
    $o->type = $row['type'];
    $specificationList[] = $o;
    }
    return $specificationList;
    }
    
    public function loadAdditionalFields(Entity $entity)
    {
    parent::loadAdditionalFields($entity);
    $this->loadSpecificationsField($entity);
    }
    
    protected function loadSpecificationsField(Entity $entity)
    {
    $specifications = $this->getRepository()->getEntitySpecificationList($entity);
    $entity->set('specification', $specifications);
    } 
    
    and in your pdf :

    {{#each specification}}

    for your request is no need sql.. you can use orm.
    for info, specification is not a field in entity ..
    If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

    Comment

    • esforim
      Active Community Member
      • Jan 2020
      • 2204

      #3
      Thank you so much item. I gave this a try but failed. I create a Contact.php file with the code.

      File:
      Code:
       /custom/Espo/Custom/Services/Contact.php
      Getting this error on rebuild:
      Code:
      Server side error 500: Class Espo\Custom\Services\Contact does not exist
      As for the code all I did was change the word Specifications to Account

      Code:
      public function getEntityAccountList(Entity $entity)
      {
      $pdo = $this->getEntityManager()->getPDO();
      $accountList = [];
      
      $sql = "
      SELECT DISTINCT `id`, `seconds`, `type`
      FROM `account`
      WHERE
      `entity_type` = ".$pdo->quote($entity->getEntityType())." AND
      `entity_id` = ".$pdo->quote($entity->id)." AND
      `deleted` = 0
      ORDER BY `seconds` ASC
      ";
      
      $sth = $pdo->prepare($sql);
      $sth->execute();
      $rows = $sth->fetchAll(\PDO::FETCH_ASSOC);
      
      foreach ($rows as $row) {
      $o = new \StdClass();
      $o->id = $row['id'];
      $o->seconds = intval($row['seconds']);
      $o->type = $row['type'];
      $accountList[] = $o;
      }
      return $accountList;
      }
      
      public function loadAdditionalFields(Entity $entity)
      {
      parent::loadAdditionalFields($entity);
      $this->loadAccountsField($entity);
      }
      
      protected function loadAccountsField(Entity $entity)
      {
      $specifications = $this->getRepository()->getEntityAccountList($entity);
      $entity->set('account', $accounts);
      }
      Looking at other file in the folder, it seem like I need to do something like this:

      Code:
      <?php
      
      namespace Espo\Custom\Services;
      
      class Contact extends \Espo\Core\Templates\Services\BasePlus
      {
      }
      But this where I have no idea what to do next. Thank you very much.

      Comment


      • item
        item commented
        Editing a comment
        i will try then i come..
    • item
      Active Community Member
      • Mar 2017
      • 1476

      #4
      So this work ... not really good coding .. but you can adapt :

      service/ contact.php

      PHP Code:
      <?php
      namespace Espo\Custom\Services;
      
      use Espo\ORM\{
      Entity,
      EntityManager,
      ServiceFactory,
      };
      
      class Contact extends \Espo\Modules\Crm\Services\Contact
      {
      protected function contactFromParent(Entity $entity)
      {
      $em = $this->getEntityManager();
      $contactId = $entity->id;
      $parentAccountId = $entity->get('accountId');
      
      $account = $em->getEntity('Account', $parentAccountId);
      $contacts = $em->getRepository('Account')
      ->getRelation($account, 'contacts')
      ->order('name')
      ->find();
      
      
      foreach ($contacts as $contact) {
      
      $contactFromParentList[$contact->id]['name'] = $contact->get('name');
      }
      
      
      $parentContactJson = json_decode(json_encode($contactFromParentList, true));
      
      $entity->set('parentContactJson',$parentContactJson );
      }
      public function loadAdditionalFields(Entity $entity)
      {
      parent::loadAdditionalFields($entity);
      $this->contactFromParent($entity);
      
      //$GLOBALS['log']->warning( $entity->get('calls' ) );
      }
      
      public function loadAdditionalFieldsForPdf(Entity $entity)
      {
      $this->loadAdditionalFields($entity);
      
      }
      
      }
      i have forget the cruxial : need to add in metadata a custom field :

      entityDefs/contact :

      important is jsonArray and notStorable .. other i don't know
      PHP Code:
      "parentContactJson": {
      "type": "jsonArray",
      "notStorable": true,
      "layoutDetailDisabled": true,
      "layoutFiltersDisabled": true,
      "layoutListDisabled": true,
      "reportDisabled": true
      } 
      
      en in pdf :

      {{#each parentContactJson}}
      {{name}}

      {{/each}}


      Please choose better "word" for field
      If you could give the project a star on GitHub. EspoCrm believe our work truly deserves more recognition. Thanks.​

      Comment

      Working...