dqdtgiw4736 2017-07-11 10:29
浏览 59
已采纳

Zend 3 + Doctrine 2创建表格?

I have been wondering around articles on google trying to "find a way" to create tables using doctrine. I need to create "temporary tables" because the project I am working on will have a lot of "temp statistics" and I do not need to have all the information, just a "total" at some point. Anyway MYSQL "temporary" tables would just be the "solution".

However, I cannot find a single piece of information where it shows you how to create tables with Doctrine.

I understand that Doctrine needs the tables to be created and the entities, but there must be a way to create tables "on-the-fly", or a way to run raw queries.

Here's a piece of code I have tried, and of course it failed:

$schema= new \Doctrine\DBAL\Schema\Schema();

        //if (!$schema->tablesExist('post'))
        //{
            $table= new \Doctrine\DBAL\Schema\Table('post');
            $table->addColumn('id', 'integer', ['autoincrement'=>true]);
            $table->addColumn('title', 'text', ['notnull'=>true]);
            $table->addColumn('content', 'text', ['notnull'=>true]);
            $table->addColumn('status', 'integer', ['notnull'=>true]);
            $table->addColumn('date_created', 'datetime', ['notnull'=>true]);
            $table->setPrimaryKey(['id']);
            $table->addOption('engine' , 'InnoDB');

            $schema->createTable($table); // save to DB
        //}

Can someone please clarify to me if tables can or cannot be created with Doctrine ?

Many Thanks.

  • 写回答

1条回答 默认 最新

  • douyi4991 2017-07-11 10:32
    关注

    Code you have written is called a migration, and if the migrations are installed and configured properly, can be executed using the command:

    php public/index.php migrations:migrate
    

    If you wish to create a table without a migrations, simply create an entity first, configure the mappings (using the annotations or any other type of your choice) and force create the schema using:

    php public/index.php doctrine:schema:update --force
    

    If you wish to run raw queries you can:

    $this->entityManager->getConnection()->executeQuery('SQL Query', $optionalParams);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?