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

Proxy generator dumps incorrect default value for enums into lazy properties defaults #9371

Closed
mbabker opened this issue Jan 12, 2022 · 4 comments · Fixed by doctrine/common#955
Assignees
Labels

Comments

@mbabker
Copy link
Contributor

mbabker commented Jan 12, 2022

Bug Report

Q A
BC Break no
Version 2.11.0

Summary

When updating an app to ORM 2.11 and changing an existing string field to use enums, when the field has a default value, the proxy is incorrectly generated and results in errors similar to "ReflectionException: Class "App\Entity\Card\App\Suit" does not exist." This is because the proxy class doesn't have the leading slash to make it a FQCN, so it's treated as a relative namespace when accessed later.

Current behavior

The proxy class' $lazyPropertiesDefaults array dumps defaults with a class name that's not fully qualified, resulting in a value that's perceived as a relative namespace path.

How to reproduce

Expected behavior

The $lazyPropertiesDefaults array should use fully qualified class names to avoid class not found errors, or if it cannot support enums with lazy loading, the proxy generator skips the field.

As a temporary workaround, I can move these default assignments to the class constructor.

@mbabker mbabker changed the title Proxy generator dumps incorrect value for lazy properties defaults Proxy generator dumps incorrect default value for enums into lazy properties defaults Jan 12, 2022
@derrabus derrabus added the Bug label Jan 12, 2022
@derrabus
Copy link
Member

Thank you. Would you be able to work on a bugfix?

@mbabker
Copy link
Contributor Author

mbabker commented Jan 13, 2022

Doubtful, anything meaningful is going to require more engine knowledge than I have.

I can monkey-patch the ProxyGenerator so it doesn't consider enums as a lazy-loadable property:

diff --git a/lib/Doctrine/Common/Proxy/ProxyGenerator.php b/lib/Doctrine/Common/Proxy/ProxyGenerator.php
index f71cb915..29e0de4a 100644
--- a/lib/Doctrine/Common/Proxy/ProxyGenerator.php
+++ b/lib/Doctrine/Common/Proxy/ProxyGenerator.php
@@ -980,6 +980,12 @@ EOT;
                 continue;
             }
 
+            if (function_exists('enum_exists')) {
+                if (!$property->getType()->isBuiltin() && enum_exists($property->getType()->getName())) {
+                    continue;
+                }
+            }
+
             $properties[] = $name;
         }
 

But, getting it to actually support enums is where my skills end. The issue looks to be in how var_export() exports things and I don't have the engine or regex knowledge to be able to mess with that output and get it to fix things.

@kimhemsoe
Copy link
Member

kimhemsoe commented Jan 13, 2022

Can confirm that the issue is with var_export. Ex:

<?php

namespace Proxies;

class AProxy {
    public static $lazyPropertiesDefaults = array (
  'suit' => 
  App\Suit::Hearts,
);
}

Simplified version of what ProxyGenerator is generation

Possible solution could prefix with preg_replace()

@beberlei
Copy link
Member

beberlei commented Feb 2, 2022

Please upgrade to doctrine/common 3.2.2 to fix this issue in your projects.

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

Successfully merging a pull request may close this issue.

4 participants