Skip to content

Instantly share code, notes, and snippets.

@tanakahisateru
Last active March 21, 2021 06:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tanakahisateru/cfd47196310dafa15c073ecd44293d90 to your computer and use it in GitHub Desktop.
Save tanakahisateru/cfd47196310dafa15c073ecd44293d90 to your computer and use it in GitHub Desktop.
Symfony book problem
doctrine:
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
<?php
namespace Symfony\Bundle\MakerBundle\Doctrine;
final class DoctrineHelper
{
/**
* @return MappingDriver|LegacyMappingDriver|null
*
* @throws \Exception
*/
public function getMappingDriverForClass(string $className)
{
/** @var EntityManagerInterface $em */
$em = $this->getRegistry()->getManagerForClass($className);
if (null === $em) {
throw new \InvalidArgumentException(sprintf('Cannot find the entity manager for class "%s"', $className));
}
$metadataDriver = $em->getConfiguration()->getMetadataDriverImpl();
/////////////////// patch
// Doctrine bundle injects a proxy driver that hides target
if ($metadataDriver instanceof \Doctrine\Bundle\DoctrineBundle\Mapping\MappingDriver) {
$reflectionClass = new \ReflectionClass(get_class($metadataDriver));
$property = $reflectionClass->getProperty('driver');
$property->setAccessible(true);
$metadataDriver = $property->getValue($metadataDriver);
}
///////////////////
if (!$this->isInstanceOf($metadataDriver, MappingDriverChain::class)) {
return $metadataDriver;
}
foreach ($metadataDriver->getDrivers() as $namespace => $driver) {
if (0 === strpos($className, $namespace)) {
return $driver;
}
}
return $metadataDriver->getDefaultDriver();
}
# https://symfony.com/doc/current/the-fast-track/en/8-doctrine.html#creating-entity-classes
# ---
% symfony new --version=stable myapp
% cd myapp
% symfony composer req logger orm
% symfony composer req maker --dev
% symfony console make:entity Acme
[ERROR] Only annotation mapping is supported by make:entity, but the <info>App\Entity\Acme</info> class uses a
different format. If you would like this command to generate the properties & getter/setter methods, add your
mapping configuration, and then re-run this command with the <info>--regenerate</info> flag.
exit status 1
% php -v
PHP 8.0.3 (cli) (built: Mar 4 2021 20:42:56) ( NTS )
Copyright (c) The PHP Group
Zend Engine v4.0.3, Copyright (c) Zend Technologies
with Xdebug v3.0.2, Copyright (c) 2002-2021, by Derick Rethans
with Zend OPcache v8.0.3, Copyright (c), by Zend Technologies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment