Method beforeRelate and afterRelate in Hooks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • VladZP
    Junior Member
    • Mar 2023
    • 6

    Method beforeRelate and afterRelate in Hooks

    Hi there! I use validation in my custom Hooks and it works good, but I can't see changes with my relation Many-to-Many. I have alredy read about methods afterRelate and beforeRelate. Method beforeRelate doesn't work in my Hook =( Method afterRelate gave me 500 error (second argument must be string, array given) but I just only write function like beforeSave and don't pass any arrays.
    My code in Hooks:
    PHP Code:
    public function afterRelate(Entity $entity, string $relationName, Entity $foreignEntity, array $options = [])
    {
    $this->throwException('test', 'test');
    }
    
    private function throwException($label, $log)
    {
    $body = Body::create()
    ->withMessageTranslation($label, null)
    ->encode();
    throw Error::createWithBody($log, $body);
    }​​ 
    
    What I should to do for beforeRelate or afterRelate methods?
  • yuri
    Member
    • Mar 2014
    • 8455

    #2
    Hi,

    Not an answer, but worth to mention that it's not good to use entity hooks for validation and error response throwing.

    Use record hooks instead: https://docs.espocrm.com/development...kclassnamelist
    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

    • telecastg
      Active Community Member
      • Jun 2018
      • 907

      #3
      Hello yuri, do you think that you could move this thread to the parent Developer forum ?. I think that this sub-forum should be used strictly for postings that provide examples and guidance, questions should be posted somewhere else in my opinion.
      Last edited by telecastg; 04-15-2023, 05:02 AM.

      Comment

      Working...