Skip to content

ManyToOne and OneToMany Serialization Groups #387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
bassrock opened this issue Feb 3, 2015 · 1 comment
Closed

ManyToOne and OneToMany Serialization Groups #387

bassrock opened this issue Feb 3, 2015 · 1 comment

Comments

@bassrock
Copy link

bassrock commented Feb 3, 2015

I currently have a class called User that has a Transaction like so:

/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\BaseBundle\Entity\TransactionUserRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class TransactionUser {

    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Serializer\Groups({"transactionUser"})
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     * @Serializer\Groups({"transactionUser"})
     */
    private $name;

    /**
     * @var \ Acme\BaseBundle\Entity\Transaction $transaction
     * @ORM\ManyToOne(targetEntity="Acme\BaseBundle\Entity\Transaction", inversedBy="transactionUsers", cascade={"persist", "remove"})
     * @Serializer\Groups({"transactionUser"})
     */
    private $transaction;

}

And a transaction class:

/**
 * Transaction
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Acme\BaseBundle\Entity\TransactionRepository")
 * @ORM\HasLifecycleCallbacks()
 */
class Transaction
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Serializer\Groups({"transaction"})
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Acme\BaseBundle\Entity\TransactionUser", mappedBy="transaction", cascade={"persist", "remove"})
     * @Serializer\Groups({"transaction"})
     */
    private $transactionUsers;

}

I return the TransactionUser in my rest controller like so with the serialization group of transaction and transactionUser:

/** @var array|null $transactionUsers */
$transactionUsers = $transactionUserRepository->findAllWithUserQueryBuilder($this->user)->getQuery()->getResult();

return $transactionUsers;

I would like to return the transactionUser with the transaction inside it and the list of transaction users, but on the internal list of transaction users only want to output their id not the name property. Is there a way to handle this type of serialization?

Also it currently outputs it like so:

[
  {
    "id": 3,
    "transaction_users": {
      "1": {
        "id": 2,
        "name": "Test123"
      },
      "2": {
        "id": 3,
        "name": "Test"
      }
    },
    "name": "Test"
  },
  {
    "id": 5,
    "transaction_users": {
      "1": {
        "id": 4,
        "name": "Test1234"
      },
      "2": {
        "id": 5,
        "name": "Test1"
      }
    },
    "name": "Test1"
  }
]

But I would like this:

[
  {
    "id": 3,
    "transaction_users": {
      "1": {
        "id": 2
      },
      "2": {
        "id": 3
      }
    },
    "name": "Test"
  },
  {
    "id": 5,
    "transaction_users": {
      "1": {
        "id": 4
      },
      "2": {
        "id": 5
      }
    },
    "name": "Test1"
  }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants