Reputation: 437
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.
Upvotes: 0
Views: 1213
Reputation: 3
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
Upvotes: 0
Reputation: 604
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
Upvotes: 3