formula: record create/update support object
This commit is contained in:
@@ -29,13 +29,17 @@
|
||||
|
||||
namespace Espo\Core\Formula\Functions\RecordGroup;
|
||||
|
||||
use Espo\Core\Formula\{
|
||||
Functions\BaseFunction,
|
||||
ArgumentList,
|
||||
};
|
||||
use Espo\Core\Formula\ArgumentList;
|
||||
use Espo\Core\Formula\Exceptions\BadArgumentType;
|
||||
use Espo\Core\Formula\Functions\BaseFunction;
|
||||
|
||||
use Espo\Core\Di;
|
||||
use RuntimeException;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
class CreateType extends BaseFunction implements
|
||||
Di\EntityManagerAware
|
||||
{
|
||||
@@ -49,12 +53,34 @@ class CreateType extends BaseFunction implements
|
||||
|
||||
$args = $this->evaluate($args);
|
||||
|
||||
if (!is_array($args)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
$entityType = $args[0];
|
||||
|
||||
if (!is_string($entityType)) {
|
||||
$this->throwBadArgumentType(1, 'string');
|
||||
}
|
||||
|
||||
$data = $this->getData($args, $entityType);
|
||||
|
||||
$entity = $this->entityManager->createEntity($entityType, $data);
|
||||
|
||||
return $entity->getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, mixed> $args
|
||||
* @return array<string, mixed>
|
||||
* @throws BadArgumentType
|
||||
*/
|
||||
private function getData(array $args, mixed $entityType): array
|
||||
{
|
||||
if (count($args) >= 2 && $args[1] instanceof stdClass) {
|
||||
return get_object_vars($args[1]);
|
||||
}
|
||||
|
||||
$data = [];
|
||||
|
||||
$i = 1;
|
||||
@@ -75,8 +101,6 @@ class CreateType extends BaseFunction implements
|
||||
$i = $i + 2;
|
||||
}
|
||||
|
||||
$entity = $this->entityManager->createEntity($entityType, $data);
|
||||
|
||||
return $entity->getId();
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,13 +29,17 @@
|
||||
|
||||
namespace Espo\Core\Formula\Functions\RecordGroup;
|
||||
|
||||
use Espo\Core\Formula\{
|
||||
Functions\BaseFunction,
|
||||
ArgumentList,
|
||||
};
|
||||
use Espo\Core\Formula\ArgumentList;
|
||||
use Espo\Core\Formula\Exceptions\BadArgumentType;
|
||||
use Espo\Core\Formula\Functions\BaseFunction;
|
||||
|
||||
use Espo\Core\Di;
|
||||
use RuntimeException;
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* @noinspection PhpUnused
|
||||
*/
|
||||
class UpdateType extends BaseFunction implements
|
||||
Di\EntityManagerAware
|
||||
{
|
||||
@@ -49,6 +53,10 @@ class UpdateType extends BaseFunction implements
|
||||
|
||||
$args = $this->evaluate($args);
|
||||
|
||||
if (!is_array($args)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
$entityType = $args[0];
|
||||
$id = $args[1];
|
||||
|
||||
@@ -60,6 +68,32 @@ class UpdateType extends BaseFunction implements
|
||||
$this->throwBadArgumentType(2, 'string');
|
||||
}
|
||||
|
||||
$data = $this->getData($args, $entityType);
|
||||
|
||||
$entity = $this->entityManager->getEntityById($entityType, $id);
|
||||
|
||||
if (!$entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$entity->set($data);
|
||||
|
||||
$this->entityManager->saveEntity($entity);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, mixed> $args
|
||||
* @return array<string, mixed>
|
||||
* @throws BadArgumentType
|
||||
*/
|
||||
private function getData(array $args, mixed $entityType): array
|
||||
{
|
||||
if (count($args) >= 3 && $args[2] instanceof stdClass) {
|
||||
return get_object_vars($args[2]);
|
||||
}
|
||||
|
||||
$data = [];
|
||||
|
||||
$i = 2;
|
||||
@@ -80,17 +114,6 @@ class UpdateType extends BaseFunction implements
|
||||
$i = $i + 2;
|
||||
}
|
||||
|
||||
$em = $this->entityManager;
|
||||
|
||||
$entity = $em->getEntity($entityType, $id);
|
||||
|
||||
if (!$entity) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$entity->set($data);
|
||||
$em->saveEntity($entity);
|
||||
|
||||
return true;
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@ namespace tests\integration\Espo\Core\Formula;
|
||||
|
||||
use Espo\Core\Acl\Table;
|
||||
use Espo\Core\Field\DateTimeOptional;
|
||||
use Espo\Core\Formula\Exceptions\Error;
|
||||
use Espo\Core\Formula\Exceptions\UnsafeFunction;
|
||||
use Espo\Core\Formula\Manager;
|
||||
use Espo\Entities\User;
|
||||
@@ -464,26 +465,67 @@ class FormulaTest extends BaseTestCase
|
||||
$this->assertEquals('1', $result);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Error
|
||||
*/
|
||||
public function testRecordCreateUpdate()
|
||||
{
|
||||
$em = $this->getContainer()->get('entityManager');
|
||||
$fm = $this->getContainer()->get('formulaManager');
|
||||
$em = $this->getEntityManager();
|
||||
$fm = $this->getContainer()->getByClass(Manager::class);
|
||||
|
||||
$script = "record\\create('Meeting', 'name', 'test', 'assignedUserId', '1')";
|
||||
$id = $fm->run($script);
|
||||
|
||||
$this->assertIsString('string', $id);
|
||||
|
||||
$m = $em->getEntity('Meeting', $id);
|
||||
$meeting = $em->getEntityById(Meeting::ENTITY_TYPE, $id);
|
||||
|
||||
$this->assertNotNull($m);
|
||||
$this->assertEquals('1', $m->get('assignedUserId'));
|
||||
$this->assertNotNull($meeting);
|
||||
$this->assertEquals('1', $meeting->get('assignedUserId'));
|
||||
|
||||
$script = "record\\update('Meeting', '{$id}', 'name', 'test-chanhed', 'assignedUserId', '2')";
|
||||
//
|
||||
|
||||
$script = "record\\update('Meeting', '$id', 'name', 'test-changed', 'assignedUserId', '2')";
|
||||
$fm->run($script);
|
||||
|
||||
$m = $em->getEntity('Meeting', $id);
|
||||
$this->assertEquals('2', $m->get('assignedUserId'));
|
||||
$meeting = $em->getEntityById(Meeting::ENTITY_TYPE, $id);
|
||||
$this->assertEquals('2', $meeting->get('assignedUserId'));
|
||||
|
||||
//
|
||||
|
||||
$script = "
|
||||
\$data = object\\create();
|
||||
object\\set(\$data, 'name', 'test-1');
|
||||
|
||||
\$id = record\\create('Meeting', \$data);
|
||||
";
|
||||
|
||||
$vars = (object) [];
|
||||
|
||||
$fm->run($script, null, $vars);
|
||||
|
||||
$id2 = $vars->id;
|
||||
|
||||
$this->assertIsString('string', $id2);
|
||||
|
||||
$meeting = $em->getEntityById(Meeting::ENTITY_TYPE, $id2);
|
||||
|
||||
$this->assertNotNull($meeting);
|
||||
$this->assertEquals('test-1', $meeting->get('name'));
|
||||
|
||||
//
|
||||
|
||||
$script = "
|
||||
\$data = object\\create();
|
||||
object\\set(\$data, 'name', 'test-updated');
|
||||
|
||||
record\\update('Meeting', '$id2', \$data);
|
||||
";
|
||||
|
||||
$fm->run($script);
|
||||
|
||||
$meeting = $em->getEntityById(Meeting::ENTITY_TYPE, $id2);
|
||||
$this->assertEquals('test-updated', $meeting->get('name'));
|
||||
}
|
||||
|
||||
public function testRecordDelete(): void
|
||||
|
||||
Reference in New Issue
Block a user