1

Invoice と InvoiceRow という 2 つの関連エンティティがあり、新しい請求書を作成できるフォームがあります。

また、データベースにテスト データをシードするためにロードするテスト データのフィクスチャもいくつかあります。エンティティをデータベースに永続化する方法に関係なくinvoice_id、InvoiceRow は常にnull

請求書を永続化しようとしたフィクスチャで

InvoiceRow.php:

namespace Settleup\InvoiceBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
Class InvoiceRow {
    /**
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $description;

    /**
     * The invoice row amount, stored in "bottle tops", ie £1.00 is 10000 bottle tops
     * 
     * @ORM\Column(type="integer")
     */
    protected $amount;

    /**
     * The invoice this row belongs to.
     *
     * @ORM\ManyToOne(targetEntity="Settleup\InvoiceBundle\Entity\Invoice", inversedBy="rows")
     * @var integer
     **/
    protected $invoice;

    public function setDescription($description) {
        $this->description = $description;
    }

    public function getDescription() {
        return $this->description;
    }

    public function setAmount($amount) {
        $this->amount = $amount * 10000;
    }

    public function getAmount() {
        return $this->amount / 10000;
    }


    /**
     * Returns the invoice this row is attached to.
     *
     * @return Settleup\InvoiceBundle\Entity\Invoice The invoice.
     */
    public function getInvoice() {
        return $this->invoice;
    }

    /**
     * Set the invoice this row belongs to.
     *
     * @param Settleup\InvoiceBundle\Entity\Invoice $newinvoice The invoice.
     */
    public function setInvoice($invoice) {
        $this->invoice = $invoice;

        return $this;
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
}

Invoice.php

<?php

namespace Settleup\InvoiceBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

use Sylius\Bundle\AddressingBundle\Model\AddressInterface;

use Settleup\InvoiceBundle\Exception\MerchantUpdateException;

/**
 * @ORM\Entity
 */
Class Invoice {
    /**
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $reference;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $issueDate;

    /**
     * @ORM\OneToMany(targetEntity="Settleup\InvoiceBundle\Entity\InvoiceRow", mappedBy="invoice", cascade="persist")
     */
    protected $rows;

    /**
     * @ORM\ManyToOne(targetEntity="Settleup\UserBundle\Entity\User")
     */
    protected $merchant;

    /**
     * @ORM\ManyToOne(targetEntity="Settleup\InvoiceBundle\Entity\InvoiceAddress", cascade={"persist"})
     */
    protected $invoiceAddress;

    /**
     * undocumented function
     *
     * @return void
     * @author 
     **/
    public function __construct()
    {
        $this->rows = new ArrayCollection();

        $this->issueDate = new \DateTime();
    }

    /**
     * undocumented function
     *
     * @return void
     * @author 
     **/
    public function getId()
    {
        return $this->id;
    }

    /**
     * Returns all the rows on this invoice.
     *
     * @return ArrayCollection
     * @author 
     **/
    public function getRows()
    {
        return $this->rows;
    }

    /**
     * Set the reference for this invoice.
     *
     * @return void
     * @author 
     **/
    public function setReference($reference)
    {
        $this->reference = $reference;
    }

    /**
     * Returnt he invoice reference.
     *
     * @return string
     * @author 
     **/
    public function getReference()
    {
        return $this->reference;
    }

    /**
     * Sets the date this invoice was issued.
     *
     * @param  \DateTime $issueDate The date the invoice was issued.
     * @return void
     * @author 
     **/
    public function setIssueDate(\DateTime $issueDate)
    {
        $this->issueDate = $issueDate;
    }

    /**
     * Return the date the invoice was raised.
     *
     * @return \DateTime
     * @author 
     **/
    public function getIssueDate()
    {
        return $this->issueDate;
    }

    /**
     * Sets the collections of rows that make up this invoice.
     *
     * @return void
     * @author 
     **/
    public function setRows(ArrayCollection $rows)
    {
        $this->rows = $rows;
    }

    /**
     * Return the merchant for this invoice.
     *
     * @return Settleup\UserBundle\Entity\User
     * @author 
     **/
    public function getMerchant()
    {
        return $this->merchant;
    }

    /**
     * Returns an instance of SyliusAddress, the address this invoice is for.
     *
     * @return Settleup\InvoiceBundle\Entity\InvoiceAddress
     * @author 
     **/
    public function getInvoiceAddress()
    {
        return $this->invoiceAddress;
    }

    /**
     * Sets the address for this invoice
     *
     * @param Settleup\InvoiceBundle\Entity\InvoiceAddress invoiceAddress The address this invoice is for.
     *
     * @return void
     * @author 
     **/
    public function setInvoiceAddress(AddressInterface $invoiceAddress)
    {
        $this->invoiceAddress = $invoiceAddress;
    }

    /**
     * Add rows
     *
     * @param \Settleup\InvoiceBundle\Entity\InvoiceRow $rows
     * @return Invoice
     */
    public function addRow(\Settleup\InvoiceBundle\Entity\InvoiceRow $rows)
    {
        $this->rows[] = $rows;

        return $this;
    }

    /**
     * Remove rows
     *
     * @param \Settleup\InvoiceBundle\Entity\InvoiceRow $rows
     */
    public function removeRow(\Settleup\InvoiceBundle\Entity\InvoiceRow $rows)
    {
        $this->rows->removeElement($rows);
    }

    /**
     * Set merchant
     *
     * @param \Settleup\UserBundle\Entity\User $merchant
     * @return Invoice
     */
    public function setMerchant(\Settleup\UserBundle\Entity\User $merchant = null)
    {
        if ($this->id == null) {
            $this->merchant = $merchant;
        } else {
            throw new MerchantUpdateException("Unable to update the merchant for this invoice, you can only assign a merchant when creating the invoice.");
        }

        return $this;
    }

    /**
     * @TODO Find out why this method is needed.
     *
     * @return void
     * @author 
     **/
    public function getAddresses()
    {
    }

    public function setAddresses() {}
}

両方のテーブルに行を入力するために使用するフォームがあります。

class InvoiceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('reference')
            ->add('rows', 'collection', array(
                    'type' => 'invoice_row',
                    'allow_add' => true,
                    'by_reference' => false
                ))
            ->add('invoiceAddress', 'sylius_address');

            $builder->addEventListener(
                FormEvents::PRE_SET_DATA,
                function(FormEvent $event) {
                    $form = $event->getForm();

                    $form->add('merchant', 'hidden');
                }
            );
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Settleup\InvoiceBundle\Entity\Invoice',
            'cascade_validation' => true
        ));
    }

    public function getName()
    {
        return 'settleup_invoicebundle_invoicetype';
    }
}

コントローラーのアクションを使用してそのフォームを保存すると、次のようになります。

public function createAction(Request $request)
{
    $entity  = new Invoice();

    $form = $this->createForm(new InvoiceType(), $entity);
    $form->bind($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();

        $entity->setMerchant($this->getUser());

        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('invoice_show', array('id' => $entity->getId())));
    } else {

    }

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

InvoiceRowにinvoice_idがないことを除いて、両方の行にすべて正しいデータがあります。

4

1 に答える 1

0

少し遅いですが、他の人には役立つかもしれません

1) 所有側 (FK を持つ側) を永続化してみてください。つまり、invoiceRow

2) id フィールドがデフォルトに従わなかったため、関連エンティティの永続化に問題がありました。これを追加すると機能しました:

* @ORM\ManyToOne(targetEntity="Personas", fetch="EAGER", inversedBy="cuentasCorriente",
 * cascade={"persist", "refresh"})
 * @ORM\JoinColumn(name="persona_id", referencedColumnName="ID", nullable=false)

referencedColumnName="ID" See "ID" != "id" Doctrine をレガシー システムに追加したため、選択の余地がありませんでした。

于 2013-11-08T00:50:14.357 に答える