0

So I want to display id value(AI) before I insert it to the db,from the user to be checked and confirmed . I need to display InvoiceId which is foreign key related to id of 1st table. When I use getId() method I get null value.Basicaly I want to get InvoiceId not to be null. Or do I let user to insert InvoiceId but then it wont be related to id of 1st table.

I get this message:

An exception occurred while executing 'INSERT INTO invoice_rows (InvoiceId, Description, Quantity, Amount, VAT, TotalWithVAT) VALUES (?, ?, ?, ?, ?, ?)' with params [null, "111", 111, 111, 1, 111]: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'InvoiceId' cannot be null

I do not have set method for id,can it be done in some other way. Ex. I get last inserted id from table and then increment it for 1?

class InvoiceController extends Controller
{
    /**
     * @Route("/", name="invoice")
     * @param Request $request
     * @return Response|null
     */
    public function invoiceAction(Request $request)
    {

        $invoiceHeaderForm = $this->createForm(\InvoiceFormType::class);
        $invoiceBodyForm = $this->createForm(\InvoiceRowsFormType::class);
        $invoiceHeaderForm->handleRequest($request);
        $invoiceBodyForm->handleRequest($request);
        //var_dump($invoiceBodyForm);

        if ($invoiceHeaderForm->isSubmitted() && $invoiceHeaderForm->isValid()){
               // && $invoiceBodyForm->isSubmitted() && $invoiceBodyForm->isValid()){
            /*
             * Get the data from the form
             */
            $dataHeader = $invoiceHeaderForm->getData();
            $dataBody = $invoiceBodyForm->getData();
            $invoice = new Invoice();
            $invoiceBody = new InvoiceBody();

            // Set the data from the form to the $invoice
            $invoice->setClientId($dataHeader['clientId']);
            $invoice->setInvoiceDate($dataHeader['invoiceDate']);
            $invoice->setInvoiceNumber($dataHeader['invoiceNumber']);
            // Set the data form the body form to the $invoiceBody
            $invoiceBody->setInvoiceId($dataBody['invoiceId']);
            $invoiceBody->setDescription($dataBody['description']);
            $invoiceBody->setQuantity($dataBody['quantity']);
            $invoiceBody->setAmount($dataBody['amount']);
            $invoiceBody->setVAT($dataBody['VAT']);
            $invoiceBody->setTotalWithVAT($dataBody['totalWithVAT']);

            // Send the data to the next page with session
            $this->get('session')->set('invoice',$invoice);
            $this->get('session')->set('invoiceBody',$invoiceBody);

             return $this->redirectToRoute('invoice_confirm');
 
        }
        
        return $this->render('invoice/invoice.html.twig', [
            'invoiceHeaderForm' => $invoiceHeaderForm->createView(),
            'invoiceBodyForm' => $invoiceBodyForm->createView()
        ]);
    }

    /**
     * @Route("/invoice-confirm", name="invoice_confirm")
     * @param Request $request
     * @param EntityManagerInterface $em
     * @return RedirectResponse|Response|null
     */
    public function invoiceActionConfirm(Request $request,EntityManagerInterface $em){

        $confirmForm = $this->createForm(ConfirmFormType::class);
        $confirmForm->handleRequest($request);
        //var_dump($confirmForm);
        $invoice = $this->get('session')->get('invoice');
        $invoiceBody = $this->get('session')->get('invoiceBody');

        $invoiceBody->getInvoiceId();
        if($confirmForm->isSubmitted() && $confirmForm->isValid()){

            $em->persist($invoice);
            $em->persist($invoiceBody);
            $em->flush();
            return $this->redirectToRoute('invoice_created',[
                'request' => $request
            ],307);
        }
        return $this->render('invoice/invoice-confirm.html.twig',[
            'invoice' => $invoice,
            'invoiceBody' => $invoiceBody,
            'confirmCreation' => $confirmForm->createView()
        ]);
    }

    /**
     * @Route("/invoice-created", name="invoice_created",methods={"POST"})
     * @param EntityManagerInterface $em
     * @param InvoiceRepository $invoiceRepository
     * @return string
     */
    public function invoiceCreated(EntityManagerInterface $em){

        //$confirmCreation = $this->createForm(ConfirmFormType::class);
        $invoiceRepository = $em->getRepository(Invoice::class);
        $invoice = $invoiceRepository->findBy([],['id'=>'DESC'],1,0);
        //var_dump($invoice);

        return $this->render('invoice/invoice-created.html.twig',[
            'invoices' => $invoice
        ]);
    }
}

One more question: Did I correctly do this foreign key?

/**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\InvoiceBody")
     */
    private $id; 

/**
     * @var int
     *
     * @ORM\Column(name="InvoiceId", type="integer", unique=true)
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\Invoice")
     * @JoinColumn(name="InvoiceId", referencedColumnName="id")
     */
    private $invoiceId;
nexon91
  • 19
  • 1
  • 5

1 Answers1

0

there are two solutions:

a) manually generate the ID (so no autoincrement) and then after the check of your user, persist and flush the object (=store to database), or

[...]
    /**
     * @var int
     *
     * @ORM\Column(name="ID", type="integer", nullable=false, options={"unsigned"=true})
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="CUSTOM")
     * @ORM\CustomIdGenerator(class="App\Service\RandomIdGenerator")

     */
    private $id;
[...]

b) create an (from the ID) independent field for the invoice number. (you need either to store something like "nextNumber" somewhere in an Option-Table in the database or generate that number and crosscheck for unwanted dublication)

Tim K.
  • 335
  • 4
  • 23