Download the PHP package mash/mysql-json-serializer without Composer
On this page you can find all versions of the php package mash/mysql-json-serializer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mash/mysql-json-serializer
More information about mash/mysql-json-serializer
Files in mash/mysql-json-serializer
Package mysql-json-serializer
Short Description Tools for serialization data to json from mysql
License MIT
Homepage https://github.com/AndreyMashukov/mysql-json-serializer
Informations about the package mysql-json-serializer
mysql-json-serializer
This solution will help you to get serialized data from mysql
About package
This package allows to get json objects already mapped by created mapping scheme. You will not lost time after query because you will get json from database.
Install
Examples
You will be able to make easy difficult query and get already mapped data. For example I will show you some SQL queries which I created by this package.
Example 1 (SELECT JSON WITH OneToMany relation field)
result
Example 2 (SELECT JSON WITH ManyToOne relation field)
result
Example 3 (SELECT JSON WITH ManyToMany relation field)
result
Example 4 (OneToOne relation)
result
Symfony 4 (flex) Integration
Configure
Add this to services.yaml:
Setup your Entities
Below you will find information how to configure your entities for auto serialization
Look to example on top, it's really easy to configure the serialization, looks like using JMSSerializer, or Symfony serializer, when I was writing this feature, I use my experience in Symfony, and take only good things.
If you want to expose some field add @MysqlJSON\Expose(groups={"page_full"})
and add @Rest\View(serializerGroups={"Default", "page_full"})
on controller method
note
: If you use @MysqlJSON\Expose
without setting groups, field will have Default
group
Lets see, how to return result from controller
Method $builder->jsonPagination($page, $limit);
allows to paginate data into mysql, and return json with all relations, pages, total count and other pagination fields
Example:
Also you can get the single serialized object by $builder->jsonObject($page->getId());
method, it will return serialized object
Example:
You can use your CustomTypes for implementation you custom serialization logic, for example, by default MySQL serialize blob as base64 decoded value in JSON, but if we add @MysqlJSON\Expose(groups={"Default", "page_full"}, type="Mash\MysqlJsonSerializer\Wrapper\Type\Blob")
annotation, FieldWrapper will use this type in serialization and return text.
Example of CustomType:
It's easy to write your custom type, just implement interface CustomTypeInterface
and use MySQL functions into.
Updates
v. 2.0.5
Now you can create virtual properties, it allows to avoid database denormalization and keep relation model.
example of Entity configuration, just add map to class doc comment.
result:
All versions of mysql-json-serializer with dependencies
friendsofsymfony/rest-bundle Version ^2.3
sensio/framework-extra-bundle Version ^5.1
symfony/http-kernel Version ^4.1
symfony/event-dispatcher Version ~3.4|~4.0
symfony/doctrine-bridge Version ~3.0|~4.0
doctrine/orm Version ^2.3