0

I do not use Symfony framework. I want to generate Entity from MySQL table using console. Here is the cli-config.php file in the app folder-

<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;

// replace with file to your own project bootstrap
require_once 'bootstrap.php';

// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();

return ConsoleRunner::createHelperSet($entityManager);

And the bootstrap.php file is as below-

<?php
// bootstrap.php
require_once "vendor/autoload.php";

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

$paths = array("test");
$isDevMode = false;

// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => '',
    'dbname'   => 'test',
);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);

Suppose I have a table named categories like-

CREATE TABLE categories(
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL DEFAULT '',
  is_active TINYINT(1) NOT NULL DEFAULT 1,
  created DATETIME,
  modified TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

I want a YAML Metadata file for the Entity too.

2 Answers 2

3

please use google first. If you look for "doctrine2 generate entities from database" this is the first match.

http://symfony.com/doc/current/cookbook/doctrine/reverse_engineering.html

Think you can adapt it for your Problem also if you dont use Symfony.

this will create your mappings as xml. if you replace xml to yaml you get them. php bin/console doctrine:mapping:import --force AcmeBlogBundle xml

this will generate annotations for you after in second step.

php bin/console doctrine:mapping:convert annotation ./src

and at least, this will generate the entities very well.

php bin/console doctrine:generate:entities AcmeBlogBundle

i tried it too for my Symfony Project and it worked very well.

Greetings

Sign up to request clarification or add additional context in comments.

6 Comments

I think your answer is OK. Can you tell me how can I configure DB Parameters as well as Command options when I am not using Symfony? For example we need to replace this configuration file- app/config/parameters.yml somehow. Thanks for answering.
i think here you get the answere you looking for ;).link
Not necessarily. I want to use cli-config.php that is using bootstrap.php in turn and no reference to symphony framework.
the link i posted to you, is not a symfony link. it´s the default installation of doctrine 2. doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/….
I am on your reference page given most recently but I can not make it working anyhow.
|
0

For me the best way is: php bin/console doctrine:mapping:import "App\Entity" annotation --filter="NameOfTableInCamelCase" --path=src/Entity

It creates NameOfTableInCamelCase.php in src/Entity with all the data you need

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.