3

Unable to create tables dynamically using doctrine,I got queries for insert,update but i am not able to create table in database,I tried to find but it is showing table created using command line,I want to create using php script and dynamically

HERE IS MY CODE

<?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
  • And where is your code so we can help you? Commented Dec 1, 2015 at 8:43

1 Answer 1

4

The following two answers helped me solve a comparable problem:

Code:

$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));
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.