MySQL 8 文档存储中的值在存储时被转义,但在检索时未转义

发布于 2025-01-16 13:58:57 字数 1451 浏览 5 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文