gpt4 book ai didi

php - 火力地堡PHP : Updating also deletes the other children

转载 作者:搜寻专家 更新时间:2023-10-31 20:57:32 25 4
gpt4 key购买 nike

我正在使用 Firebase PHP 管理 SDK:https://firebase-php.readthedocs.io/en/stable/realtime-database.html#update-specific-fields

这是更新特定字段的示例:

$uid = 'some-user-id';
$postData = [
'title' => 'My awesome post title',
'body' => 'This text should be longer',
];

// Create a key for a new post
$newPostKey = $db->getReference('posts')->push()->getKey();

$updates = [
'posts/'.$newPostKey => $postData,
'user-posts/'.$uid.'/'.$newPostKey => $postData,
];

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

由此,我创建了一个用户类,并且在其中我有一个更新功能。像这样:

public function update() {

$data = array('users' => array('1' => 'David'));
$this->database->getReference()->update($data);

return true;
}

在我的数据库中我有这样的结构:

enter image description here

现在,如果我运行该函数 $users->update();

它删除了另一个 child ,只留下了大卫。像这样:

enter image description here

如何只更新指定键的特定值而不覆盖其他数据?

最佳答案

这里没有特定于 PHP 的内容。这就是实时数据库更新操作的工作方式。如果你想要最小更新,你必须定位你想要更新的最深的键。在您的情况下,由于您存储的是数组类型对象,因此键是您编写的数组项的数字索引。如果要修改其中一个,则需要构建一个引用,其中包含要更新的子编号。在这种情况下,不会触及任何兄弟值。

试试这个:

$this->database->getReference('users')->update(array('1' => 'David'));

请注意,这里的更新根植于“用户”,而您只是更新了它的直接子节点“1”。

关于php - 火力地堡PHP : Updating also deletes the other children,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54120048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com