MySQL表中的Doctrine2实体生成

时间:2016-01-01 11:16:48

标签: doctrine-orm

我不使用Symfony框架。我想使用控制台从MySQL表生成实体。这是app文件夹中的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);

bootstrap.php文件如下 -

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

假设我有一个名为 -

的类别的表
ERROR [STREAM-IN-/172.31.20.223] 2016-01-01 15:37:30,941 StreamSession.java:524 - [Stream #b1cc3600-b054-11e5-80c4-21d1c7c11a01] Streaming error occurred:
java.nio.channels.ClosedChannelException: null
        at sun.nio.ch.SocketChannelImpl.ensureReadOpen(SocketChannelImpl.java:257) ~[na:1.8.0_66]
        at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:300) ~[na:1.8.0_66]
        at org.apache.cassandra.streaming.messages.StreamMessage.deserialize(StreamMessage.java:53) ~[apache-cassandra-2.2.4.jar:2.2.4]

我也想为实体提供YAML元数据文件。

2 个答案:

答案 0 :(得分:2)

请先使用谷歌。如果你寻找&#34; doctrine2从数据库生成实体&#34;这是第一场比赛。

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项目尝试了它并且效果很好。

问候

答案 1 :(得分:0)

对我来说,最好的方法是:php bin/console doctrine:mapping:import "App\Entity" annotation --filter="NameOfTableInCamelCase" --path=src/Entity

它使用所需的所有数据在src / Entity中创建NameOfTableInCamelCase.php