Php 西尔尤斯:需要注册地址吗

Php 西尔尤斯:需要注册地址吗,php,symfony,symfony-forms,sylius,Php,Symfony,Symfony Forms,Sylius,我希望客户在注册时填写他们的地址,而不是在第一批订单结账时填写。 我明白了,Sylius\Component\Core\Model\Customer具有属性$defaultAddress:AddressInterface和$addresses:Collection | AddressInterface[] 这就是我结巴的地方。如果有单数($address:AddressInterface)我会知道,我应该扩展表单类型并在其中添加地址字段 但如何要求用户在这个集合中只填写一个地址呢 我试过这个:

我希望客户在注册时填写他们的地址,而不是在第一批订单结账时填写。

我明白了,
Sylius\Component\Core\Model\Customer
具有属性
$defaultAddress:AddressInterface
$addresses:Collection | AddressInterface[]

这就是我结巴的地方。如果有单数(
$address:AddressInterface
)我会知道,我应该扩展表单类型并在其中添加地址字段

但如何要求用户在这个集合中只填写一个地址呢

我试过这个: 用于注册的我的表单类型(其中父项为
Sylius\Bundle\CoreBundle\form\type\Customer\CustomerRegistrationType
):

->添加(
"地址",,
CollectionType::类,
[
“条目类型”=>批发商CustomerAddressType::class
]
)

其中,批发商CustomerAddressType是Sylius\Bundle\AddressingBundle\Form\Type\AddressType的子项,并删除一些字段(姓名、电话等)

工作原理: 不呈现注册页面上的地址字段(仅显示空div)。当我转储
form.address
时,我看到它没有子对象

它仅呈现以下内容:

工作原理: 表单应该只呈现一个地址的字段。单击“提交”后,应注册该用户,该用户应有一个地址,并且该地址也应为
$defaultAddress


我发现问题在于,注册时地址的集合是空的。如何在Sylius中添加第一张唱片?

可能会有点晚,但如果其他人偶然发现了你的问题,我就是这样解决的

正如您自己指出的,有一个属性
$defaultAddress:AddressInterface
。只需将此字段添加到自定义表单中,或者在我的情况下添加表单扩展名,并指定适当的类型。我使用了
Sylius\Bundle\AddressingBundle\Form\Type\AddressType
,就是这样

我的表格分机:

<?php

namespace App\Form\Extension;

use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Sylius\Bundle\AddressingBundle\Form\Type\AddressType;
use Sylius\Bundle\CoreBundle\Form\Type\Customer\CustomerRegistrationType;

final class CustomerRegistrationTypeExtension extends AbstractTypeExtension
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        // Adding the defaultAddress as extra fields to the form        
        $builder->add('defaultAddress', AddressType::class, [
            'label' => 'sylius.form.customer.default_address',
        ]);

    }

    /**
     * {@inheritdoc}
     */
    public function getExtendedTypes(): array
    {
        return [CustomerRegistrationType::class];
    }
}
不要忘记在巡更模板中渲染新字段:

{{ form_row(form.defaultAddress) }}

您是否在指定的细枝模板中添加了
{{form_row(form.addresses)}}
,该表单在其中呈现?实际上我尝试过,但忘了提到,它呈现了一个空div。当我转储form.addresses时,我可以看到它没有子级。我更新了我的问题以包含此信息和呈现的HTML代码。
{{ form_row(form.defaultAddress) }}