Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fields showing as null when fetching object from database if properties are private. #134

Open
syther101 opened this issue Jan 19, 2024 · 0 comments

Comments

@syther101
Copy link

Unsure if this is something already aware of. But we found out today that if a property on an object is marked private, when fetching that document from the database, the field will fail to be serialised. You can see this by testing the value of 114 of the JsonDocumentType.

I can add more context if needed, but if this is to be expected, maybe a note could be added to the documentation. I'd be happy to do so if this is indeed something you'd like.

In the below example, the value of dismissedWelcomeCardAt will return null when fetched from the database unless the property is public.

Document field on our User entity:

<?php
declare(strict_types=1);

namespace App\Entity\User;

use App\Entity\User\Preferences\UserOnboardingPreferences;

class UserPreferences
{
    private UserOnboardingPreferences $userOnboardingPreferences;

    public function __construct()
    {
        $this->userOnboardingPreferences = new UserOnboardingPreferences();
    }

    public function getUserOnboardingPreferences(): UserOnboardingPreferences
    {
        return $this->userOnboardingPreferences;
    }

    public function setUserOnboardingPreferences(UserOnboardingPreferences $userOnboardingPreferences): void
    {
        $this->userOnboardingPreferences = $userOnboardingPreferences;
    }
}

Nested onboarding object:

<?php
declare(strict_types=1);

namespace App\Entity\User\Preferences;

use DateTime;

class UserOnboardingPreferences
{
    private ?DateTime $dismissedWelcomeCardAt = null;

    public function getDismissedWelcomeCardAt(): ?DateTime
    {
        return $this->dismissedWelcomeCardAt;
    }

    public function dismissWelcomeCard(): void
    {
        $this->dismissedWelcomeCardAt = new DateTime();
    }

    public function hasDismissedWelcomeCard(): bool
    {
        return $this->getDismissedWelcomeCardAt() !== null;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant