1
votes

I'm sending to a Twig view a doctrine result. Then in Twig:

  {% for key, value in result|array%}
    {{key}} : {{value}}

The array filter is a extension that converts the Doctrine Result Object into an Array using $result = (array) $result;

And... {{key}} is rendering the field name, but preceded of the full Bundle/Entity path... Any ideas of getting rid of this prefix?

Thanks in advance ;)

2
Could you give an example of how the prefix looks?chh

2 Answers

0
votes

One method would be to use the replace filter and replace the prefix with nothing, if it's a common prefix for all items.

The other method would be to split on a known separator, e.g. \ for class names, and then get the last part with the last filter.

For example:

{{ key|split('\\')|last }}
0
votes

The solution was calling my getFieldnames service from the new array filter... and then replace the keys from the array... Maybe it's not the best option... let me know if there is another better , I mean deeper...

 public function arrayFilter($entity) {
    $fieldnames = $this->container->getFieldnames($entity);
    if (is_object($entity)) {
        $response = (array) $entity;
        $i = 0;
        foreach ($response as $k => $v) {

            unset($response[$k]);
            $new_key = $fieldnames[$i];
            $response[$new_key] = $v;
            $i++;
        }
        return $response;
    }
    return null;
}