gpt4 book ai didi

forms - Symfony2 表单集合即使 'by_reference' => false 也不调用 addxxx 和 removexxx

转载 作者:行者123 更新时间:2023-12-04 17:33:35 25 4
gpt4 key购买 nike

我有 Customer 实体和两个一对多关系 CustomerPhone 和 CustomerAddress。

Customer 实体具有 addPhone/removePhone 和 addAddress/removeAddress “adders”。

CustomerType 集合选项对于两个集合都具有 'by_reference' => false。

实体函数 addPhone/removePhone 和 addAddress/removeAddress 在表单提交后没有被调用,所以 CustomerPhone 和 CustomerAddress 在持久化后没有父 id。

为什么不能在表单提交时调用 addPhone/removePhone 和 addAddress/removeAddress?

UPD 1。

@Baig 之后 suggestion现在我调用了 addPhone/removePhone“adders”,但没有调用 addAddress/removeAddress。不明白为什么,因为它们是相同的。

 # TestCustomerBundle/Entity/Customer.php

/**
* @var string
*
* @ORM\OneToMany(targetEntity="CustomerPhone", mappedBy="customerId", cascade={"persist"}, orphanRemoval=true)
*/
private $phone;

/**
* @var string
*
* @ORM\OneToMany(targetEntity="CustomerAddress", mappedBy="customerId", cascade={"persist"}, orphanRemoval=true)
*/
private $address;

相同的文件“加法器”
# TestCustomerBundle/Entity/Customer.php
/**
* Add customer phone.
*
* @param Phone $phone
*/
public function addPhone(CustomerPhone $phone) {
$phone->setCustomerId($this);
$this->phone->add($phone);

return $this;
}

/**
* Remove customer phone.
*
* @param Phone $phone customer phone
*/
public function removePhone(CustomerPhone $phone) {
$this->phone->remove($phone);
}
/**
* Add customer address.
*
* @param Address $address
*/
public function addAddress(CustomerAddress $address) {
$address->setCustomerId($this);
$this->address->add($address);

return $this;
}

/**
* Remove customer address.
*
* @param Address $address customer address
*/
public function removeAddress(CustomerAddress $address) {
$this->address->remove($address);
}

关系:
# TestCustomerBundle/Entity/CustomerPhone.php
/**
* @ORM\ManyToOne(targetEntity="Customer", inversedBy="phone")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
**/
private $customerId;

#TestCustomerBundle/Entity/CustomerAddress.php
/**
* @ORM\ManyToOne(targetEntity="Customer", inversedBy="address")
* @ORM\JoinColumn(name="customer_id", referencedColumnName="id")
**/
private $customerId;

客户类型表格:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('phone', 'collection', array(
'type' => new CustomerPhoneType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'options' => array('label' => false)
))
->add('address', 'collection', array(
'type' => new CustomerAddressType(),
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'options' => array('label' => false)
))
->add('submit', 'submit')
;
}

Controller 。
# TestCustomerBundle/Controller/DefaultController.php

public function newAction(Request $request)
{
$customer = new Customer();
// Create form.
$form = $this->createForm(new CustomerType(), $customer);
// Handle form to store customer obect with doctrine.
if ($request->getMethod() == 'POST')
{
$form->bind($request);
if ($form->isValid())
{
/*$em = $this->get('doctrine')->getEntityManager();
$em->persist($customer);
$em->flush();*/
$request->getSession()->getFlashBag()->add('success', 'New customer added');
}
}
// Display form.
return $this->render('DeliveryCrmBundle:Default:customer_form.html.twig', array(
'form' => $form->createView()
));
}

UPD 2。
测试是否调用了 addAddress。
/**
* Add customer address.
*
* @param Address $address
*/
public function addAddress(Address $address) {
jkkh; // Test for error if method called. Nothing throws.
$address->setCustomerId($this);
$this->address->add($address);
}

UPD 3。

客户地址类型.php
<?php

namespace Delivery\CrmBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CustomerAddressType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('street')
->add('house')
->add('building', 'text', ['required' => false])
->add('flat', 'text', ['required' => false])
;
}

/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Delivery\CrmBundle\Entity\CustomerAddress'
));
}

/**
* @return string
*/
public function getName()
{
return 'delivery_crmbundle_customeraddress';
}
}

客户电话类型.php
<?php

namespace Delivery\CrmBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class CustomerPhoneType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('number')
;
}

/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Delivery\CrmBundle\Entity\CustomerPhone'
));
}

/**
* @return string
*/
public function getName()
{
return 'phone';
}
}

最佳答案

对我来说,这最终通过添加 getXXX 解决了。 ,它将集合返回到 PropertyAccessor .没有那个你会一直想知道为什么addXXXremoveXXX不叫。

所以请确保:

  • 选项 by_reference设置为 false在现场,
  • 您拥有 adderremover关系拥有方的方法,
  • getter可通过 PropertyAccessor 访问检查是否 by_reference可以用,
  • 如果您想使用 prototype要通过 Javascript 处理添加/删除,请确保 allow_add设置为 true .
  • 关于forms - Symfony2 表单集合即使 'by_reference' => false 也不调用 addxxx 和 removexxx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33358989/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com