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 sent form

此时,我的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,
    ]);
}

sent form with data_class

User实体的所有属性都存在此错误.

我试图降低symfony的版本,因为我在开发中使用的是6.4版,但这个问题在稳定的6.3版上也很普遍.

Using xdebug I managed to find the path how the getter is called. The Xdebug trace

I believe the problem starts within the setValue that tries to getValue before setting the object. enter image description here

一种显而易见的方法是将所有属性的缺省值设置为空字符串'',但我不喜欢这种解决方案,因为对象实际上没有初始化,因此我认为它不应该具有非缺省值.

我也试图摆脱这些特点,在另一个实体上这样做,只有一个专栏,由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. enter image description here

另外,如果我在没有dd的情况下运行它,它将出现错误"Headers Are Headers Are Sent".但我不会在其他任何地方使用垃圾场.

推荐答案

我已经找到了答案,所以我想让每个人知道,如果朝着同样的方向前进.原因是XDEBUG设置,这是我想象不到的.

我的配置文件是

zend_extension=xdebug

error_reporting=E_NONE
display_errors=off

[xdebug]
xdebug.mode=develop,debug
xdebug.client_host=host.docker.internal
xdebug.client_port=9003
xdebug.idekey="PHPSTORM"
;xdebug.show_error_trace=1
xdebug.start_with_request=yes
xdebug.discover_client_host=1
xdebug.log_level=0

comments 第xebug.show_error_trace=1行导致了这一点.我不知道为什么,但 comments 它是解决方案.XDEBUG仍然可以工作,现在就连symfony也能正常工作了.

Php相关问答推荐

Laravel迁移返回SQL错误,但SQL有效且工作正常

Htaccess-重写对&api.php";的API请求以及对";web.php";的其他请求

在WooCommerce中禁用客户用户角色的纳税计算

PHP -多维数组到一维数组

PHP空数组定义与开始或结束逗号

使注册字段UserMeta值对于WooCommerce中的每个用户是唯一的

如何在laravel中使用script type= text/html/script

创建一个Woocommerce我的帐户订单页面,仅显示已完成的订单

PHP -流加密启用服务器切断字节

使用HPOS过滤WooCommerce中的订单列表

从WooCommerce管理订单汇总中隐藏原始小计行

为什么PHP PDO忽略NOT NULL约束?

使用 PHP,我可以将 foreach 语句放入瞬态中吗?

为什么从一个页面到另一个页面都找不到 JQuery,因为它们使用相同的 twig 模板?

PHP访问子数组列表出错结果和长度

PHP 解析 XML 以获取多个深度的所有字符串值

从图像URL中获取文件扩展名?

为什么foreach循环会输出每个结果?

如何通过额外的属性和本地化来使用 laravel 枚举

如何从 PHP 中的字符串位置排除图像扩展名