0

I use to add vue3 frontend content in a backend symfony 5, I need to return a jsonResponse for sorting values in the front, I did a Serializer Service to Serialize in Json an object which is from the Doctrine Repository, The BDD callback is a type Grilles object, So to have a Json from the object I use for Serializer, Until that there's no problem, The Serializer service return correct Object but it's empty about the content... I don't see anything about that, can you debugging me please ?

The SerializerService:

namespace App\Services;

use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class SerializerService
{
    public function serialize()
    {

    $encoders = [new XmlEncoder(), new JsonEncoder()];
    $normalizers = [new ObjectNormalizer()];

    $serializer = new Serializer($normalizers, $encoders);

    return $serializer;
    
    }
}

The Repository:

public function findOneInAllGrillesObj($email, $type): ?Grilles
    {
        return $this->createQueryBuilder('g')
            ->andWhere('g.email = :email')
            ->andWhere('g.type = :type')
            ->setParameter('email', $email)
            ->setParameter('type', $type)
            ->getQuery()
            ->getOneOrNullResult()
        ;
    }

The controller:

/**
     *
     * @Route("/grilleADisplay", name= "grilleADisplay", methods={"GET","POST"})
     * IsGranted("ROLE_USER", message="vous devez vous connecter pour accéder à cette ressource")
     */
    public function jsonDisplayGrilleA(EntityManagerInterface $manager, SerializerService $serializer): JsonResponse
    {
        if(is_null($this->getUser())){
            $session=$request->getSession();
            $session->getFlashBag()->add("messageError" , "Vous devez être connecté pour accéder à cette ressource");
            return $this->redirectToRoute("login");
        }
        $type="A";
        $email=$this->getUser();
        $em = $this->getDoctrine()->getManager();
        $grillesrepository = $em->getRepository(Grilles::class);
        $datas = $grillesrepository->findOneInAllGrillesObj($email, $type);
        //dd($datas);
        $jsonContent = $serializer->serialize($datas);
        //dd($jsonContent);
        dd(new JsonResponse($jsonContent));
        return new JsonResponse($jsonContent);
    }

The DD give:

GrillesController.php on line 81:
Symfony\Component\Serializer\Serializer {#1144 ▼
  #encoder: Symfony\Component\Serializer\Encoder\ChainEncoder {#1158 ▶
    #encoders: array:2 [▶
      0 => Symfony\Component\Serializer\Encoder\XmlEncoder {#1149 ▶
        -defaultContext: array:7 [▶
          "as_collection" => false
          "decoder_ignored_node_types" => array:2 [▶
            0 => 7
            1 => 8
          ]
          "encoder_ignored_node_types" => []
          "load_options" => 2304
          "remove_empty_tags" => false
          "xml_root_node_name" => "response"
          "xml_type_cast_attributes" => true
        ]
        -dom: null
        -format: null
        -context: null
        #serializer: Symfony\Component\Serializer\Serializer {#1144}
      }
      1 => Symfony\Component\Serializer\Encoder\JsonEncoder {#1150 ▶
        #encodingImpl: Symfony\Component\Serializer\Encoder\JsonEncode {#1151 ▶
          -defaultContext: array:1 [▶
            "json_encode_options" => 0
          ]
        }
        #decodingImpl: Symfony\Component\Serializer\Encoder\JsonDecode {#1173 ▶
          #serializer: null
          -defaultContext: array:3 [▶
            "json_decode_associative" => true
            "json_decode_options" => 0
            "json_decode_recursion_depth" => 512
          ]
        }
      }
    ]
    #encoderByFormat: []
  }
  #decoder: Symfony\Component\Serializer\Encoder\ChainDecoder {#1171 ▶
    #decoders: array:2 [▶
      0 => Symfony\Component\Serializer\Encoder\XmlEncoder {#1149 ▶}
      1 => Symfony\Component\Serializer\Encoder\JsonEncoder {#1150 ▶}
    ]
    #decoderByFormat: []
  }
  -normalizers: array:1 [▶
    0 => Symfony\Component\Serializer\Normalizer\ObjectNormalizer {#1146 ▶
      #defaultContext: array:5 [▶
        "allow_extra_attributes" => true
        "circular_reference_handler" => null
        "circular_reference_limit" => 1
        "ignored_attributes" => []
        "exclude_from_cache_key" => array:1 [▶
          0 => "circular_reference_limit_counters"
        ]
      ]
      #classMetadataFactory: null
      #nameConverter: null
      #serializer: Symfony\Component\Serializer\Serializer {#1144}
      -propertyTypeExtractor: null
      -typesCache: []
      -attributesCache: []
      -objectClassResolver: null
      #classDiscriminatorResolver: null
      #propertyAccessor: Symfony\Component\PropertyAccess\PropertyAccessor {#1147 ▶
        -magicMethodsFlags: 3
        -ignoreInvalidIndices: true
        -ignoreInvalidProperty: false
        -cacheItemPool: null
        -propertyPathCache: []
        -readInfoExtractor: Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor {#1167 ▶
          -mutatorPrefixes: []
          -accessorPrefixes: array:4 [▶
            0 => "get"
            1 => "is"
            2 => "has"
            3 => "can"
          ]
          -arrayMutatorPrefixes: array:2 [▶
            0 => "add"
            1 => "remove"
          ]
          -enableConstructorExtraction: false
          -methodReflectionFlags: 1
          -magicMethodsFlags: 3
          -propertyReflectionFlags: 1
          -inflector: Symfony\Component\String\Inflector\EnglishInflector {#1143}
          -arrayMutatorPrefixesFirst: array:2 [▶
            0 => "add"
            1 => "remove"
          ]
          -arrayMutatorPrefixesLast: array:2 [▶
            0 => "remove"
            1 => "add"
          ]
        }
        -writeInfoExtractor: Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor {#1140 ▶
          -mutatorPrefixes: array:1 [▶
            0 => "set"
          ]
          -accessorPrefixes: array:4 [▶
            0 => "get"
            1 => "is"
            2 => "has"
            3 => "can"
          ]
          -arrayMutatorPrefixes: array:2 [▶
            0 => "add"
            1 => "remove"
          ]
          -enableConstructorExtraction: false
          -methodReflectionFlags: 1
          -magicMethodsFlags: 3
          -propertyReflectionFlags: 1
          -inflector: Symfony\Component\String\Inflector\EnglishInflector {#1141}
          -arrayMutatorPrefixesFirst: array:3 [▶
            0 => "add"
            1 => "remove"
            2 => "set"
          ]
          -arrayMutatorPrefixesLast: array:3 [▶
            0 => "set"
            1 => "remove"
            2 => "add"
          ]
        }
        -readPropertyCache: []
        -writePropertyCache: []
      }
      -discriminatorCache: []
      -objectClassResolver: Closure($class) {#1148 ▶
        class: "Symfony\Component\Serializer\Normalizer\ObjectNormalizer"
        this: Symfony\Component\Serializer\Normalizer\ObjectNormalizer {#1146}
      }
    }
  ]
  -denormalizerCache: []
  -normalizerCache: []
}

Nothing more about the data !!! The data From Repo is like That...

GrillesController.php on line 79:
App\Entity\Grilles {#796 ▼
  -id: 27
  -email: App\Entity\User {#674 ▶
    -id: 46
    -email: "[email protected]"
    -roles: array:1 [▶
      0 => "ROLE_USER"
    ]
    -password: "password"
    -tokenAVerifier: "03cb21ca9"
    -googleId: "16700"
    -avatar: "https://lh3.googleusercontent.com/a-/7SBolyARBgYgPA53ieFJ8WAJREw=s96-c"
    -hostedDomain: "Google"
    -facebookId: null
  }
  -type: "A"
  -procAge: array:3 [▶
    0 => "12"
    1 => "10"
    2 => "12"
  ]
  -procSexe: array:3 [▶
    0 => "12"
    1 => "13"
    2 => "12"
  ]
  -procCorde: array:8 [▶
    0 => "12"
    1 => "12"
    2 => "12"
    3 => "11"
    4 => "11"
    5 => "10"
    6 => "10"
    7 => "10"
  ]
  -procOeilleres: array:2 [▶
    0 => "12"
    1 => "10"
  ]
  -procFerrage: array:3 [▶
    0 => "2"
    1 => "1"
    2 => "3"
  ]
  -procNbreCourses: array:8 [▶
    0 => "12"
    1 => "12"
    2 => "12"
    3 => "11"
    4 => "11"
    5 => "10"
    6 => "10"
    7 => "10"
  ]
  -procVictoires: array:8 [▶
    0 => "15"
    1 => "14"
    2 => "13"
    3 => "13"
    4 => "12"
    5 => "12"
    6 => "11"
    7 => "10"
  ]
  -procPlaces: array:8 [▶
    0 => "14"
    1 => "14"
    2 => "14"
    3 => "13"
    4 => "12"
    5 => "12"
    6 => "11"
    7 => "10"
  ]
  -procGains: array:8 [▶
    0 => "15"
    1 => "14"
    2 => "13"
    3 => "13"
    4 => "13"
    5 => "12"
    6 => "12"
    7 => "10"
  ]
  -procRapportGainsParCourses: array:8 [▶
    0 => "16"
    1 => "15"
    2 => "14"
    3 => "13"
    4 => "12"
    5 => "11"
    6 => "10"
    7 => "9"
  ]
  -procHandPoids: array:3 [▶
    0 => "14"
    1 => "12"
    2 => "14"
  ]
  -procJockey: array:8 [▶
    0 => "ABRIVARD"
    1 => "RAFFIN"
    2 => "LEBOURGEOIS"
    3 => "THOMAIN"
    4 => "LAGADEUC"
    5 => "GELORMINI"
    6 => "MOTTIER"
    7 => "BEKAERT"
  ]
  -procMusique: array:6 [▶
    0 => "4"
    1 => "3"
    2 => "3"
    3 => "2"
    4 => "2"
    5 => "1"
  ]
  -pointsEntraineur: array:1 [▶
    0 => "null"
  ]
  -pointsJockey: array:1 [▶
    0 => "4"
  ]
  -nomPlat: array:8 [▶
    0 => "OMMEEL"
    1 => "PTIT PEDRO"
    2 => "EL OJANCANO"
    3 => "CAMPELLO"
    4 => "ARDITO"
    5 => "PEARL HARBOUR"
    6 => "BELINSKOV"
    7 => "FILLE DU ROI"
  ]
  -nomTrotAttele: array:8 [▶
    0 => "JUST A GIGOLO"
    1 => "IDAO DE TILLARD"
    2 => "HOHNECK"
    3 => "VIVID WISE AS"
    4 => "ETONNANT"
    5 => "HAPPY AND LUCKY"
    6 => "IDEALE DU CHENE"
    7 => "HOOKER BERRY"
  ]
  -nomHaies: array:8 [▶
    0 => "SUROIT"
    1 => "CAPODIMONTE"
    2 => "CIRANO DE PAIL"
    3 => "JIGTSAR PAM"
    4 => "ICEO MADRIK"
    5 => "FEDOR ROSAY"
    6 => "YOUTWO GLASS"
    7 => "INSIDE MONTLIOUX"
  ]
  -nomTrotMonte: array:8 [▶
    0 => "HONECK"
    1 => "VIVID WISE AS"
    2 => "GRANVILLAISE BLEUE"
    3 => "ETONNANT"
    4 => "JUST A GIGOLO"
    5 => "IDAO DE TILLARD"
    6 => "IZOARD VEDAQUAIS"
    7 => "HOOKER BERRY"
  ]
  -pointsNomPlat: array:1 [▶
    0 => "3"
  ]
  -pointsNomTrotAttele: array:1 [▶
    0 => "3"
  ]
  -pointsNomHaies: array:1 [▶
    0 => "3"
  ]
  -pointsNomTrotMonte: array:1 [▶
    0 => "3"
  ]
  -entraineursPlat: array:4 [▶
    0 => "GAUVIN"
    1 => "ROUGET"
    2 => "GRAFFARD"
    3 => "REYNIER"
  ]
  -entraineursTrotAttele: array:4 [▶
    0 => "DUVALDESTIN"
    1 => "ABRIVARD"
    2 => "TERRY"
    3 => "GUARATO"
  ]
  -entraineursHaies: array:4 [▶
    0 => "DANIELA MELE"
    1 => "QUINTON"
    2 => "FUIN"
    3 => "COTTIN"
  ]
  -entraineursTrotMonte: array:4 [▶
    0 => "DUVALDESTIN"
    1 => "ABRIVARD"
    2 => "TERRY"
    3 => "GUARATO"
  ]
  -pointsEntraineursPlat: array:1 [▶
    0 => "3"
  ]
  -pointsEntraineursTrotAttele: array:1 [▶
    0 => "3"
  ]
  -pointsEntraineursHaies: array:1 [▶
    0 => "3"
  ]
  -pointsEntraineursTrotMonte: array:1 [▶
    0 => "3"
  ]
  -proprietairesPlat: array:4 [▶
    0 => "AUGUSTIN-NORMAND"
    1 => "FRASSETTO"
    2 => "CAULLERY"
    3 => "LIBERTY LEAF"
  ]
  -proprietairesTrotAttele: array:4 [▶
    0 => "ALLAIRE"
    1 => "CHARMES"
    2 => "HUNTER VALLEY"
    3 => "BEAUDOUIN"
  ]
  -proprietairesHaies: array:4 [▶
    0 => "AUGUSTIN-NORMAND"
    1 => "FRASSETTO"
    2 => "CAULLERY"
    3 => "LIBERTY LEAF"
  ]
  -proprietairesTrotMonte: array:4 [▶
    0 => "ALLAIRE"
    1 => "CHARMES"
    2 => "HUNTER VALLEY"
    3 => "BEAUDOUIN"
  ]
  -pointsProprietairesPlat: array:1 [▶
    0 => "3"
  ]
  -pointsProprietairesTrotAttele: array:1 [▶
    0 => "3"
  ]
  -pointsProprietairesHaies: array:1 [▶
    0 => "3"
  ]
  -pointsProprietairesTrotMonte: array:1 [▶
    0 => "3"
  ]
  -useNoms: "non"
  -useEntraineurs: "non"
  -useProprietaires: "non"
  -useJockeys: "oui"
  -useDistance: "oui"
  -procRapport: array:8 [▶
    0 => "13"
    1 => "13"
    2 => "13"
    3 => "12"
    4 => "12"
    5 => "12"
    6 => "12"
    7 => "10"
  ]
}

An help will be soon... Thank you for all...

1 Answer 1

0

Ok that's fine I know how to ! The serializer is not the answer about have a not empty return, Like I see in Symfony 4 - JsonResponse returns a non-empty object as empty The object Grilles must be transform in array by:

$datas=(array)$datas;

Before return like that a JsonResponse:

 $datas = new JsonResponse($datas);

I add this, if the repository return an array it don't work, must return an object ans convert to an array before return new JsonResponse() (Symfony 5)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.