dongying2112 2019-06-03 15:26
浏览 77

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

Overview I'm learning Symfony and doctrine. I'm trying to save a plan payment into a table using a foreach, the problem is that it's not saving anything into my database.

The object I'm sending from angular is this:


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 The most important thing here is the "tablaPagos" because it's the list of payments I'm trying to save.

This is my 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);
        }


    }
}

The console says this, but it's not really saving anything to the database

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

Questions

  • Right way to save my entity
  • Right way to make a dump or vardump to console log
  • The response must go in the foreach or outside?

Im really new to all this

  • I'm really new at this so if I miss something just tell me on the comments and I will add the info to the question.

UPADTE

Thanks for the suggestion I change 'flush' => false to true, now is saving but only one entry

 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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 运筹学排序问题中的在线排序
    • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
    • ¥30 求一段fortran代码用IVF编译运行的结果
    • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
    • ¥15 C++ 头文件/宏冲突问题解决
    • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
    • ¥50 安卓adb backup备份子用户应用数据失败
    • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
    • ¥30 python代码,帮调试,帮帮忙吧
    • ¥15 #MATLAB仿真#车辆换道路径规划