contact: relate with account if related through accounts

This commit is contained in:
Yurii
2026-03-03 13:34:55 +02:00
parent d2f51066c0
commit c52846f480
2 changed files with 31 additions and 2 deletions
@@ -44,6 +44,8 @@ class Contact extends Person
public const string ATTR_ACCOUNT_ID = 'accountId';
/** @since v9.3.0. */
public const string FIELD_ACCOUNTS = 'accounts';
/** @since v9.3.0. */
public const string FIELD_ACCOUNT = 'account';
/** @since v9.3.0. */
public const string RELATIONSHIP_ACCOUNT_CONTACT = 'AccountContact';
@@ -31,19 +31,22 @@ namespace Espo\Modules\Crm\Hooks\Contact;
use Espo\Core\Field\Link;
use Espo\Core\Field\LinkMultipleItem;
use Espo\Core\Hook\Hook\AfterRelate;
use Espo\Core\Hook\Hook\AfterSave;
use Espo\Core\Hook\Hook\BeforeSave;
use Espo\Modules\Crm\Entities\Contact;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Name\Attribute;
use Espo\ORM\Repository\Option\RelateOptions;
use Espo\ORM\Repository\Option\SaveOptions;
/**
* @implements BeforeSave<Contact>
* @implements AfterSave<Contact>
* @implements AfterRelate<Contact>
*/
class Accounts implements BeforeSave, AfterSave
class Accounts implements BeforeSave, AfterSave, AfterRelate
{
private const string COLUMN_ROLE = Contact::COLUMN_ACCOUNTS_ROLE;
private const string ATTR_TITLE = 'title';
@@ -54,7 +57,6 @@ class Accounts implements BeforeSave, AfterSave
{
$this->setPrimary($entity);
$this->restoreAccounts($entity);
}
public function afterSave(Entity $entity, SaveOptions $options): void
@@ -146,4 +148,29 @@ class Accounts implements BeforeSave, AfterSave
$entity->setAccounts($accounts);
}
public function afterRelate(
Entity $entity,
string $relationName,
Entity $relatedEntity,
array $columnData,
RelateOptions $options,
): void {
if ($relationName !== Contact::FIELD_ACCOUNTS) {
return;
}
if ($entity->getAccount()) {
return;
}
$relation = $this->entityManager->getRelation($entity, Contact::FIELD_ACCOUNT);
if ($relation->findOne()) {
return;
}
$relation->relate($relatedEntity);
}
}