0

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?

IMSoP
  • 89,526
  • 13
  • 117
  • 169
Joseph
  • 1,988
  • 14
  • 21
  • Maybe you'd unescape retrieved value before its using? – Akina Mar 24 '22 at 05:01
  • @Akina The unescaping is a manual process using regex. Or at least I haven't found a good function to do it reliably. But then again, why do I need to unescape it in the first place? I don't escape it on entry. The PHP PECL library does that. It should reverse that on retrieval. And that is assuming there aren't any other changes that happen to the data that I haven't found yet. – Joseph Mar 24 '22 at 06:36

0 Answers0