1. Go to this page and download the library: Download mash/mysql-json-serializer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mash / mysql-json-serializer example snippets
use \Mash\MysqlJsonSerializer\QueryBuilder\Table\JoinStrategy\FieldStrategy;
use \Mash\MysqlJsonSerializer\Wrapper\FieldWrapper;
use \Mash\MysqlJsonSerializer\QueryBuilder\Table\Table;
use \Mash\MysqlJsonSerializer\Wrapper\Mapping;
use \Mash\MysqlJsonSerializer\QueryBuilder\QueryBuilder;
use \Mash\MysqlJsonSerializer\Service\TableManager;
$oneToManyTable = (new Table('advert_group', 'adg', 'adg_id'))
->addSimpleField('adg_id')
->addSimpleField('adg_name')
;
$table = (new Table('estate', 'est', 'est_id'))
->addSimpleField('est_id')
->addSimpleField('est_name')
->addOneToManyField($oneToManyTable, 'advert_groups', new FieldStrategy('adg_estate'));
$mapping = new Mapping();
$mapping
->addMap($table, 'est_id', 'id')
->addMap($table, 'est_name', 'name')
->addMap($oneToManyTable, 'adg_id', 'id')
->addMap($oneToManyTable, 'adg_name', 'name');
$builder = new QueryBuilder($table, new FieldWrapper($mapping, new TableManager()));
$builder
->setOffset(2)
->setLimit(1);
$sql = $builder->jsonArray();
use \Mash\MysqlJsonSerializer\QueryBuilder\Table\JoinStrategy\FieldStrategy;
use \Mash\MysqlJsonSerializer\Wrapper\FieldWrapper;
use \Mash\MysqlJsonSerializer\QueryBuilder\Table\Table;
use \Mash\MysqlJsonSerializer\Wrapper\Mapping;
use \Mash\MysqlJsonSerializer\QueryBuilder\QueryBuilder;
use \Mash\MysqlJsonSerializer\Service\TableManager;
$manyToOneTable = (new Table('estate', 'est', 'est_id'))
->addSimpleField('est_id')
->addSimpleField('est_name')
;
$table = (new Table('advert_group', 'adg', 'adg_id'))
->addSimpleField('adg_id')
->addSimpleField('adg_name')
->addManyToOneField($manyToOneTable, 'estate', new FieldStrategy('adg_estate'))
;
$mapping = new Mapping();
$mapping
->addMap($manyToOneTable, 'est_id', 'id')
->addMap($manyToOneTable, 'est_name', 'name')
->addMap($table, 'adg_id', 'id')
->addMap($table, 'adg_name', 'name');
$builder = new QueryBuilder($table, new FieldWrapper($mapping, new TableManager()));
$builder
->setOffset(2)
->setLimit(2);
$sql = $builder->jsonArray();
use \Mash\MysqlJsonSerializer\QueryBuilder\Field\CrossReference\Reference;
use \Mash\MysqlJsonSerializer\QueryBuilder\Table\JoinStrategy\ReferenceStrategy;
use \Mash\MysqlJsonSerializer\QueryBuilder\Field\CrossReference\Pair;
use \Mash\MysqlJsonSerializer\Wrapper\FieldWrapper;
use \Mash\MysqlJsonSerializer\QueryBuilder\Table\Table;
use \Mash\MysqlJsonSerializer\Wrapper\Mapping;
use \Mash\MysqlJsonSerializer\QueryBuilder\QueryBuilder;
use \Mash\MysqlJsonSerializer\Service\TableManager;
$photo = new Table('photo', 'pht', 'pht_id');
$advert = (new Table('advert', 'adv', 'adv_id'))
->addSimpleField('adv_id')
->addSimpleField('adv_type')
;
$reference = new Table('photo_xref', 'xrf');
$strategy = new ReferenceStrategy(
new Reference(
new Pair($advert, 'adv_id'),
new Pair($reference, 'xref_adv_id')
),
new Reference(
new Pair($photo, 'pht_id'),
new Pair($reference, 'xref_pht_id')
)
);
$mapping = new Mapping();
$mapping
->addMap($advert, 'adv_id', 'id')
->addMap($advert, 'adv_type', 'type')
->addMap($photo, 'pht_id', 'id')
->addMap($photo, 'pht_hash', 'hash')
;
$builder = new QueryBuilder($advert, new FieldWrapper($mapping, new TableManager()));
$builder->setLimit(2);
$photo
->addSimpleField('pht_id')
->addSimpleField('pht_hash')
;
$advert->addManyToManyField($photo, 'photos', $strategy);
$sql = $builder->jsonArray();
use \Mash\MysqlJsonSerializer\QueryBuilder\Table\JoinStrategy\FieldStrategy;
use \Mash\MysqlJsonSerializer\Wrapper\FieldWrapper;
use \Mash\MysqlJsonSerializer\QueryBuilder\Table\Table;
use \Mash\MysqlJsonSerializer\Wrapper\Mapping;
use \Mash\MysqlJsonSerializer\QueryBuilder\QueryBuilder;
use \Mash\MysqlJsonSerializer\Service\TableManager;
$oneToOneTable = (new Table('page', 'pge', 'pge_id'))
->addSimpleField('pge_id')
->addSimpleField('pge_url')
;
$table = (new Table('advert', 'adv', 'adv_id'))
->addSimpleField('adv_id')
->addSimpleField('adv_type')
->addManyToOneField($oneToOneTable, 'page', new FieldStrategy('adv_page'))
;
$mapping = new Mapping();
$mapping
->addMap($oneToOneTable, 'pge_id', 'id')
->addMap($oneToOneTable, 'pge_url', 'url')
->addMap($table, 'adv_id', 'id')
->addMap($table, 'adv_type', 'type');
$builder = new QueryBuilder($table, new FieldWrapper($mapping, new TableManager()));
$builder
->setOffset(2)
->setLimit(2);
$expected = "SELECT JSON_ARRAYAGG(JSON_OBJECT('id',adv_res.adv_id,'type',adv_res.adv_type,'page',(SELECT JSON_OBJECT('id',pge.pge_id,'url',pge.pge_url) FROM page pge WHERE pge.pge_id = adv_res.adv_page LIMIT 1))) FROM (SELECT * FROM advert adv LIMIT 2 OFFSET 2) adv_res";
$sql = $builder->jsonArray();