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

Already on GitHub? Sign in to your account

Fix `data\source\Database::read()` hack #745

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
2 participants
Contributor

jails commented Dec 17, 2012

With this PR array('return' => 'array') option on queries with relations will return something like the following:

array(
    'field1' => 'value1',
    'field2' => 'value2',
    'field3' => 'value3',
    /* ... other fields ... */
    'Dotted.Relation.Path' => array(
             'field1' => 'value1',
             'field2' => 'value2',
             'field3' => 'value3',
             /* ... other fields ... */
 );

When the 'joined' strategy is used, all primary keys of relations are automagically added to the query to allow a correct hydratation of nested entities. However with the array('return' => 'array') you will get the following:

array(
    'field1' => 'value1',
    'field2' => 'value2',
    'field3' => 'value3',
    /* ... other fields ... */
    'Relation' => array( 'id' => #),
    'Relation.Sub' => array( 'id' => #),
    'Relation.Sub.Sub'  => array( 'id' => #),
    'Relation.Sub.Sub.Sub' => array( 'id' => #)
 );

If you are only interested by the root level fields, you will need something like the following for removing the "pollution":

    $result = $db->query(/*the query*/, array('return' => 'array'));
    $filtered = array_filter($result, function($v) {return !is_array($v);})

To avoid this 'tricky step", you can get only the first level using the array('return' => 'root') option.

    $result = $db->query(/*the query*/, array('return' => 'root'));

@jails jails closed this Dec 17, 2012

@jails jails reopened this Dec 17, 2012

@jails jails closed this Dec 18, 2012

Owner

nateabele commented Dec 18, 2012

@jails Maybe the same, but with Set::expand() applied?

Contributor

jails commented Dec 18, 2012

The first thing that confuses me is difference beetween array('return' => 'array') / ->to('array').
1)- Is array('return' => 'array') should return the same thing as ->to('array') ?
2)- Or 'array' means more something "raw" (i.e. with no extra processing, only fieldname matching is done) ?

The other thing that confuses me is the use case when you only need "root" datas. Sometimes you're using "joins" for selecting a correct record but due to the "automagically added key" by the strategy, you'll need to filter the result on your own.

Solution 1) the primary keys won't be added anymore, so the entity hydratation will be stoped as soon as no primary key are present.
Solution 3) Having a kind of array('root' => true/false) option for both array('return' => 'array') / array('return' => 'item')
Solution 3) Let people filtering datas with things like : $filtered = array_filter($result, function($v) {return !is_array($v);})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment