1

In a Zend Framework & Apiglity driven application I'm using Zend\Paginator\Paginator for my collection objects. A Paginator object contains a ResultSet, so flat data structures like:

{
    "project": [
        {
            "id": "1",
            "title": "...",
            ...
        },
        ...
    ]
}

The result output after the processing it by the Hal REST controller plugin / view helper (ZF\Hal\Plugin\Hal) looks like this:

{
    "_links": {...},
    "_embedded": {
        "project": [
            {
                "id": "1",
                "title": "...",
                ...
            },
            ...
        ]
    },
    "page_count": 3,
    "page_size": 25,
    "total_items": 72
}

Now, I want to nest a new level to it, e.g. every project should contain a list of images. The result output should look as follows:

{
    "_links": {...},
    "_embedded": {
        "project": [
            {
                "id": "1",
                "title": "...",
                "_embedded": {
                    "images": [
                        {
                            "id": "1",
                            "src": "...",
                            ...
                        },
                        ...
                    ]
                }
            },
            ...
        ]
    },
    "page_count": 3,
    "page_size": 25,
    "total_items": 72
}

I know, how to get an array copy of the data and extend it. But I have to return the Paginator object itselft.

(How) Can Zend\Paginator\Paginator's data be modified?

automatix
  • 14,018
  • 26
  • 105
  • 230

1 Answers1

0

You can get your array and extend it and then create a new Paginator object with that modified array.

itrascastro
  • 775
  • 6
  • 14
  • Thank you for your answer! I don't think, that would help, since the new `Paginator` will miss all the "meta information" like links, the page count, page size, etc. But anyway, let us try. I cannnot create a new `Paginator` with predefined data (retrieved from another one and modified), since the [constructor](https://github.com/zendframework/zf2/blob/master/library/Zend/Paginator/Paginator.php#L275) expects an `AdapterInterface` or an `AdapterAggregateInterface`, no array. I also don't see any appropriate setter. Can you please post an example? – automatix Mar 11 '15 at 11:32
  • The part with createing a new `Paginator`, that contains the enriched data, works! I only needed to create a an `Adapter` class, that extends `Zend\Paginator\Adapter\ArrayAdapter`. I'm passing the data to it and then create a new `Paginator` with my `ProjectAdapter` as adapter. So, at lease this step works. But as I already said, it causes issues with the pagination -- there is only one page, no `next`, no `prev`. How can that be solved? – automatix Mar 11 '15 at 12:08
  • I've just tried to modify the `ProjectAdapter#count`. Well, it works and I get my `HAL` links. But it only works for the first page. Other pages are empty. Do you have an idea, how to make it work for all pages? It would be great, if you could provide an example. – automatix Mar 11 '15 at 12:33