在后端使用原则和symfony保存多个实体的正确方法

时间:2019-06-03 15:26:56

标签: php symfony doctrine

概述 我正在学习Symfony和学说。我正在尝试使用foreach将计划付款保存到表中,问题是它没有将任何内容保存到数据库中。

我要从角度发送的对象是这样的:


SaldoEntrada: 0

clienteProveedor: {id: 6, nombre: "Consumidor Final", representante: null, telefono: "356241254"}

entrada: 80

fechaEmision: "2019-05-01 00:00:00"

formaPago: 1

id: 11

incio: Moment {_isAMomentObject: true, _i: {year: 2019, month: 5, date: 10}, _isUTC: false, _pf: Object, _locale: Locale, …}

intereses: 0

numero: 123333214

recibido: "100.00"

saldo: 502.4

saldoFinal: 502.4

tablaPagos: Array (4)
0 {numeroCuota: 1, valorCuota: "125.60", fechaPago: Moment, comprasVentas: 11, saldo: "125.60"}
1 {numeroCuota: 2, valorCuota: "125.60", fechaPago: Moment, comprasVentas: 11, saldo: "125.60"}
2 {numeroCuota: 3, valorCuota: "125.60", fechaPago: Moment, comprasVentas: 11, saldo: "125.60"}
3 {numeroCuota: 4, valorCuota: "125.60", fechaPago: Moment, comprasVentas: 11, saldo: "125.60"}

Array prototipo

tipocom: 1

total: 582.4

totalConIntereses: 502.4

enter image description here 这里最重要的是“ tablaPagos”,因为这是我要保存的付款清单。

这是我的savePlanPagos.php

<?php

namespace App\Action\ClienteProveedor;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\EntityManagerInterface;
use App\Doctrine\DoctrineEntity;

use App\Entity\PlanPagos;

use Carbon\Carbon;

class SavePlanPagos
{
    private $em;
    private $doctrineEntity;
    private $tokenStorage;

    public function __construct(EntityManagerInterface $em, DoctrineEntity $doctrineEntity, TokenStorageInterface $tokenStorage)
    {
        $this->em = $em;
        $this->doctrineEntity = $doctrineEntity;
        $this->tokenStorage = $tokenStorage;
    }

    /**
     * @Route(
     *     name="save_plan_pagos",
     *     path="/clientes_proveedores/save_plan_pagos",
     *     methods={"POST"},
     *     defaults={"_api_item_operation_name"="save_plan_pagos"}
     * )
     *
     * @return cliente
     */
    public function __invoke(Request $request)
    {
        $user = $this->tokenStorage->getToken()->getUser();
        $planPagos = $request->request->get('tablaPagos');
        foreach ($planPagos as $pagoDetalle) {

            $detallePagos = new PlanPagos(); 

            $this->doctrineEntity->save($detallePagos, $pagoDetalle, [
                'flush' => false
            ]);

            $serializer = new Serializer([new ObjectNormalizer()]);
            return new JsonResponse($serializer->normalize($detallePagos, 'json', ['attributes' => [
                'id'
            ]]), 200);
        }


    }
}

控制台说了这一点,但实际上并没有将任何内容保存到数据库中

2019-06-03T15:14:52+00:00 [info] Matched route "save_plan_pagos".
2019-06-03T15:14:52+00:00 [info] User Deprecated: The "lexik_jwt_authentication.security.authentication.entry_point.api" service is deprecated since LexikJWTAuthenticationBundle version 2.0 and will be removed in 3.0
2019-06-03T15:14:52+00:00 [info] User Deprecated: The "Lexik\Bundle\JWTAuthenticationBundle\Security\Http\EntryPoint\JWTEntryPoint" class is deprecated since version 2.0 and will be removed in 3.0. Use "Lexik\Bundle\JWTAuthenticationBundle\Security\Guard\JWTTokenAuthenticator" instead.
2019-06-03T15:14:52+00:00 [info] User Deprecated: The "security.authentication.listener.jwt.api" service is deprecated since LexikJWTAuthenticationBundle version 2.0 and will be removed in 3.0
2019-06-03T15:14:52+00:00 [info] User Deprecated: The "Lexik\Bundle\JWTAuthenticationBundle\Security\Firewall\JWTListener" class is deprecated since version 2.0 and will be removed in 3.0. See "Lexik\Bundle\JWTAuthenticationBundle\Security\Guard\JWTTokenAuthenticator" instead.
2019-06-03T15:14:52+00:00 [info] User Deprecated: The "security.authentication.provider.jwt.api" service is deprecated since LexikJWTAuthenticationBundle version 2.0 and will be removed in 3.0
2019-06-03T15:14:52+00:00 [info] User Deprecated: The "Lexik\Bundle\JWTAuthenticationBundle\Security\Authentication\Provider\JWTProvider" class is deprecated since version 2.0 and will be removed in 3.0. See "Lexik\Bundle\JWTAuthenticationBundle\Security\Guard\JWTTokenAuthenticator" instead.
2019-06-03T15:14:52+00:00 [info] User Deprecated: The "App\Entity\Auth" class implements "Symfony\Component\Security\Core\User\AdvancedUserInterface" that is deprecated since Symfony 4.1.
2019-06-03T15:14:53+00:00 [debug] SELECT u0_.id AS id_0, u0_.email AS email_1, u0_.username AS username_2, u0_.password AS password_3, u0_.verified AS verified_4, u0_.active AS active_5, u0_.recovery AS recovery_6, u0_.must_change_password AS must_change_password_7, u0_.rol AS rol_8, u0_.hash AS hash_9, u0_.last_login AS last_login_10, u0_.attempts AS attempts_11, u0_.locked AS locked_12 FROM users u0_ WHERE (u0_.email = ? OR u0_.username = ?) AND u0_.active = ? AND u0_.verified = ? AND u0_.locked = ?
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\DebugHandlersListener::configure".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ValidateRequestListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "Nelmio\CorsBundle\EventListener\CorsListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\SessionListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "Qandidate\Common\Symfony\HttpKernel\EventListener\JsonRequestTransformerListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\ResolveControllerNameSubscriber::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "ApiPlatform\Core\Filter\QueryParameterValidateListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "ApiPlatform\Core\EventListener\AddFormatListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "ApiPlatform\Core\EventListener\ReadListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "ApiPlatform\Core\EventListener\DeserializeListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "ApiPlatform\Core\Security\EventListener\DenyAccessListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.request" to listener "ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\SwaggerUiListener::onKernelRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelController".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.controller_arguments" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\SecurityListener::onKernelControllerArguments".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.controller_arguments" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\IsGrantedListener::onKernelControllerArguments".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.response" to listener "ApiPlatform\Core\Hydra\EventListener\AddLinkHeaderListener::onKernelResponse".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\ResponseListener::onKernelResponse".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.response" to listener "Symfony\Component\WebLink\EventListener\AddLinkHeaderListener::onKernelResponse".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.response" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\HttpCacheListener::onKernelResponse".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.response" to listener "Symfony\Component\Security\Http\RememberMe\ResponseListener::onKernelResponse".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.response" to listener "Nelmio\CorsBundle\EventListener\CorsListener::onKernelResponse".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.response" to listener "ApiPlatform\Core\HttpCache\EventListener\AddHeadersListener::onKernelResponse".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\SessionListener::onKernelResponse".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.response" to listener "Symfony\Component\HttpKernel\EventListener\StreamedResponseListener::onKernelResponse".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelFinishRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\SessionListener::onFinishRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\TranslatorListener::onKernelFinishRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.finish_request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelFinishRequest".
2019-06-03T15:14:53+00:00 [debug] Notified event "kernel.finish_request" to listener "Symfony\Bundle\SecurityBundle\Debug\TraceableFirewallListener::onKernelFinishRequest".
[Mon Jun  3 10:14:53 2019] 127.0.0.1:49791 [200]: /api/clientes_proveedores/save_plan_pagos

问题

  • 保存我的实体的正确方法
  • 进行转储或向控制台日志进行转换的正确方法
  • 响应必须在前面还是外面?

我真的很陌生

  • 我真的很陌生,所以如果我错过了什么,请告诉我 评论,然后将信息添加到问题中。

更新

感谢我将'flush'=> false更改为true的建议,现在保存但只有一个条目

 public function __invoke(Request $request)
    {
        $user = $this->tokenStorage->getToken()->getUser();
        $planPagos = $request->request->get('tablaPagos');
        foreach ($planPagos as $pagoDetalle) {

            $detallePagos = new PlanPagos(); 
            $detallePagos->setFechaInteres(new \Carbon\Carbon());
            $this->doctrineEntity->save($detallePagos, $pagoDetalle, ['flush' => true]);

            $serializer = new Serializer([new ObjectNormalizer()]);
            return new JsonResponse($serializer->normalize($detallePagos, 'json', ['attributes' => [
                'id'
            ]]), 200);
        }


    }

0 个答案:

没有答案