实体字段 nullable=true,但不能为 null。(交响乐)

Entity field nullable=true but can not be null. (Symfony)

提问人:mco 提问时间:12/7/2022 最后编辑:mco 更新时间:12/9/2022 访问量:326

问:

我有一些实体,当我找到每个实体的交互 (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,
    ]);
}

我无法将其作为值,因此我尝试创建所需的实体,添加日志,然后删除实体,但它也不起作用。它说我不能在子行之前删除父行(我有一个组,一个客户端的子行),但是如果我删除该组,然后删除客户端,我仍然遇到此错误。这就是为什么我只想在一开始就给出一个,这样我就不需要创建和删除无用的实体。nullnull

我的函数如下所示:

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 组名”

php symfony doctrine-orm null 学说

评论

0赞 Dylan KAS 12/7/2022
你能给我们看看你的吸气剂吗?$client
0赞 mco 12/7/2022
如果您发表评论,我不知道 SO 是否会提醒您新的答案,所以我在这里回答是为了通知您:)
0赞 Maniax 12/8/2022
你可以@username通知某人有关更新的信息,你应该用你作为答案的信息更新帖子并删除答案,它会更清楚。想象一下,有几个赞成票的回答,你的回答会有很多关于这个问题的信息,这不是一个好的做法。话虽如此,您能否添加(作为编辑)抛出错误的位置?看起来更像是控制器问题而不是实体问题,但很难说它是什么。
0赞 mco 12/8/2022
好的,TY @Maniax我将更新我的帖子:)
0赞 Maniax 12/8/2022
更好的:)那么函数createANewLog抛出错误了吗?您是否接受那里的客户端参数为 null?

答: 暂无答案