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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable