I encountered the problem of using the doctrine and realized that I was not fully aware of the mechanism of its work. I have several entities, the main one is the client (company, firm, team) this client has users, but all actions come from the client. It works fine everywhere, but I get an error with a certain transaction entity.
The client makes a transaction, then I get it and add to the payment. But when I write a transaction with the help of merge, flush, the transaction for some reason has a blank id null, although everything is written correctly to the database.
public function createConfirmedTransaction(Client $client, PaymentMethod $method, $amount, $wallet, $invoice, $payment_code) { $status = $this->getStatus('confirmed'); $transaction = new Transaction(); $transaction->setClient($client); $transaction->setMethod($method); $transaction->setAmount($amount); $transaction->setWallet($wallet); $transaction->setInvoice($invoice); $transaction->setPaymentCode($payment_code); $transaction->setStatus($status); $transaction->setConfirmedAt(new \DateTime()); $this->entityManager->merge($transaction); $this->entityManager->flush(); return $transaction->getId(); } Here we have created a transaction and then I try to write it as payment.
public function createPayment(Client $client, PaymentMethod $method, Transaction $transaction, $usd) { $payment = new Payment(); $payment->setTransaction($transaction); $payment->setClient($client); $payment->setMethod($method); $payment->setAmount($usd); $this->entityManager->merge($payment); $this->entityManager->flush(); } But the error takes off:
A new entity has been found through the relationship 'AppBundle \ Entity \ Payment' transaction.
It turns out that the doctrine has inserted into the database, but does not understand what it did, because it did not receive the transaction id and does not see it. Next, I tried to make persist transactions along the chain until the last User connection, when I make a persist on it, the doctrine tries to insert the user again into the database, although it is there.
Why in this case merge does not work, and persist does the user re-insert?