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

Question: How symfony plugin detects Doctrine mapping driver ? #2077

Open
soltmar opened this issue Mar 6, 2023 · 8 comments
Open

Question: How symfony plugin detects Doctrine mapping driver ? #2077

soltmar opened this issue Mar 6, 2023 · 8 comments
Labels

Comments

@soltmar
Copy link

soltmar commented Mar 6, 2023

Hi @Haehnchen ,

Firstly thanks for the awesome plugin. It helps a lot with doctrine.

I'm using doctrine in non-symfony environment and with your plugin I had no auto-complete problems when using annotations mappings.
Since I've converted everything to Attribute mapping, auto-complete stopped working for joins. Suggestions show correctly in 'select()` though.

recording

I assume this is because annotation is default driver in plugin.

Is there any way to tell the plugin which mapping driver I am using ?

Thanks!

@soltmar
Copy link
Author

soltmar commented Mar 17, 2023

Any ideas ?

@soltmar
Copy link
Author

soltmar commented May 25, 2023

Just add in, I'm working in Laravel with Laravel-doctrine package.
Any help will be greatly appreciated.

Thanks

@Haehnchen
Copy link
Owner

sry totally missed this request.

fyi: I have have still in mind, that relations inside php attributes changed "somehow or simplified" in Doctrine. Its already on my todo list to check and extract in "the wild" project cases.

  • Feel free to provide code snippets on how you used the joins for attributes; would make it easier here.
  • by design the logic is not limited to a Symfony project and was done right from the beginning; improvements can also be done if on detection

are you having still any completion at all? e.g. field completion inside select for fields, for this snippet?


All mapping drivers iterated per file, so there no detection involved:

fr.adrienbrault.idea.symfony2plugin.doctrine.metadata.util.DoctrineMetadataUtil#MAPPING_DRIVERS

php attribute driver is located here:

public class DoctrinePhpAttributeMappingDriver implements DoctrineMappingDriverInterface {

@Haehnchen Haehnchen added the todo label May 25, 2023
@m29corey
Copy link

I think a problem might be if you omit the targetEntity of the attribute when using typed properties.

When using a *ToOne attribute, Doctrine allows you to omit the targetEntity if you type the property itself.

The attribute mapping driver appears to explicitly look only for the targetEntity of the attribute and does not take a look at the property type itself:

PhpExpectedFunctionArgument argument = PhpElementsUtil.findAttributeArgumentByName("targetEntity", attribute);

As soon as I added targetEntity to each attribute, autocomplete started populating as expected within queries.

@soltmar
Copy link
Author

soltmar commented Sep 22, 2023

I've found that autocompletion didn't work when mappings are set to PHP attributes and having targetEntity typed as string, ie:

This doesn't work:
#[ORM\OneToOne(targetEntity: 'App\Entities\Employees')]
#[ORM\OneToOne(targetEntity: '\App\Entities\Employees')]

This works:
#[ORM\OneToOne(targetEntity: \App\Entities\Employees:class)]

@m29corey
Copy link

m29corey commented Oct 3, 2023

@soltmar
Indeed, it appears that the code then expects that the targetEntity of the attribute is a class constant:

PhpExpectedFunctionArgument argument = PhpElementsUtil.findAttributeArgumentByName("targetEntity", attribute);
if (argument instanceof PhpExpectedFunctionClassConstantArgument) {
String repositoryClassRaw = ((PhpExpectedFunctionClassConstantArgument) argument).getClassFqn();
if (StringUtils.isNotBlank(repositoryClassRaw)) {
doctrineModelField.setRelation(repositoryClassRaw);
}
}

But this portion is probably written this way because referencing a class by using ::class is the recommended way, rather than a hard-coded string.

Haehnchen added a commit that referenced this issue Oct 3, 2023
…-targetEntity

#2077 support resolving "targetEntity" as string for Doctrine attribute metadata
@Haehnchen
Copy link
Owner

@soltmar i just added your case.

@soltmar
Copy link
Author

soltmar commented Oct 3, 2023

@soltmar Indeed, it appears that the code then expects that the targetEntity of the attribute is a class constant:

PhpExpectedFunctionArgument argument = PhpElementsUtil.findAttributeArgumentByName("targetEntity", attribute);
if (argument instanceof PhpExpectedFunctionClassConstantArgument) {
String repositoryClassRaw = ((PhpExpectedFunctionClassConstantArgument) argument).getClassFqn();
if (StringUtils.isNotBlank(repositoryClassRaw)) {
doctrineModelField.setRelation(repositoryClassRaw);
}
}

But this portion is probably written this way because referencing a class by using ::class is the recommended way, rather than a hard-coded string.

Yeah, but this was a "leftover" in my case when converting legacy entities to attribute based mappings.

@Haehnchen, thanks :)

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

No branches or pull requests

3 participants