mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Generate entities and mapping fields name #5031
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @pokakhoaitay on GitHub (Mar 1, 2016).
I have a table with this structure in db:
I use this code to generate my entities class from my db:
$config = new Configuration; $cache = new ArrayCache; $config->setMetadataCacheImpl($cache); $driverImpl = $config->newDefaultAnnotationDriver(array(__DIR__ . '/Entities/'), false); $config->setMetadataDriverImpl($driverImpl); $config->setQueryCacheImpl($cache); $config->setAutoGenerateProxyClasses(TRUE); //$namingStrategy = new \Doctrine\ORM\Mapping\UnderscoreNamingStrategy(CASE_UPPER); //$config->setNamingStrategy($namingStrategy); // Proxy configuration $config->setProxyDir(__DIR__ . '/proxies'); $config->setProxyNamespace('Proxies'); $dbParams = array( 'driver' => 'pdo_mysql', 'user' => \lib\config\DbConfig::USER_NAME, 'password' => \lib\config\DbConfig::USER_PASSWORD, 'dbname' => \lib\config\DbConfig::DB_NAME, 'port' => \lib\config\DbConfig::PORT, 'host' => \lib\config\DbConfig::HOST, ); //$config = Setup::createAnnotationMetadataConfiguration(self::getEntityPaths(), $isDevMode=true,__DIR__ . '/proxies',null,false); static::$instance = EntityManager::create($dbParams, $config); static::$instance->getConfiguration()-> addEntityNamespace('E', 'Entities'); $driver = new DatabaseDriver(self::$instance->getConnection()->getSchemaManager()); $driver->setNamespace('Entities\\'); self::$instance->getConfiguration()->setMetadataDriverImpl($driver); $cmf = new DisconnectedClassMetadataFactory(); $cmf->setEntityManager(self::$instance); $metadata = $cmf->getAllMetadata(); $generator = new EntityGenerator(); //$generator->setUpdateEntityIfExists(true); $generator->setRegenerateEntityIfExists(true); $generator->setGenerateStubMethods(true); $generator->setGenerateAnnotations(true); $generator->generate($metadata, __DIR__ . "/");And this is result:
namespace Entities; use Doctrine\ORM\Mapping as ORM; /** * SiteData * * @ORM\Table(name="site_data", indexes={@ORM\Index(name="IDX_site_data", columns={"category_name", "dataValue"})}) * @ORM\Entity */ class SiteData { /** * @var integer * * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @var string * * @ORM\Column(name="category_name", type="string", length=50, nullable=false) */ private $categoryname; /** * @var string * * @ORM\Column(name="dataKey", type="string", length=50, nullable=false) */ private $datakey; /** * @var string * * @ORM\Column(name="dataValue", type="text", length=65535, nullable=true) */ private $datavalue; /** * @var boolean * * @ORM\Column(name="isActive", type="boolean", nullable=true) */ private $isactive; [get set medthods...] }As you can see, my table named is site_data, it's has a column named is categoryName. But the entity generated has field named is private $categoryname; (all lowercase). I want that field named private $categoryName; instead private $categoryname;. I want doctrine DO NOT rename fields in entities class when generate, it should be keep name the same with db column name.
How can i do that?
I also implementing a custom NamingStrategy but it seem not working.
Please help me. Thanks so so much!
@Ocramius commented on GitHub (Mar 1, 2016):
I see
private $categoryName;in the code you pasted?Regardless of that, entity generation is a one-time process meant for importing existing schemas: you are supposed to adapt the generated code to your preferences post-generation.
@pokakhoaitay commented on GitHub (Mar 1, 2016):
@Ocramius, sorry it's my mistake, it's private $categoryname, I was modified my post. I just need a solution that i can fully control my fields are named. Please give me some advices!
@Ocramius commented on GitHub (Mar 1, 2016):
@pokakhoaitay the solution (for now) is manually editing, or patching the entity generator for the time you need it. The entity generator is not that flexible.
@Ocramius commented on GitHub (Mar 1, 2016):
Actually, the bit you may be interested about is the
DatabaseDriverYou can adapt that to your own needs when importing field definitions.
@pokakhoaitay commented on GitHub (Mar 1, 2016):
Thank you, i will try that. But i hope Doctrine team will do more work to implement a solution can handle named fields in entities generated more effective and more flexible for Doctrine version in future.
Thanks for you help @Ocramius!
@Ocramius commented on GitHub (Mar 1, 2016):
@pokakhoaitay the entity generator is a candidate for removal from the main repo (in future): we don't plan to improve it further, sorry.
@pokakhoaitay commented on GitHub (Mar 1, 2016):
Oh, I'm sorry to hear that. Thank your helpful information. Good day!