PHP 8.2-apache (Docker图像)
已启用XDebug Symfony 6.4
Quick summary
I am creating form in Symfony 6.4 Framework and when it is submitted and request is handled, it tries to get value of unset entity, so it could set it. This problem is present only if data_class is specified, otherwise it gives an array without a problem. Mapping to the entity is a problem though.
In more detail个
我正在使用类以标准的symfony方式创建表单
<?php
...
#[ORM\Entity(repositoryClass: UserRepository::class)]
#[UniqueEntity(fields: ['username'], message: 'There is already an account with this username')]
class User extends AbstractADUCEntity implements UserInterface, PasswordAuthenticatedUserInterface
{
use UuidV7;
use Username;
use Password;
use Vocative;
use FullName;
use Email;
public function getRoles(): array
{
return [];
}
public function eraseCredentials(): void {}
public function getUserIdentifier(): string
{
return $this->getUsername();
}
}
在这里,我的php属性始终是属性、getter和setter,例如,使用用户名为类提供以下内容:
<?php
use Doctrine\ORM\Mapping as ORM;
trait Username
{
#[ORM\Column(unique: true)]
private string $username;
public function getUsername(): string
{
return $this->username;
}
public function setUsername(string $username): static
{
$this->username = $username;
return $this;
}
}
然后将该表单制作为一个类
<?php
...
class SignUpType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('firstName', TextType::class, [
'attr' => [
'placeholder' => 'Křestní jméno',
'data-field-name' => 'firstName',
'data-action' => 'keyup->sign-up#processControl'
],
])
...
;
}
In this scenario everything works and the result after sending the form is
此时,我的SecurityController非常简单:
<?php
...
class SecurityController extends AbstractController
{
#[Route('registrace', name: 'sign_up', methods: ['HEAD', 'GET', 'POST'])]
public function signUp(Request $request): Response
{
$form = $this->createForm(SignUpType::class);
$form->handleRequest($request);
dump($form->getData());
if ($form->isSubmitted() && $form->isValid()) {
die();
}
return $this->render('security/sign-up.html.twig', [
'sign_up_form' => $form,
]);
}
}
不过,如果我把代码加到SignUpType
<?php
...
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
User
实体的所有属性都存在此错误.
我试图降低symfony的版本,因为我在开发中使用的是6.4版,但这个问题在稳定的6.3版上也很普遍.
Using xdebug I managed to find the path how the getter is called.
I believe the problem starts within the setValue that tries to getValue before setting the object.
一种显而易见的方法是将所有属性的缺省值设置为空字符串''
,但我不喜欢这种解决方案,因为对象实际上没有初始化,因此我认为它不应该具有非缺省值.
我也试图摆脱这些特点,在另一个实体上这样做,只有一个专栏,由symfony Maker制造.故事是一样的,所以它不是由我用来作为教义实体的trait 系统造成的.
在这里,我堆积如山,不知道如何继续下go .
One interesting behavior个
在发出指定为data_class
的POST请求时,我运行了xdebug,我发现方法handleRequest运行得很顺利,没有任何问题.但是,当我没有在handleRequest
方法中放置断点,而是将断点放在UserName的getter中时,我们可以看到它从handleRequest
更远.在我看来,它似乎有两条不同的路径,一条是当我在进程中看到xdebug时,另一条是当我将断点放到getter中的末尾时.我无法解释这一点.
Edit: If I scroll few kilometers down below those errors, there is actually my dump with good entity. Therefore something else must be going on than creation of this entity.
另外,如果我在没有dd
的情况下运行它,它将出现错误"Headers Are Headers Are Sent".但我不会在其他任何地方使用垃圾场.