学说可以基于小数据输入,包括相关信息生成实体。 但是,我没能找到关于该主题的任何实例或简明的信息。 官方文档只有一个小段落,与未成年人的命令行例子,解释绝对没问题,
任何人都可以指书籍,文章或这甚至代码示例?
学说可以基于小数据输入,包括相关信息生成实体。 但是,我没能找到关于该主题的任何实例或简明的信息。 官方文档只有一个小段落,与未成年人的命令行例子,解释绝对没问题,
任何人都可以指书籍,文章或这甚至代码示例?
首先,你需要引导Doctrine
,并得到一个实例EntityManager
,那么,一旦你拥有它,你可以做到以下几点:
$cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em); // $em is EntityManager instance
$metadata = $cmf->getAllMetadata();
$generator = new \Doctrine\ORM\Tools\EntityGenerator();
$generator->setGenerateAnnotations(true);
$generator->setGenerateStubMethods(true);
$generator->setRegenerateEntityIfExists(true);
$generator->setUpdateEntityIfExists(false);
$generator->generate($metadata, '/path/to/entities');
有关配置的详细信息,请阅读本:
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/configuration.html
我通常申报阳明的元数据,并开始从那里产生类和数据库。
下面是与阳明元数据文件以及一个完整的例子:
//example.php
function getDbConfig()
{
//An example configuration
return array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => 'potatoes',
'dbname' => 'garden',
'host' => 'localhost',
'charset' => 'utf8',
'driverOptions' => array(
1002=>'SET NAMES utf8'
)
);
}
function bootstrapDoctrine()
{
require_once ($this->_libDir . DS . 'Doctrine/ORM/Tools/Setup.php');
Doctrine\ORM\Tools\Setup::registerAutoloadDirectory('/full/path/to/lib');//So that Doctrine is in /full/path/to/lib/Doctrine
}
function getEntityFolders()
{
//An example configuration of two entity folders
return array(
'/full/path/to/App/Module1/Entities/yml' => '\\App\\Module1\\Entities',
'/full/path/to/App/Module2/Entities/yml' => '\\App\\Module2\\Entities'
);
}
function setupDoctrine()
{
$config = Doctrine\ORM\Tools\Setup::createConfiguration();
$driver = new \Doctrine\ORM\Mapping\Driver\SimplifiedYamlDriver(getEntityFolders());
$driver->setGlobalBasename('schema');
$config->setMetadataDriverImpl($driver);
$entityManager = \Doctrine\ORM\EntityManager::create($dbConfig, $config);
return $entityManager;
}
function getEntitiesMetaData($em)
{
$cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory();
$cmf->setEntityManager($em); // we must set the EntityManager
$driver = $em->getConfiguration()->getMetadataDriverImpl();
$classes = $driver->getAllClassNames();
$metadata = array();
foreach ($classes as $class) {
//any unsupported table/schema could be handled here to exclude some classes
if (true) {
$metadata[] = $cmf->getMetadataFor($class);
}
}
return $metadata;
}
function generateEntities($rootDir, $metadata)
{
$generator = new Doctrine\ORM\Tools\EntityGenerator();
$generator->setUpdateEntityIfExists(true); // only update if class already exists
//$generator->setRegenerateEntityIfExists(true); // this will overwrite the existing classes
$generator->setGenerateStubMethods(true);
$generator->setGenerateAnnotations(true);
$generator->generate($metadata, $rootDir);
}
function generateDatabase()
{
$schema = new Doctrine\ORM\Tools\SchemaTool($em);
$schema->createSchema($metadata);
}
//Sets up the Doctrine classes autoloader
bootstrapDoctrine();
//Sets up database connection, schema files (yml) and returns the EntityManager
$em = setupDoctrine();
//Returns the metadata specified by the two schema.orm.yml files
$metadata = getEntitiesMetaData($em);
/* Generates the classes based on the yml schema. Using the yml files in the example
* the result will be the following files:
* /full/path/to/App/Module1/Entities/User.php
* /full/path/to/App/Module2/Entities/Comment.php
* /full/path/to/App/Module2/Entities/Page.php
*/
generateEntities('/full/path/to', $metadata);
//Now generate database tables:
generateDatabase($metadata);
和示例架构文件:用于模块1 YAML架构:
# /full/path/to/App/Module1/Entities/yml/schema.orm.yml
App\Module1\Entities\User:
type: entity
table: user
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
email:
YAML架构为模块2:
# /full/path/to/App/Module2/Entities/yml/schema.orm.yml
App\Module2\Entities\Page:
type: entity
table: page
id:
id:
type: integer
generator:
strategy: AUTO
fields:
name:
content:
type: text
App\Module2\Entities\Comment:
type: entity
table: comment
id:
id:
type: integer
generator:
strategy: AUTO
fields:
comment_date:
type: datetime
content:
type: text
manyToOne:
user:
targetEntity: App\Module1\Entities\User
请注意,我用的是SimplifiedYamlDriver元数据驱动程序,可以从一个单一的YML文件加载多个实体。
这是一个工作的例子,我使用这些具体步骤从阳明生成的类文件和数据库。 此外, 入门教程包含好几个例子阳明海运。