-
-
Notifications
You must be signed in to change notification settings - Fork 586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add metadata informations #637
Comments
your data structure must be
(pay attention that I have used As example: class PaginatedResult
{
protected $items;
// @exclude
protected $current_page;
// @exclude
protected $page_nr;
} and
registered for |
Hi @goetas, thanks! However I don't still understand the way in which I can add Can you provide me some additional details? |
yes you need to store somewhere the current page number and the number of pages I suggest you to have a look to :
|
But is there a way to achieve my goal without additional bundle?
but doesn't work |
can you share the code used to generate the pagination result? (not the event listener) |
Thanks @goetas
|
how your code is able to obtain the number of pages ? you are passing to the serializer only the array of the current page... right? |
The number of page can be obtained by dividing total items with the number of elements in page |
so |
no no
|
then you have to :
and the handler now you can extract the page informations, because doing |
Uhm ok.. |
Do I need to use KnpPaginatorBundle? |
no, a custom listener on |
on Doctrine\ORM\Tools\Pagination\Paginator? Can you help me with an example? |
If I try to serialize a Paginator object,
I get this error:
|
you need a custom handler for it class MyHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods()
{
return array(array(
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => 'Paginator',
'method' => 'serialize'
)
);
}
public function serializeAnyType(JsonSerializartionVisigtor $visitor, Paginator $data, array $type, Context $context)
{
// serialize your object here
}
} |
of course you need the right paginator namespace and class name |
Ok, I have removed the listener to post.serialized event since it no longer need.
But I have still doubts about the creation of metadata and results fields
|
public function serializeCollection(JsonSerializationVisitor $visitor, Collection $collection, array $type, Context $context)
{
// We change the base type, and pass through possible parameters.
$type['name'] = 'array';
$p = new StaticPropertyMetadata(get_class($collection), 'page_nr', 5);
$visitor->visitProperty($p, 5, $context);
$p = new StaticPropertyMetadata(get_class($collection), 'total_pages', 99);
$visitor->visitProperty($p, 5, $context);
// serialize collection
return $visitor->visitArray($collection->->getIterator()->toArray(), $type, $context);
} is just an example |
There is something that doesn't work...
return only the number 5, that is the total pages |
Why |
can you try to serialize |
Whats is the second parameters of visitProperty() ? visitProperty() method calls getValue() on StaticPropertyMetadata class but this is the implementation:
$obj seems to be unused About your last message, how can I serialize thanks for your patience |
can this work? |
Nothing to do, continues to return "5" instead a serialized json object
|
in my example i have intentionally omitted |
OK sorry, but the problem doesn't change. I have remove the return line, but in this code
|
Obviously, |
@matiux I guess, you are italian, please write me over skype, my id is the same as my github username |
I gess https://github.com/TheFootballSocialClub/FSCHateoasBundle/blob/master/Serializer/Handler/PagerfantaHandler.php#L107 is exactly what you need |
Thank @goetas finally works and this is the current implementation of the handler for Doctrine Paginator: class DoctrinePaginatorHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods()
{
return [
[
'direction' => GraphNavigator::DIRECTION_SERIALIZATION,
'format' => 'json',
'type' => 'Doctrine\ORM\Tools\Pagination\Paginator',
'method' => 'serializeDoctrinePaginator',
]
];
}
public function serializeDoctrinePaginator(JsonSerializationVisitor $visitor, Paginator $paginator, array $type, Context $context)
{
$totalPage = ceil($paginator->count() / 40);
$resultsType = array(
'name' => 'array',
);
if (isset($type['params'])) {
$resultsType['params'] = $type['params'];
}
$shouldSetRoot = null === $visitor->getRoot();
$data = array(
'page' => $paginator->getQuery()->getFirstResult() + 1,
'total_page' => $totalPage,
'total' => $paginator->count(),
'results' => $visitor->getNavigator()->accept($paginator->getIterator()->getArrayCopy(), $resultsType, $context),
);
if ($shouldSetRoot) {
$visitor->setRoot($data);
}
}
} |
Hi,
I'm using jms serializer (bundle) in a symfony project.
For now, I'm serializing an array of object and it's all ok.
But I need to add information about the pagination (I fetch data with doctrine) to end up to have a json like this:
As you can see, I need to add the informations about total items, current pagination page and the number of the elements in the page.
I have created a
PostSerializationSubscriber
that implementsJMS\Serializer\EventDispatcher\EventSubscriberInterface
and I have implemented thegetSubscribedEvents()
method like this:how can I edit my serialized object in
onPostSerialize()
method?The code below, add 'someKey','someValue' for each elements... but it is not what I want
Thanks
The text was updated successfully, but these errors were encountered: