【问题标题】:Symfony form not set entity relationSymfony 表单未设置实体关系
【发布时间】:2017-01-11 14:36:23
【问题描述】:

我有实体 OutboundInvoice for OneToMany 到实体 OutboundInvoiceRow 我使用 CollectionType 为 OutboundInvoice 创建表单,当我在我的数据库中添加一些 OutboundInvoiceRow 时,在 OutboundInvoiceRow 中看不到 invoice_id,但是当在调试中看到 $form->handleRequest($request); 时,我看到带有集合 OutboundInvoiceRow 的 OutboundInvoice,在里面看到我的 OutboundInvoiceRow。我尝试调试而不是在函数 addRows 或 addRow 中的实体 OutboundInvoice 中输入。现在我有带有集合 OutboundInvoiceRow 的实体 OutboundInvoice,但在没有 OutboundInvoice 的情况下刷新 DB OutboundInvoiceRow 之后。在哪里将 OutboundInvoice 设置为 OutboundInvoiceRow 或使用表单有多正确???

class OutboundInvoice
{
//
    /**
 * @var \OutboundInvoiceRow
 *
 * @ORM\OneToMany(targetEntity="OutboundInvoiceRow", mappedBy="invoice", cascade={"persist"})
 */
private $rows;
//
    /**
 * Add row
 *
 * @param \AppBundle\Entity\OutboundInvoiceRow $row
 *
 * @return OutboundInvoice
 */
public function addRow(\AppBundle\Entity\OutboundInvoiceRow $row)
{
    $this->rows[] = $row;

    $row->setInvoice($this);

    return $this;
}

/**
 * @param OutboundInvoiceRow[] $rows
 * @return $this
 */
public function addRows($rows)
{
    foreach ($rows as $row) {
        if ($row instanceof OutboundInvoiceRow) {
            $this->addRow($row);   
        }
    }

    return $this;
}

我的实体 OutboundInvoiceRow

class OutboundInvoiceRow
{
    /**
 * @var \OutboundInvoice
 *
 * @ORM\ManyToOne(targetEntity="OutboundInvoice", inversedBy="rows", cascade={"persist"})
 * @ORM\JoinColumns({
 *   @ORM\JoinColumn(name="invoice_id", referencedColumnName="id", onDelete="CASCADE")
 * })
 */
private $invoice;
//
    /**
 * Set invoice
 *
 * @param \AppBundle\Entity\OutboundInvoice $invoice
 *
 * @return OutboundInvoiceRow
 */
public function setInvoice(\AppBundle\Entity\OutboundInvoice $invoice = null)
{
    $this->invoice = $invoice;

    return $this;
}

我的行动

    public function createAction(Request $request)
{
    $entity = new OutboundInvoice();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('new_outbound_invoices'));
    }

    return array(
        'entity' => $entity,
        'form' => $form->createView(),
    );
}

    private function createCreateForm(OutboundInvoice $entity)
{
    $form = $this->createForm(new OutboundInvoiceForm(), $entity, array(
        'validation_groups' => ['post_out_bound_invoice'],
        'cascade_validation' => true,
        'action' => $this->generateUrl('outbound_invoices_create'),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;
}

我的表格

class OutboundInvoiceForm extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
////
        ->add('rows', CollectionType::class, array(
            'entry_type' => OutBoundInvoiceRowType::class,
            'allow_add'    => true,
            'allow_delete' => true,
            'prototype' => true,
            'prototype_name' => 'rows__name__',
            'error_bubbling' => true
        ))
    ;
}

/**
 * @param OptionsResolverInterface $resolver
 */
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => OutboundInvoice::class,
        'csrf_protection' => false,
    ));
}

这是我在提交和有效表单后调试时的实体

$entity = {AppBundle\Entity\OutboundInvoice} [27]
id = null
invoiceNumber = null
receiver = null
streetAddress = null
postal = null
postOffice = null
invoicingEmail = null
edi = null
dueDate = null
reversedVat = true
invoiceDate = null
referenceNumber = null
customerReference = "qqqq"
companyReference = "qqqq"
message = "ddd"
notes = "dddd"
termsOfPayment = 22
status = "draft"
currencyCode = "EUR"
languageCode = "FI"
netvisorId = null
crmCustomer = null
customer = {AppBundle\Entity\Customer} [17]
inboundInvoice = null
invoicingType = null
serviceCompany = null
rows = {Doctrine\Common\Collections\ArrayCollection} [1]
 elements = {array} [1]
  rows0 = {AppBundle\Entity\OutboundInvoiceRow} [13]
   id = null
   description = "qqq"
   unitPrice = "22"
   amount = "22"
   vat = 3
   unit = "22"
   accountingAccount = null
   contract = null
   crmContract = null
   costObject = null
   taskExecution = null
   invoice = null //why not out bound invoice id ?? after flush still empty 
   location = {AppBundle\Entity\Location} [38]

【问题讨论】:

    标签: php forms symfony


    【解决方案1】:

    'type' => new OutBoundInvoiceRowType() 错了,你应该改用entry_type 选项

    您的实体中没有 removeRow 方法

    Doctrine 将只检查拥有方的级联操作,因此您不必在 OutboundInvoiceRow 实体中声明级联操作

    并将'by_reference' => false 添加到您的表单(在收集选项中)。

    【讨论】:

    • 我尝试了`'entry_type' => OutBoundInvoiceRowType::class,`但仍然没有设置发票到行。我有removeRow 更新我的问题,但是当我们创建实体时它会影响什么。我在实体中有cascade={"persist"}
    • 哦,是的,在您的表单中添加 'by_reference' => false。它将显式调用 addRow 方法。我的意思是你不需要在你的OutboundInvoiceRow 实体中保留cascade 设置。看看docs.doctrine-project.org/projects/doctrine-orm/en/latest/… 可能你需要在OutboudInvoice 实体中将 $rows 初始化为空的 ArrayCollection() 对象。只需在构造函数中设置它。并使用$rows->add() 而不是$rows[]
    • 以什么形式添加'by_reference' => false? OutboundInvoiceForm 或 OutBoundInvoiceRowType 或 ->add('rows', CollectionType::class。当我添加'by_reference' => false 以及当handleRequest 代码进入OutboudInvoice 实体的addRow 函数时?
    • OutboundInvoiceForm(与您指定 allow_addallow_delete 等的数组相同)。 by_reference => false 表示将显式调用填充实体的方法。否则,值将通过反射设置。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多