EspoCRM – Open Source CRM Application
PHP calendarcontactscrmcrm-platformcrm-systemcustomer-portalcustomer-supportcustomizabledocumentsemail-marketingkanbanleadsopen-sourcephpplatformsales-automationsingle-page-applicationsupport
Import relate foreign fix
Yurii5c6bf69665b5
diff --git a/application/Espo/Modules/Crm/Entities/Account.php b/application/Espo/Modules/Crm/Entities/Account.phpindex 0045297cfb..50c534d460 100644--- a/application/Espo/Modules/Crm/Entities/Account.php+++ b/application/Espo/Modules/Crm/Entities/Account.php@@ -42,6 +42,11 @@ class Account extends Entity{public const ENTITY_TYPE = 'Account';+ /**+ * @since 10.0.7+ */+ public const string FILED_TYPE = 'type';+public const TYPE_CUSTOMER = 'Customer';public const TYPE_PARTNER = 'Partner';public const TYPE_RESELLER = 'Reseller';@@ -148,4 +153,20 @@ class Account extends Entity{return $this->get('description');}++ /**+ * @since 10.0.7+ */+ public function setType(?string $type): self+ {+ return $this->set(self::FILED_TYPE, $type);+ }++ /**+ * @since 10.0.7+ */+ public function getType(): ?string+ {+ return $this->get(self::FILED_TYPE);+ }}diff --git a/application/Espo/Tools/Import/Import.php b/application/Espo/Tools/Import/Import.phpindex 9b4f0844ff..070b4bcc22 100644--- a/application/Espo/Tools/Import/Import.php+++ b/application/Espo/Tools/Import/Import.php@@ -42,6 +42,7 @@ use Espo\ORM\Defs\Params\AttributeParam;use Espo\ORM\Defs\Params\FieldParam;use Espo\ORM\Defs\Params\RelationParam;use Espo\ORM\Name\Attribute;+use Espo\ORM\Type\AttributeType;use Espo\Tools\Import\Jobs\RunIdle;use Espo\ORM\Entity;use Espo\Core\ORM\Entity as CoreEntity;@@ -1502,13 +1503,26 @@ class Import{$entityType = $this->entityType ?? throw new LogicException();+ $entityDefs = $this->entityManager->getDefs()->getEntity($entityType);+$forbiddenAttributeList =$this->aclManager->getScopeForbiddenAttributeList($this->user, $entityType, Table::ACTION_EDIT);foreach ($attributeList as $k => $attribute) {- if (in_array($attribute, $forbiddenAttributeList)) {- unset($attributeList[$k]);+ if (!in_array($attribute, $forbiddenAttributeList)) {+ continue;}++ $attributeDefs = $entityDefs->tryGetAttribute($attribute);++ if (+ $attributeDefs?->getType() === AttributeType::FOREIGN &&+ $entityDefs->tryGetField($attribute)?->getParam('relateOnImport')+ ) {+ continue;+ }++ unset($attributeList[$k]);}if ($entityType === User::ENTITY_TYPE) {diff --git a/tests/integration/Espo/Tools/Import/ImportTest.php b/tests/integration/Espo/Tools/Import/ImportTest.phpnew file mode 100644index 0000000000..f65b8b583b--- /dev/null+++ b/tests/integration/Espo/Tools/Import/ImportTest.php@@ -0,0 +1,180 @@+<?php+/************************************************************************+ * This file is part of EspoCRM.+ *+ * EspoCRM – Open Source CRM application.+ * Copyright (C) 2014-2026 EspoCRM, Inc.+ * Website: https://www.espocrm.com+ *+ * This program is free software: you can redistribute it and/or modify+ * it under the terms of the GNU Affero General Public License as published by+ * the Free Software Foundation, either version 3 of the License, or+ * (at your option) any later version.+ *+ * This program is distributed in the hope that it will be useful,+ * but WITHOUT ANY WARRANTY; without even the implied warranty of+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the+ * GNU Affero General Public License for more details.+ *+ * You should have received a copy of the GNU Affero General Public License+ * along with this program. If not, see <https://www.gnu.org/licenses/>.+ *+ * The interactive user interfaces in modified source and object code versions+ * of this program must display Appropriate Legal Notices, as required under+ * Section 5 of the GNU Affero General Public License version 3.+ *+ * In accordance with Section 7(b) of the GNU Affero General Public License version 3,+ * these Appropriate Legal Notices must retain the display of the "EspoCRM" word.+ ************************************************************************/++namespace integration\Espo\Tools\Import;++use Espo\Core\Acl\Table;+use Espo\Entities\Attachment;+use Espo\Entities\Role;+use Espo\Modules\Crm\Entities\Account;+use Espo\Modules\Crm\Entities\Contact;+use Espo\Tools\Import\Import;+use Espo\Tools\Import\Params;+use tests\integration\Core\BaseTestCase;++class ImportTest extends BaseTestCase+{+ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ public function testRelateOnImport(): void+ {+ $metadata = $this->getMetadata();++ $metadata->set('entityDefs', Contact::ENTITY_TYPE, [+ 'fields' => [+ 'accountType' => [+ 'relateOnImport' => true,+ ]+ ],+ ]);++ $metadata->save();++ //++ $this->createUser('test', [+ Role::FIELD_DATA => [+ 'Import' => true,+ Account::ENTITY_TYPE => [+ Table::ACTION_READ => Table::LEVEL_ALL,+ ],+ Contact::ENTITY_TYPE => [+ Table::ACTION_CREATE => Table::LEVEL_YES,+ Table::ACTION_READ => Table::LEVEL_ALL,+ Table::ACTION_EDIT => Table::LEVEL_ALL,+ ],+ ],+ ]);++ $this->authenticate('test');++ $em = $this->getEntityManager();++ //++ $attachment = $em->getRDBRepositoryByClass(Attachment::class)->getNew();+ $attachment+ ->setName('test.csv')+ ->setType('text/csv')+ ->setContents(+ <<<'EOT'+ lastName,accountType+ Test,Partner+ ```+ EOT+ );+ $em->saveEntity($attachment);++ $account = $em->getRDBRepositoryByClass(Account::class)->getNew();+ $account->setType(Account::TYPE_PARTNER);+ $em->saveEntity($account);++ $import = $this->getInjectableFactory()->create(Import::class);++ $import+ ->setEntityType(Contact::ENTITY_TYPE)+ ->setParams(+ Params::create()+ )+ ->setAttachmentId($attachment->getId())+ ->setAttributeList(['lastName', 'accountType']);++ $import->run();++ $contact = $em->getRDBRepositoryByClass(Contact::class)+ ->where(['lastName' => 'Test'])+ ->findOne();++ $this->assertNotNull($contact);+ $this->assertEquals($account->getId(), $contact->getAccount()?->getId());+ }++ /**+ * @noinspection PhpUnhandledExceptionInspection+ */+ public function testRelateByForeignName(): void+ {+ $this->createUser('test', [+ Role::FIELD_DATA => [+ 'Import' => true,+ Account::ENTITY_TYPE => [+ Table::ACTION_READ => Table::LEVEL_ALL,+ ],+ Contact::ENTITY_TYPE => [+ Table::ACTION_CREATE => Table::LEVEL_YES,+ Table::ACTION_READ => Table::LEVEL_ALL,+ Table::ACTION_EDIT => Table::LEVEL_ALL,+ ],+ ],+ ]);++ $this->authenticate('test');++ $em = $this->getEntityManager();++ //++ $attachment = $em->getRDBRepositoryByClass(Attachment::class)->getNew();+ $attachment+ ->setName('test.csv')+ ->setType('text/csv')+ ->setContents(+ <<<'EOT'+ lastName,accountName+ Test,Hello+ ```+ EOT+ );+ $em->saveEntity($attachment);++ $account = $em->getRDBRepositoryByClass(Account::class)->getNew();+ $account->setName('Hello');+ $em->saveEntity($account);++ $import = $this->getInjectableFactory()->create(Import::class);++ $import+ ->setEntityType(Contact::ENTITY_TYPE)+ ->setParams(+ Params::create()+ )+ ->setAttachmentId($attachment->getId())+ ->setAttributeList(['lastName', 'accountName']);++ $import->run();++ $contact = $em->getRDBRepositoryByClass(Contact::class)+ ->where(['lastName' => 'Test'])+ ->findOne();++ $this->assertNotNull($contact);+ $this->assertEquals($account->getId(), $contact->getAccount()?->getId());+ }+}