PHP Как добавить / вставить таблицы в таблицу в Firebase - PullRequest
2 голосов
/ 20 марта 2019

Как добавить / вставить таблицы в таблицу в Firebase?

У меня есть таблица с именем "users" в моей базе данных Firebase, и она выглядит так:

enter image description here

Как видите, ключ - это идентификатор, а значение - это имя пользователя.Это простая структура, но я хочу, чтобы в моей основной таблице «users» было несколько таблиц, например:

enter image description here

Я кодирую в PHP, и это функцияя использую для вставки данных:

public function insert(array $data) {

    if (empty($data) || !isset($data)) {
        return false;
    }

    foreach($data as $key => $value) {
        $this->database->getReference()->getChild($this->table)->getChild($key)->set($value);
    }

    return true;
}

Спасибо

1 Ответ

2 голосов
/ 20 марта 2019

Сделай что-нибудь подобное

$postData = [
    'UserWatchList' => [
       ['Player' => 'Player1'],
       ['Player' => 'Player2']
    ]
];

// Create a key for a new user in users table
// Note: if you have user key then you don't need to create a key
$newUserKey = $db->getReference('users')->push()->getKey();    

$updates = [
    'users/'.$newPostKey => $postData,
];    

$db->getReference() // this is the root reference
   ->update($updates);

Ссылка: https://firebase -php.readthedocs.io / en / latest / realtime-database.html # update-specific-fields

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...