From e7ab75ec5aee7b04aa70b6256404c1ab7ec47355 Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sun, 23 Jun 2024 10:47:07 +0300 Subject: [PATCH] link-one fix --- application/Espo/Core/Record/Service.php | 8 +++ tests/integration/Espo/Record/LinkTest.php | 61 ++++++++++++++++++++++ 2 files changed, 69 insertions(+) diff --git a/application/Espo/Core/Record/Service.php b/application/Espo/Core/Record/Service.php index 8b92255dd8..56fa1d9c99 100644 --- a/application/Espo/Core/Record/Service.php +++ b/application/Espo/Core/Record/Service.php @@ -603,6 +603,14 @@ class Service implements Crud, continue; } + if ( + // link-one + $attributeDefs->getType() === AttributeType::FOREIGN && + $attributeDefs->getParam('attributeRole') === 'id' + ) { + continue; + } + $attribute = $attributeDefs->getName(); unset($data->$attribute); diff --git a/tests/integration/Espo/Record/LinkTest.php b/tests/integration/Espo/Record/LinkTest.php index e8f40aed64..deebfd1145 100644 --- a/tests/integration/Espo/Record/LinkTest.php +++ b/tests/integration/Espo/Record/LinkTest.php @@ -31,6 +31,7 @@ namespace tests\integration\Espo\Record; use Espo\Core\Exceptions\Forbidden; use Espo\Core\Field\Date; +use Espo\Core\ORM\Type\FieldType; use Espo\Core\Record\CreateParams; use Espo\Core\Record\ServiceContainer; use Espo\Core\Record\UpdateParams; @@ -43,6 +44,7 @@ use Espo\Modules\Crm\Entities\Lead; use Espo\Modules\Crm\Entities\Opportunity; use Espo\Modules\Crm\Entities\Task; use Espo\ORM\EntityManager; +use Espo\ORM\Type\RelationType; use tests\integration\Core\BaseTestCase; class LinkTest extends BaseTestCase @@ -439,4 +441,63 @@ class LinkTest extends BaseTestCase $case->get('contactsNames') ); } + + public function testOneToOne(): void + { + $metadata = $this->getContainer()->getByClass(Metadata::class); + + $metadata->set('entityDefs', CaseObj::ENTITY_TYPE, [ + 'links' => [ + 'task' => [ + 'foreign' => 'case', + 'type' => RelationType::BELONGS_TO, + 'entity' => Task::ENTITY_TYPE, + ], + ] + ]); + + $metadata->set('entityDefs', Task::ENTITY_TYPE, [ + 'fields' => [ + 'case' => [ + 'type' => FieldType::LINK_ONE, + ], + ], + 'links' => [ + 'case' => [ + 'foreign' => 'task', + 'type' => RelationType::HAS_ONE, + 'entity' => CaseObj::ENTITY_TYPE, + ], + ] + ]); + + $metadata->save(); + + /** @noinspection PhpUnhandledExceptionInspection */ + $this->getDataManager()->rebuild(); + $this->reCreateApplication(); + + $em = $this->getEntityManager(); + + $task = $em->getRDBRepositoryByClass(Task::class)->getNew(); + $task->setMultiple(['name' => 'Task']); + $em->saveEntity($task); + + $case = $em->getRDBRepositoryByClass(CaseObj::class)->getNew(); + $case->setMultiple(['name' => 'Case']); + $em->saveEntity($case); + + $service = $this->getContainer()->getByClass(ServiceContainer::class)->getByClass(Task::class); + + /** @noinspection PhpUnhandledExceptionInspection */ + $service->update($task->getId(), (object) [ + 'caseId' => $case->getId(), + ], UpdateParams::create()); + + $this->assertTrue( + $em->getRDBRepositoryByClass(Task::class) + ->getRelation($task, 'case') + ->isRelatedById($case->getId()) + ); + } }