Skip to content

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

@syther101

Description

@syther101

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;
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions