2016-01-01 41 views
0

我不使用Symfony框架。我想使用控制檯從MySQL表生成實體。這是在應用程序文件夾 -來自MySQL的Doctrine2實體生成表

<?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); 

而且的bootstrap.php文件中的CLI-config.php文件作爲如下─

<?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); 

假設我有一個表命名類別喜歡 -

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 
); 

我想要一個實體的YAML元數據文件。

回答

1

請先使用谷歌。如果你尋找「doctrine2從數據庫生成實體」這是第一場比賽。

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

你覺得你能適應它爲你的問題,如果你還不使用Symfony的。

這將創建您的映射爲XML。如果你將xml替換爲yaml,你會得到它們。 php bin/console doctrine:mapping:import --force AcmeBlogBu​​ndle xml

這將在第二步中爲您生成註釋。

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

並且至少會產生很好的實體。

php bin/console doctrine:generate:entities AcmeBlogBundle 

我也爲我的Symfony項目嘗試過,它工作得很好。

問候

+0

我認爲你的回答是確定的。你能告訴我如何配置數據庫參數以及命令選項,當我不使用Symfony?例如,我們需要以某種方式替換這個配置文件 - app/config/parameters.yml。謝謝回答。 – observo

+0

我想在這裏你得到你想要的答案;)[鏈接](http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/configuration.html) – ZFNerd

+0

不一定。我想要使​​用cli-config.php,它依次使用bootstrap.php,並且沒有引用symphony框架。 – observo