如何使用doctrine

时间:2015-12-01 08:39:23

标签: doctrine

无法使用doctrine动态创建表,我得到了插入,更新的查询,但我无法在数据库中创建表,我试图找到但是它显示使用命令行创建的表,我想使用php脚本创建并动态

这是我的代码

<?php
//bootstrap.php
// I am able to fetch ,insert ,delete records from existing tables and      wanted to 
//know if there is any way i can create table using php script 
// i am aware that we can create table using CMD but willing to create using     only php scripts i.e Dynamically


use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;

require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/entities/Users.php';

$paths            = array(__DIR__ . '/entities');
$isDevMode        = false;
$connectionParams = array(
'driver'   => 'pdo_mysql',
'user'     => 'root',
'host'     =>  'localhost',
'password' => '',
'dbname'   => 'doctrine',
);

$config = Setup::createConfiguration($isDevMode);
$driver = new AnnotationDriver(new AnnotationReader(), $paths);

// registering noop annotation autoloader - allow all annotations by default
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);

$em = EntityManager::create($connectionParams, $config);

$query = $em->createQuery('SELECT u.email, u.password FROM users u');
$users = $query->getResult(); // array of CmsUser username and name values
echo $users[0]['email']; 

1 个答案:

答案 0 :(得分:2)

以下两个答案帮助我解决了一个类似的问题:

代码:

$metadata = $entityManager->getClassMetadata($entityClass)

$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($entityManager);
// you can drop the table like this if necessary
$schemaTool->dropSchema(array($metadata));
$schemaTool->createSchema(array($metadata));
相关问题