MySQL 8 文档存储中的值在存储时被转义,但在检索时未转义
我使用 xdevapi 运行 PHP 8 和 MySQL 8。一切都运行良好,但我遇到了将数据存储在集合中文档中的问题。
我在一个类中有以下方法:
use YYY\DatabaseDS;
final class Draft
{
private mysql_xdevapi\Collection $collection;
/**
* @param DatabaseDS $pdo The database connection.
*/
public function __construct(
public DatabaseDS $pdo,
) {
$this->collection = $this->pdo->getCollection('drafts');
}
/**
* @param string $draftID The primary key for the draft.
* @param array $data The SOW data in object format
* @return void
*/
private function updateDraftData(string $draftID, array $data): void
{
$doc = json_encode($data);
$this->collection->replaceOne($draftID, $doc);
}
/**
* @param string $hash The identifying hash for the draft.
* @return array
*/
private function getDraftStructure(string $hash): array
{
$results = $this->collection->find('hash = :hash')
->bind(['hash' => $hash])
->execute();
return $results->fetchOne();
}
}
我还有一个与此类似的文档:
{"name": "Alfred", "age": 42, "job": "Butler/Maid"}
保存文档时,job
的值被转义,因此它看起来像 Butler\/Maid< /代码>。检索值时就会出现问题。我希望检索到的值与保存到文档中的值相同,但转义值仍然存在。如果我再次保存它,它会保存为
Butler\\\/Maid
,再次转义所有内容。
有没有一种方法可以检索该值而无需手动将其更改回来?或者我是否必须考虑再次使用多个数据库系统?
I have PHP 8 and MySQL 8 running using the xdevapi. Everything is working well with it, but I am running into an issue storing data in a document in a collection.
I have the following methods in a class:
use YYY\DatabaseDS;
final class Draft
{
private mysql_xdevapi\Collection $collection;
/**
* @param DatabaseDS $pdo The database connection.
*/
public function __construct(
public DatabaseDS $pdo,
) {
$this->collection = $this->pdo->getCollection('drafts');
}
/**
* @param string $draftID The primary key for the draft.
* @param array $data The SOW data in object format
* @return void
*/
private function updateDraftData(string $draftID, array $data): void
{
$doc = json_encode($data);
$this->collection->replaceOne($draftID, $doc);
}
/**
* @param string $hash The identifying hash for the draft.
* @return array
*/
private function getDraftStructure(string $hash): array
{
$results = $this->collection->find('hash = :hash')
->bind(['hash' => $hash])
->execute();
return $results->fetchOne();
}
}
I also have a document similar to this one:
{"name": "Alfred", "age": 42, "job": "Butler/Maid"}
When saving the document, the value for job
is being escaped so it looks like Butler\/Maid
. The problem comes when retrieving the value. I would expect the value I retrieve to be identical to what I saved to the document, but the escaped value is still there. If I save it again, it saves as Butler\\\/Maid
escaping everything another time.
Is there a way to retrieve the value without having to manually change it back? Or am I going to have to look at going back to using multiple database systems again?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论