Symfony / php : avoid query to give nested json objects from one to many relation

I have entities; Command, User, GiftCheck, and GiftCheckType :

GiftCheck

    <?php

    namespace AppEntity;

    use DoctrineORMMapping as ORM;

    /**
     * @ORMEntity(repositoryClass="AppRepositoryGiftCheckRepository")
     */
    class GiftCheck
    {
        /**
         * @ORMId()
         * @ORMColumn(type="integer", length=255)
         *@ORMGeneratedValue(strategy="AUTO")
         */
        private $id;

        /**
         * @ORMColumn(type="string", length=255, columnDefinition="ENUM('Cadeau', 'Anniversaire', ' NĂ´el', 'Saint Valentain ')")
         */
        private $theme;

        /**
        * @ORMManyToOne(targetEntity="AppEntityUser", inversedBy="giftChecks")
         */
       private $User;

       /**
        * @ORMColumn(type="string", length=255)
        */
       private $uniqueSerialId;

       /**
        * @ORMManyToOne(targetEntity="AppEntityCommand", inversedBy="giftChecks")
        * @ORMJoinColumn(nullable=false)
        */
       private $command;
  /**
     * @ORMManyToOne(targetEntity="AppEntityGiftCheckType", inversedBy="gift")
     */
    private $giftCheckType;

User

<?php
namespace AppEntity;
use FOSUserBundleModelUser as BaseUser;
use DoctrineORMMapping as ORM;
use FOSUserBundleModelUserInterface;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;

/**
 * @ORMEntity
 * @ORMTable(name="fos_user")
 * @UniqueEntity("email")
 * @UniqueEntity("username")
 * @UniqueEntity("tel")
 */
class User extends BaseUser
{
    /**
     * @ORMId
     *@ORMGeneratedValue(strategy="AUTO")
     * @ORMColumn(type="integer")
     */
    protected $id;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $firstname;

    /**
     * @ORMOneToMany(targetEntity="AppEntityCommand", mappedBy="user")
     */
    private $commands;
     /**
    * @ORMOneToMany(targetEntity="AppEntityGiftCheck", mappedBy="User")
    */
    private $giftChecks;

Command

<?php

namespace AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass="AppRepositoryCommandRepository")
 */
class Command
{
    /**
     * @ORMId()
     * @ORMColumn(type="string", length=255)
     */
    private $id;

    /**
     * @ORMColumn(type="float")
     */
    private $price;

    /**
     * @ORMManyToOne(targetEntity="AppEntityUser", inversedBy="commands")
     * @ORMJoinColumn(nullable=true, onDelete="CASCADE")
     */
    private $user;

    /**
     * @ORMOneToMany(targetEntity="AppEntityCommandLine", mappedBy="command")
     */
    private $commandLines;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $status;



    /**
     * @ORMOneToMany(targetEntity="AppEntityGiftCheck", mappedBy="command")
     */
    private $giftChecks;

GiftCheckType

<?php

namespace AppEntity;

use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;

/**
 * @ORMEntity(repositoryClass="AppRepositoryGiftCheckTypeRepository")
 */
class GiftCheckType
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $designation;

    /**
     * @ORMOneToMany(targetEntity="AppEntityGiftCheck", mappedBy="giftCheckType")
     */
    private $gift;

    /**
     * @ORMColumn(type="string", length=255)
     */
    private $type;

I found my self faced to a problem, When I try to fetch my command data I got a nested json object ( like children / parent / children structure of the same object ) which contain giftchecks array, every giftcheck contain user object which have the same giftchecks list, the two images bellow describe the status of this nested JSON in the browser console log when a fetch the result with angular :

enter image description here
enter image description here

is that problem about missing serialization on my entities or coming from other side ? are my entities relations correctly coded ?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *