Announcement

Collapse
No announcement yet.

How to get accountsColumns in ORM?

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

  • How to get accountsColumns in ORM?


    Hello! I need to change property isInactive for all contacts of account with status liquidated. On request espo/api/v1/Contact/id, I get a contact and it has property accountsColumns :
    Click image for larger version  Name:	accountColumns.png Views:	1 Size:	11.6 KB ID:	45872
    But in ORM I can't get this property.
    My code:
    PHP Code:
    <?php

    namespace Espo\Custom\Hooks\Account;

    use 
    Espo\ORM\Entity;

    class 
    LiquidationAccount extends \Espo\Core\Hooks\Base
    {    
        public function 
    afterSave(Entity $entity, array $options = array())
        {    

            
    $GLOBALS['log']->warning('LiquidationAccount - хук работает');
            
    # если статус организации ликвидирована
            
    if ($entity->get('orgStatus')=='liquidated') {

                
    $entityManager $this->getEntityManager();

                
    # получаем id организации
                
    $account_id $entity->get('id');

                
    # получаем все персоны имеющие связь с данной организацией
                
    $contacts_list $entityManager-> getRepository('Account')->findRelated($entity'contacts');

                
    # перебираем все персоны
                
    if (!empty($contacts_list)){
                    foreach(
    $contacts_list as $contact){
                        
    $contact->loadLinkMultipleField('accounts');
                        if(
    $contact->get('accountsColumns')->$account_id-> isInactive != true){                                                //works always
                            
    $GLOBALS['log']->warning(json_encode($contact->toArray()));                                                         //no accountsColumns
                            
    $accountsColumns = new \stdClass();
                            
    $accountsColumns->$account_id = new \stdClass();
                            
    $accountsColumns->$account_id-> isInactive true;
                            
    $paramsContact["accountsColumns"] = $accountsColumns;    
                            
    $contact->set($paramsContact);                    
                            
    # сохраняем изменения предотвращая запуск рабочих потоков и хуков
                            
    $entityManager->saveEntity($contact, ['skipWorkflow' => true'skipHooks' => true]);
                        }
                    }
                }
            }
        }
    }
    The hook works, but it change the isInactive property for all contacts, regardless of the current value of this property.
    Last edited by Kirill; 02-26-2019, 10:10 AM.

  • #2
    1.

    PHP Code:
     $contactList $entityManager->getRepository('Account')->findRelated($entity'contacts', [
        
    'additionalColumns' => [
             
    'role' => 'accountRole'
        
    ]
    ]);

    foreach (
    $contactList as $contact) {
        
    $role $contact->get('accountRole');


    2.

    PHP Code:

    $contact
    ->loadLinkMultipleField('accounts', [
        
    'role' => 'contactRole'
    ]); 

    I didn't test. Not sure whether snippets are correct.

    Comment


    • #3

      The second works, thanks!!!
      Last edited by Kirill; 02-26-2019, 10:09 AM.

      Comment

      Working...
      X