0
我還沒有找到我的問題的答案,所以我在這裏創建了一個帖子。MySQL到JSON(firebase)
我的客戶端有一個狗窩管理應用程序,它將從MySQL遷移到Firebase以獲得更好的性能。
因此,我將MySQL數據庫中的表導出爲JSON,現在我想爲每個用戶創建一個JSON對象。
的MySQL(JSON):
{
"users": [
{
"id": 1,
"n_adherent": "er654rds32",
"name": "Michaud",
"email": "[email protected]",
...
},
{
"id": 2,
"n_adherent": "yt154fge91",
"name": "Name2",
"email": "[email protected]",
...
},
{...},
...
],
"dogs": [
{
"id": 1,
"name": "dog1",
"user": "1",
...
},
{
"id": 2,
"name": "dog2",
"user": "1",
...
},
{
"id": 3,
"name": "dog3",
"user": "2",
...
},
{...},
...
],
"vaccination": [
{
"id": 1,
"dog_id": 1,
...
},
{
"id": 2,
"dog_id": 1,
...
},
{
"id": 3,
"dog_id": 1,
...
},
{
"id": 4,
"dog_id": 2,
...
},
{...},
...
],
"deworming": [
{
"id": 1,
"dog_id": 1,
...
},
{...},
...
],
"kennel_maintenance": [
{
"id": 1,
"user_id": 1,
"date": "2016-01-01",
...
},
{...},
...
]
}
我希望得到什麼:
{
"users": [
{
"id": 1,
"n_adherent": "er654rds32",
"name": "Michaud",
"email": "[email protected]",
"kennel_maintenance": [
{
"id": 1,
"date": "2016-01-01",
...
},
{...}
],
"dogs": [
{
"id": 1,
"name": "dog1",
"vaccination": [
{
"id": 1,
...
},
{
"id": 2,
...
}
],
"deworming": [
{
"id": 1,
...
}
],
...
},
],
...
},
{...}, <-- user id = 2
... <-- other user
]
}
或(也許更好):
{
"1": [ <-- user 1
{
"id": 1,
"n_adherent": "er654rds32",
"name": "Michaud",
"email": "[email protected]",
"kennel_maintenance": [
{
"id": 1,
"date": "2016-01-01",
...
},
{...}
],
"dogs": [
{
"id": 1,
"name": "dog1",
"vaccination": [
{
"id": 1,
...
},
{
"id": 2,
...
}
],
"deworming": [
{
"id": 1,
...
}
],
...
},
],
...
}
],
"2": [ <-- user 2
...
],
... <-- other user
}
我用PHP解析文件但我似乎無法在用戶對象中推送對象。
一塊我的代碼:
<?php
$jsonString = file_get_contents('database.json');
$data = json_decode($jsonString, true);
$list = array();
$membresGroup = array();
$vaccinationGroup = array();
$vermifugationGroup = array();
$entretiensGroup = array();
// Users
foreach ($data['membres'] as $key) {
// if($key['id'] === "99995"){ <-- for test I ask for this user
// Chiens
foreach ($data['chiens'] as $k) {
if ($k['user_id'] === "99995") {
$list[] = $k;
$key['chiens'] = $list;
// Vaccination
$vaccinations = $data['vaccination'];
foreach ($vaccinations as $object) {
//var_dump($object);
if ($object['chien_id'] === $k['id']) {
if (!array_key_exists($object['chien_id'], $vaccinationGroup)) {
$newObjectVaccination = new stdClass();
$newObjectVaccination->chien_id = (int)$object['chien_id'];
$newObjectVaccination->user_id = (int)$object['user_id'];
//$vaccinationGroup[$object['chien_id']] = $newObjectVaccination;
}
$taskObjectVaccination = new stdClass();
$taskObjectVaccination->id = (int)$object['id'];
$taskObjectVaccination->type = (boolean)$object['type'];
$taskObjectVaccination->chppil = (boolean)$object['chppil'];
$taskObjectVaccination->date = $object['date'];
$taskObjectVaccination->date_renouvellement = $object['date_renouvellement'];
$taskObjectVaccination->parvovirose = (boolean)$object['parvovirose'];
$vaccinationGroup[$object['chien_id']]->vaccination[] = $taskObjectVaccination;
}
}
$vaccinationGroup = array_values($vaccinationGroup);
// Deworming
$vermifugations = $data['vermifugation'];
foreach ($vermifugations as $object) {
//var_dump($object);
if ($object['chien_id'] === $k['id']) {
if (!array_key_exists($object['chien_id'], $vermifugationGroup)) {
$newObjectVermifugation = new stdClass();
$newObjectVermifugation->chien_id = (int)$object['chien_id'];
$newObjectVermifugation->user_id = (int)$object['user_id'];
$newObjectVermifugation->vermifugation = array();
$vermifugationGroup[$object['chien_id']] = $newObjectVermifugation;
}
$taskObjectVermifugation = new stdClass();
$taskObjectVermifugation->id = (int)$object['id'];
$taskObjectVermifugation->nom = $object['nom'];
$taskObjectVermifugation->type = $object['type'];
$taskObjectVermifugation->date = $object['date'];
$taskObjectVermifugation->date_renouvellement = $object['date_renouvellement'];
$vermifugationGroup[$object['chien_id']]->vermifugation[] = $taskObjectVermifugation;
}
}
$vermifugationGroup = array_values($vermifugationGroup);
}
}
$entretiens = $data['entretien'];
foreach ($entretiens as $object) {
//var_dump($object);
if ($object['user_id'] === $key['id']) {
if (!array_key_exists($object['user_id'], $entretiensGroup)) {
$newObjectEntretiens = new stdClass();
$newObjectEntretiens->user_id = (int)$object['user_id'];
$newObjectEntretiens->entretiens = array();
$entretiensGroup[$object['user_id']] = $newObjectEntretiens;
}
$taskObjectEntretiens = new stdClass();
$taskObjectEntretiens->id = (int)$object['id'];
$taskObjectEntretiens->date = $object['date'];
$taskObjectEntretiens->desinfection = (boolean)$object['desinfection'];
$taskObjectEntretiens->nom_desinfection = $object['nom_desinfection'];
$taskObjectEntretiens->desinfection_date = $object['desinfection_date'];
$taskObjectEntretiens->parasitaire = (boolean)$object['parasitaire'];
$taskObjectEntretiens->nom_antipara = $object['nom_antipara'];
$taskObjectEntretiens->parasitaire_date = $object['parasitaire_date'];
$taskObjectEntretiens->autres = (boolean)$object['autres'];
$taskObjectEntretiens->autres_date = $object['autres_date'];
$taskObjectEntretiens->autres_note = $object['autres_note'];
$entretiensGroup[$object['user_id']]->entretiens[] = $taskObjectEntretiens;
}
}
$entretiensGroup = array_values($entretiensGroup);
// echo json_encode($key, true); <-- I get all dogs of the user 99995
// echo json_encode($vaccinationGroup, true); <-- I get all the vaccinations dogs User 99995
// echo json_encode($vermifugationGroup, true); <-- I get all the deworming dogs User 99995
// echo json_encode($entretiensGroup, true); <-- I have all kennel maintenance from user 99995
// }
}
// $newJsonString = json_encode($output); <-- $output is not define yet but this is the result of all operations
// file_put_contents('firebase.json', $newJsonString); <-- write result in firebase.json -> to import in firebase app
?>
我嘗試使用array_push()函數,但我沒有得到預期的結果。
對不起,我不擅長PHP,我學習。 如果有人能告訴我如何手動進行,因爲它是不可能的,有超過9000個用戶...
謝謝你的幫助,Germain。
通過嵌套數據結構,您會違反Firebase的其中一項建議:[謹慎使用嵌套數據](https://www.firebase.com/docs/web/guide/structuring-data.html)。無論如何,您可能會想要在該節目指南中花費一些時間,這樣可以避免更多的麻煩。 –