Tôi có thực thể Khách hàng và hai quan hệ một-nhiều đối tượng CustomerPhone và CustomerAddress.Bộ sưu tập biểu mẫu Symfony2 không gọi addxxx và removexxx ngay cả khi 'by_reference' => false
Thực thể khách hàng có addPhone/removePhone và addAddress/removeAddress "adders".
Tùy chọn bộ sưu tập CustomerType có 'by_reference' => sai cho cả hai bộ sưu tập.
Chức năng thực thể Thêm/xóaĐiện thoại và thêmThêm/xóaĐịa chỉ không được gọi sau khi biểu mẫu được gửi, vì vậy CustomerPhone và CustomerAddress không có id cha mẹ sau khi vẫn tồn tại.
Tại sao có thể addPhone/removePhone và addAddress/removeAddress không được gọi khi gửi biểu mẫu?
UPD 1.
Sau @Baig suggestion bây giờ tôi có addPhone/removePhone "adders" được gọi, nhưng addAddress/removeAddress không. Không thể hiểu tại sao vì chúng giống hệt nhau.
# 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;
Cùng tập tin "adders"
# 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);
}
Quan hệ:
# 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;
CustomerType dạng:
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. Kiểm tra xem addAddress gọi.
/**
* 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.
CustomerAddressType.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';
}
}
CustomerPhoneType.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';
}
}
Bạn có thể cụ thể hơn về vấn đề bạn đang gặp phải không? Thật khó để nói những gì bạn nghĩ rằng vấn đề là. – kdbanman
@kdbanman chức năng thực thể addPhone/removePhone và addAddress/removeAddress không được gọi sau khi biểu mẫu được gửi. Nó không hoạt động như được mô tả ở đây symfony.com/doc/current/cookbook/form/form_collections.html Vì vậy, CustomerPhone và CustomerAddress không có id cha sau khi vẫn tồn tại. Câu hỏi được cập nhật. –
bạn có thể thử loại bỏ '$ em-> persist ($ customer);' và sau đó thử nó. Tôi cũng đang sử dụng tất cả các loại bộ sưu tập dường như được thiết lập đúng, tôi đang sử dụng 'yml' mặc dù nhưng điều đó không nên tạo sự khác biệt. Bây giờ chỉ cần thử loại bỏ '$ em-> flush();' Tôi cũng không hiểu mục đích của mọi thứ ở trên tạo biểu mẫu của bạn, tôi không nghĩ rằng bạn cần rằng – Baig