Symfony形式没有设置实体关系

时间:2017-01-11 14:36:24

标签: php forms symfony

我将OneToMany的实体OutboundInvoice添加到实体OutboundInvoiceRow 我使用CollectionType为OutboundInvoice创建表单,当我在我的数据库中添加一些OutboundInvoiceRow时,在OutboundInvoiceRow中看不到sentence_string <- function(x){ x <- tolower(x) x <- paste0(x, sep = ,) x } 但在调试中invoice_id时我看到带有OutboundInvoiceRow集合的OutboundInvoice,在里面看到我的OutboundInvoiceRow。我尝试调试,而不是在函数addRows或addRow中的实体OutboundInvoice中输入。现在我有实体OutboundInvoice与集合OutboundInvoiceRow但在DB OutboundInvoiceRow中刷新后没有OutboundInvoice。将OutboundInvoice设置为OutboundInvoiceRow或使用表单???

的正确方法
$form->handleRequest($request);

我的实体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;
}

我的行动

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,
    ));
}

1 个答案:

答案 0 :(得分:1)

'type' => new OutBoundInvoiceRowType()错误,您应该使用entry_type选项

您的实体中没有removeRow方法

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

并在表单中添加'by_reference' => false(在集合选项中)。