UnionOfRAD/lithium — pull request #715 by jails (2012-12-08 22:19:13+00:00)
Title: WARNING BC Break : `data\source\Database::fields()` && `data\model\Query::fields()` refactoring

Currently fields are stored in a raw way in `data\model\Query` and `Source::fields()` is used for exporting the fields to the desired format.

This low level field management by `data\model\Query` leads to a huge number of tests for correclty formating fields especially for `Databases` sources.

Moreover when you need to add fields to a query, this leads to a numbers of `in_array()` if you want to avoid adding them both.

This PR bring common logic to `data\model\Query::fields()`.

The setter is still unchanged.
The getter will produce:

array('id' => true, 'title' => true, 'body' => true, 'created' => true)

instead of:

array('id', 'title', 'body' , 'created' )

It accepts one level of nesting using dot notation e.g.:

array(
$query = new Query(array(
'model' => 'lithium\tests\mocks\data\model\MockQueryPost',
'type' => 'read',
'with' => 'MockQueryComment'
));
$query->fields(array('MockQueryPost.id', 'title', 'MockQueryComment.comment'));
$result = $query->fields();
$expected = array(
'id' => true,
'title' => true,
'MockQueryComment' => array(
'comment' => true
)
);
$this->assertEqual($expected, $result);

I'm not sure MongoDb allow dot notation for fields selection, but a simple `Set::flatten()` would be enough for `MongoDb::fields()`

Finally all non alphanumeric fields and object fields are stored as "special" in the '0' key e.g.:

array(
$query = new Query(array(
'model' => 'lithium\tests\mocks\data\model\MockQueryPost',
'type' => 'read',
'with' => 'MockQueryComment'
));
$query->fields(array('id', 'lower(MockQueryPost.id)', (object) 'count(*)'));
$result = $query->fields();
$expected = array(
'id' => true,
array(
'lower(MockQueryPost.id)',
(object) 'count(*)'
)
);
$this->assertEqual($expected, $result);