core: underscore in field name does not work with @Groups

I am using doctrine.orm.naming_strategy.underscore, in generated entities, the member variables are named in snake case. When using @Groups to select fields, any of the fields with an underscore in their name are missing from the response. Example: service_index Any fields that are a single word (no underscore) do not exhibit this problem. Also, when not using @Groups everything works just fine.

If variables are named in camel case, they appear in the response even when using @Groups and even if the column in the DB is still named in snake case. Example: serviceIndex

v2.0.11 and v.2.1.4 tested.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 4
  • Comments: 16 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Using camel case in variables only is mandatory to develop Symfony bundles or help in the Symfony project. If your project follow PSR-12 (PSR-2 is deprecated) you can choose what do you want for variable names. I always choose snake case and have use a lot of different bundles without problems since Symfony 2, and reflection works fine in all cases. I don’t know how this issue can be closed saying “if the behavior differs from the one expected by the property info” because is a completely correct way to define a entity in Symfony and Doctrine. I hope that you can reconsider your action and try to fix it. Meanwhile, if you have this problem you can use the groups directly in the setters o getters involved. You must change from

/**
 * @ORM\Column(type="int")
 * @Groups({"baz:read", "baz:write"})
 */
private $foo_bar;

to

/**
 * @ORM\Column(type="int")
 */
private $foo_bar;

/**
 * @Groups({"baz:read"})
 */
public function getFooBar(): ?int
{
    return $this->foo_bar;
}

/**
 * @Groups({"baz:write"})
 */
public function setFooBar(int $foo_bar): self
{
    $this->foo_bar = $foo_bar;

    return $this;
}

closing, use custom metadata if the behavior differs from the one expected by the property info as @teohhanhui said