DoctrineBehaviors icon indicating copy to clipboard operation
DoctrineBehaviors copied to clipboard

access locale and current locale don't "load" object

Open mpoiriert opened this issue 6 months ago • 3 comments

The default and current local of a translation is done via the "postLoad".

When have a proxy object the post load event is only triggered if we access a property that is not in the list of "$skippedProperties"

Accessing any property that are not in the orm (and id) will not trigger the post load event.

so this code:

protected function doTranslate(?string $locale = null, bool $fallbackToDefault = true): TranslationInterface
{
   if ($locale === null) {
     $locale = $this->getCurrentLocale();
    }
       
    // ...
}

Will always return 'en' on the first call if "translate" is the first method call (proxy will not get initialize).

Which is the wrong behavior.

I have not idea how to fix this properly since it's doctrine proxy factory that decide which proper to that should be skipped.

 while ($reflector) {
   foreach ($reflector->getProperties($filter) as $property) {
     $name = $property->name;

     if ($property->isStatic() || (($class->hasField($name) || $class->hasAssociation($name)) && ! isset($identifiers[$name]))) {
       continue;
     }

     $prefix = $property->isPrivate() ? "\0" . $property->class . "\0" : ($property->isProtected() ? "\0*\0" : '');

     $skippedProperties[$prefix . $name] = true;
   }

   $filter    = ReflectionProperty::IS_PRIVATE;
   $reflector = $reflector->getParentClass();
}

mpoiriert avatar Feb 19 '24 17:02 mpoiriert

Any solution to this problem?

pdias avatar Mar 25 '24 11:03 pdias

I have solved this using fetch EAGER in my entity relation:

#[ORM\ManyToOne(fetch: 'EAGER', targetEntity: Gender::class, inversedBy: 'staffs')]
#[ORM\JoinColumn(name: 'gender', referencedColumnName: 'id')]
private $gender;

pdias avatar Mar 26 '24 09:03 pdias

See #767 for a fix.

bobvandevijver avatar Jun 11 '24 18:06 bobvandevijver