A
A
Alexander Sevirinov2019-01-31 16:52:42
Doctrine ORM
Alexander Sevirinov, 2019-01-31 16:52:42

How to update values ​​in Doctrine 2.6 ORM row?

There is such a method, they are added to the database without problems, the question is how to update the values ​​\u200b\u200bif the qr_code is unique?

public function store(Request $request, Response $response)
    {
        $params = (object)$request->getParams();

        $entityManager = $this->container->get('em');
        $dataRepository = $entityManager->getRepository('App\Models\Entity\QrCode');
        $result = $dataRepository->findOneBy(['qr_code' => $params->qr_code]);

        if(!$result) {
            $data = (new QrCode())
                ->setUserId($params->user_id)
                ->setQrCode($params->qr_code)
                ->setAmount($params->amount)
                ->setBox($params->box);

            $logger = $this->container->get('logger');
            $logger->info('QRcode added!', $data->getValues());

            $entityManager->persist($data);
            $entityManager->flush();
        } else {
            $data = false;
        }

        $return = $response->withJson($data, 201)->withHeader('Content-type', 'application/json');

        return $return;
    }

You need to find the line with the qr_code value and update the amount or box in this line

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question