提问人:mco 提问时间:12/7/2022 最后编辑:mco 更新时间:12/9/2022 访问量:326
实体字段 nullable=true,但不能为 null。(交响乐)
Entity field nullable=true but can not be null. (Symfony)
问:
我有一些实体,当我找到每个实体的交互 (CRUD) 时,我会将操作存储在 Logs 实体中。 My Logs 实体具有其他一些实体的 FK,这些 FK 字段可以为 null。 当我创建新日志时,它说:
预期类型为“App\Entity\Client”。找到“null”。
为了精确起见,没有日志部分,一切都可以完美运行,所以我需要修复它以获得我想要的结果。
我的日志实体在我收到错误的地方:
#[ORM\ManyToOne(inversedBy: 'logs', cascade: ['persist'])]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?Client $client = null;
我的客户实体:
#[ORM\OneToMany(mappedBy: 'client', targetEntity: Logs::class, orphanRemoval:false, cascade: ['persist'])]
private Collection $logs;
PS:我希望日志在删除它所引用的内容时不会以级联方式删除。
编辑:
在我的组实体中:
public function getClient(): ?Client
{
return $this->client;
}
在我的客户实体中:
/**
* @return Collection<int, Group>
*/
public function getGroups(): Collection
{
return $this->groups;
}
这是我收到错误的地方:
#[Route('/new', name: 'app_user_new', methods: ['GET', 'POST'])]
public function new(Request $request, UserPasswordHasherInterface $userPasswordHasher, UserRepository $userRepository, LogsRepository $logsRepository, GroupRepository $groupRepository, SiteRepository $siteRepository, ClientRepository $clientRepository): Response
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// encode the plain password
$user->setPassword(
$userPasswordHasher->hashPassword(
$user,
$form->get('password')->getData()
)
);
$userRepository->save($user, true);
// take all needed information
$now = new DateTime('now');
$action = 'Create "'.$user->getMail().'" user';
$result = 'SUCCESS';
$gw_interaction = 'none';
$client = $clientRepository->clientToCreateAndDelete($clientRepository);
$group = $groupRepository->groupToCreateAndDelete($client, $groupRepository);
$site = $siteRepository->siteToCreateAndDelete($group, $siteRepository);
// write this action on the logs
$logsRepository->createANewLog($now, $action, $result, $gw_interaction, $user, $client, $site);
// delete the factice client
// $groupRepository->remove($group, true);
$clientRepository->remove($client, true);
return $this->redirectToRoute('app_user_index', [], Response::HTTP_SEE_OTHER);
}
return $this->renderForm('user/new.html.twig', [
'user' => $user,
'form' => $form,
]);
}
我无法将其作为值,因此我尝试创建所需的实体,添加日志,然后删除实体,但它也不起作用。它说我不能在子行之前删除父行(我有一个组,一个客户端的子行),但是如果我删除该组,然后删除客户端,我仍然遇到此错误。这就是为什么我只想在一开始就给出一个,这样我就不需要创建和删除无用的实体。null
null
我的函数如下所示:
public function createANewLog(DateTime $now, String $action, String $result, String $gw_interaction, User $user)
{
$logs = new Logs();
$logs->setDatetime($now);
$logs->setAction($action);
$logs->setResultat($result);
$logs->setGwInteraction($gw_interaction);
$logs->setHisUser($user);
$this->save($logs, true);
}
一种可能的解决方案:
因为我无法解决这个问题,所以我找到了另一种方法来保存我的价值观。现在,我删除了日志中的客户端和站点列:
->当我需要它们为空时,我什么都不做。
->当我需要它们具有值时,我在操作中编写了值:
-> 之前操作具有例如:“创建站点 siteName”
-> now 操作例如:“从组创建站点 siteName 组名”
答: 暂无答案
评论
$client