Skip to content

Manage AppBundle:MyEntity #20

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

Closed
soullivaneuh opened this issue Jan 22, 2018 · 13 comments
Closed

Manage AppBundle:MyEntity #20

soullivaneuh opened this issue Jan 22, 2018 · 13 comments

Comments

@soullivaneuh
Copy link

This is a valid notation for getRepository, but it's not managed.

@ondrejmirtes
Copy link
Member

Care to elaborate? Where can this definition be obtained and is it suitable from the point of a static analyzer?

@hkdobrev
Copy link
Contributor

$config = $entityManager->getConfiguration()
$config->addEntityNamespace('Foo', 'Foo\\Bar');
// Use later as:
$entityManager->getRepository('Foo::Author');
// where Author is in the `Foo\Bar` namespace.

We don't use the Symfony framework but needed to add a namespace to be used by tools expecting it. So this could be used even outside the context of bundles.

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jan 23, 2018

I need a way how to translate 'Foo::Author' -> Foo\Bar\Author.

@hkdobrev
Copy link
Contributor

@ondrejmirtes Sure! Based on the above example:

$alias = 'Foo';
$namespace = $config->getEntityNamespace('Foo');
echo $namespace;
// Foo\Bar

@ondrejmirtes
Copy link
Member

ondrejmirtes commented Jan 23, 2018 via email

@hkdobrev
Copy link
Contributor

An alternative:

// this recognises both aliases and namespace
$metadata = $entityManager->getClassMetadata('Foo::Author');
echo $metadata->getName();
// Foo\Bar\Author

@hkdobrev
Copy link
Contributor

For this and other advanced Doctrine introspections to work in PHPStan, I'd think one needs to load the entity manager or registry in the PHPStan bootstrap.

@mhujer
Copy link
Contributor

mhujer commented Jan 23, 2018

Isn't it possible to pass a entity classname to getRepository()? See the second example here http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#by-primary-key

I think I have seen a PR deprecating this "aliasing" functionality in some of the Doctrine repositories, but I can't find it right now. Maybe @Majkl578 would know?

@hkdobrev
Copy link
Contributor

hkdobrev commented Jan 23, 2018

Isn't it possible to pass a entity classname to getRepository()?

@mhujer It is definitely possible to pass the entity FQCN everywhere including getRepository. We are using it everywhere, but we needed to add a namespace to integrate with a library developed for the Symfony framework. I guess many people using Doctrine with Symfony bundle would need that as the convention there in the existing code is to use the bundle name as the namespace for entities in that bundle.

I think I have seen a PR deprecating this "aliasing" functionality in some of the Doctrine repositories, but I can't find it right now.

Thanks for that! Here is the PR: doctrine/orm#6935 This would be a breaking change in Doctrine 3.

I guess it's up to @ondrejmirtes to decide whether a functionality from Doctrine 2 which is being removed is worth supporting. Doctrine 3 is not out yet and I guess a lot of Doctrine-based projects would not be upgraded immediately.

@Majkl578
Copy link
Contributor

Majkl578 commented Jan 23, 2018

Actually you should always use Foo::class as of PHP 5.5, it's more predictable and easier to analyze by SA tools. As @hkdobrev referenced, this feature will be removed in ORM 3.0. Therefore I think it's not worth solving as it would require additional effort for deprecated feature...

@ondrejmirtes
Copy link
Member

I agree, if you want statically analyzable code, you should always Foo::class. Since this feature is removed in ORM 3.0, I'm closing this.

@soullivaneuh
Copy link
Author

Looks fair enough. Thanks for the tip! 👍

@github-actions
Copy link

github-actions bot commented May 2, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 2, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants