Skip to content
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

Strange behavior with groups : relation is no more serialized #200

Closed
vincentdieltiens opened this issue Oct 16, 2012 · 2 comments
Closed

Comments

@vincentdieltiens
Copy link

Hi,

I've a strange behavior when i use groups.
When i serialize a Doctrine entity with one relation, the relation is not serialized anymore when using groups.

Here is my configuration : I have 2 entities -- Customer and User -- with a many-to-one relation from Customer to User on the field "created_by".

Here is a piece of "Entitiy.Customer.yml" :

Dieltiens\Bundle\CustomerBundle\Entity\Customer:
    exclusion_policy: NONE
    properties:
        id:
            expose: true
            groups: [list]
        company_name:
            expose: true
            groups: [list]

        # other fields

        created_by:
            expose: true
            groups: [list, detail]
            type: Dieltiens\Bundle\UserBundle\Entity\User

In my Symfony controller, the code that is serializing is :

$customers = $this->getDoctrine()->getRepository('DieltiensCustomerBundle:Customer')->findAll();
$serializer = $this->container->get('serializer');
//$serializer->setGroups(array('list'));
$customers_json = $serializer->serialize($customers, 'json');

With the third line commented, it is working i get the following response :

[{
  "id": 1,
  "company_name": "companyName",
   // other fields  
  "created_by": {
    "id": 1,
    "username": "Vincent",
    "password": "an hashed password"
  }
}]

With the third line uncommented, i get the following response :

[{
  "id": 1,
  "company_name": "Diel",
  // other fields  
  "created_by": []
}]

It's very strange because between the to different results, nothing change except the line `$serializer->setGroups(array('list'));``

Also, the created_by value is an empty array then I set the type as a User Entity.

Thanks,

Vincent

@schmittjoh
Copy link
Owner

Do these other fields have a group assigned?

@vincentdieltiens
Copy link
Author

Hi,

sorry for this late answer.
The problem was that there was no fields with the group name in the User entity.

Thansk for your help ;-)

Vincent

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