Generate entities and mapping fields name #5031

Closed
opened 2026-01-22 14:56:57 +01:00 by admin · 7 comments
Owner

Originally created by @pokakhoaitay on GitHub (Mar 1, 2016).

I have a table with this structure in db:

CREATE TABLE `bedstore-db`.site_data (
  id int(4) UNSIGNED NOT NULL AUTO_INCREMENT,
  categoryName varchar(50) NOT NULL,
  dataKey varchar(50) NOT NULL,
  dataValue text DEFAULT NULL,
  isActive tinyint(1) DEFAULT NULL,
  PRIMARY KEY (id),
  FULLTEXT INDEX IDX_site_data (categoryName, dataValue)
)

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!

Originally created by @pokakhoaitay on GitHub (Mar 1, 2016). I have a table with this structure in db: <pre> CREATE TABLE `bedstore-db`.site_data ( id int(4) UNSIGNED NOT NULL AUTO_INCREMENT, categoryName varchar(50) NOT NULL, dataKey varchar(50) NOT NULL, dataValue text DEFAULT NULL, isActive tinyint(1) DEFAULT NULL, PRIMARY KEY (id), FULLTEXT INDEX IDX_site_data (categoryName, dataValue) ) </pre> I use this code to generate my entities class from my db: <pre> $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__ . "/"); </pre> And this is result: <pre> 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...] } </pre> 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!
admin added the ImprovementWon't Fix labels 2026-01-22 14:56:57 +01:00
admin closed this issue 2026-01-22 14:56:57 +01:00
Author
Owner

@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.

@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.
Author
Owner

@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!

@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!
Author
Owner

@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): @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.
Author
Owner

@Ocramius commented on GitHub (Mar 1, 2016):

Actually, the bit you may be interested about is the DatabaseDriver

You can adapt that to your own needs when importing field definitions.

@Ocramius commented on GitHub (Mar 1, 2016): Actually, the bit you may be interested about is the [`DatabaseDriver`](https://github.com/doctrine/doctrine2/blob/4b45183dbddd30083b2552913f070c909348d333/lib/Doctrine/ORM/Mapping/Driver/DatabaseDriver.php#L541-L555) You can adapt that to your own needs when importing field definitions.
Author
Owner

@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!

@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!
Author
Owner

@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.

@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.
Author
Owner

@pokakhoaitay commented on GitHub (Mar 1, 2016):

Oh, I'm sorry to hear that. Thank your helpful information. Good day!

@pokakhoaitay commented on GitHub (Mar 1, 2016): Oh, I'm sorry to hear that. Thank your helpful information. Good day!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5031