I want the child data to be automatically deleted when the parent data is deleted. Since there is no "After record deleted" option in Workflows, how should I proceed?
//获取请求明细ID $relatedRecordIds = requestlistsIds; output\printLine($relatedRecordIds); //循环删除请求明细记录 $i = 0; while ($i < array\length($relatedRecordIds)) { output\printLine(array\at($relatedRecordIds, $i)); $relatedId = array\at($relatedRecordIds, $i); if ($relatedId) { record\delete('Requestlist', $relatedId); } $i = $i + 1; }
<?php
namespace Espo\Custom\Hooks\YourEntity; // Replace YourEntity with the correct entity name
use Espo\Core\Hook\Hook\AfterRemove;
use Espo\Custom\Entities\RequestList; // Replace with correct name
use Espo\Modules\Crm\Entities\YourEntity; // Replace with correct name Assuming the entity is a crm entity if it is a custom entity then adjust the path Espo\Custom\Entities\YourEntity
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Repository\Option\RemoveOptions;
/**
* @implements AfterRemove<YourEntity> // Replace YourEntity with the correct entity name
*/
class DeleteRequestList implements AfterRemove
{
public function __construct(private EntityManager $entityManager) {}
public function afterRemove(Entity $entity, RemoveOptions $options): void
{
$deleteQuery = $this->entityManager
->getQueryBuilder()
->delete()
->from(RequestList::ENTITY_TYPE)
->where(['yourLinkId' => $entity->getId()]) // replace yourLinkId with the foreign link Id name
->build();
$this->entityManager->getQueryExecutor()->execute($deleteQuery);
}
}
<?php
namespace Espo\Custom\Hooks\RequestManagement;
use Espo\Core\Hook\Hook\AfterRemove;
use Espo\Custom\Entities\Requestlist;
use Espo\Custom\Entities\RequestManagement;
use Espo\ORM\Entity;
use Espo\ORM\EntityManager;
use Espo\ORM\Repository\Option\RemoveOptions;
/**
* @implements AfterRemove<RequestManagement>
*/
class DeleteRequestList implements AfterRemove
{
private EntityManager $entityManager;
public function __construct(EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function afterRemove(Entity $entity, RemoveOptions $options): void
{
$deleteQuery = $this->entityManager
->getQueryBuilder()
->delete()
->from(Requestlist::ENTITY_TYPE)
->where(['requestManagementId' => $entity->getId()]) // 替换为 Requestlist 实体中关联的外键字段名
->build();
$this->entityManager->getQueryExecutor()->execute($deleteQuery);
}
}
<?php
namespace Espo\Custom\Entities;
class RequestManagement extends \Espo\Core\Templates\Entities\Base
{
public const ENTITY_TYPE = 'RequestManagement';
protected $entityType = 'RequestManagement';
}
<?php
namespace Espo\Custom\Entities;
class Requestlist extends \Espo\Core\Templates\Entities\Base
{
public const ENTITY_TYPE = 'Requestlist';
protected $entityType = 'Requestlist';
}
Comment