Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving deeply nested data in Symfony 3 Parameterbags

Tags:

php

symfony

In Symfony 2 you could do something like this to get nested query string data:

// mixed getInt(string lookup, mixed default, bool deep)
$request->query->getInt('page[number]', 1, true);

In Symfony 3 it looks like the ability to retrieve deeply nested data has been removed in favour of retrieving the top level array and getting the data directly.

// mixed get(string key, mixed default)
(int) $request->query->get('page', ['number' => 1])['number']

Can anyone confirm that I'm not missing something here and this is now the preferred method to retrieve nested data in parameter bags?

I did look over the 3.1 docs and all examples reference retrieval of the array with no options to query for deeply nested data.

like image 738
David Barker Avatar asked Apr 19 '26 11:04

David Barker


1 Answers

You could use the PropertyAccess component:

$query = $request->query->all();

$accessor = PropertyAccess::createPropertyAccessor();

$page = (int) $accessor->getValue($query, '[page][number]');
like image 128
ShiraNai7 Avatar answered Apr 21 '26 00:04

ShiraNai7