0

MySQLに直接アクセスして、テーブルを作成することはできます。(以下テーブル作成クエリ)

CREATE TABLE `comment_table` (
  `comment_table_id` int(11) unsigned NOT NULL AUTO_INCREMENT ,
  `post_id` int(11) NOT NULL ,
  `user_id` int(11) NOT NULL ,
  `id` int(11) NOT NULL ,
  `text` text NOT NULL ,
  `img` text ,
  `create_at` datetime NOT NULL,
  PRIMARY KEY (`colony_comment_table_id`)
) ENGINE=InnoDB AUTO_INCREMENT=36 DEFAULT CHARSET=utf8;

しかし、CakePHP3のコード上で上と同じようなコードを再現することができません。
公式ドキュメントを読んで書いてみたのですが、やはり動作しないようです。

class MapsController extends AppController {
  public function createColony(){
    $createTable = $this->table('comment');
        $createTable
            ->addPrimaryKey('comment_table_id','int',[
                                                     'limit'=>11,
                                                     'unsigned'=>true,
                                                     'NOT NULL'=>true,
                                                     'AUTO_INCREMENT'=>true
                                                      ])
            ->addColumn('post_id','int',[
                                         'limit'=>11,
                                         'NOT NULL'=>true,
                                         ])
            ->addColumn('user_id','int',[
                                         'limit'=>11,
                                         'NOT NULL'=>true
                                         ])
            ->addColumn('id','int',[
                                    'limit'=>11,
                                    'NOT NULL'=>true
                                   ])
            ->addColumn('text','text',[
                                       'NOT NULL'=>true
                                       ])
            ->addColumn('img','text')
            ->addColumn('create_at','datetime',[
                                                'NOT NULL'
                                                'limit'=>11,
                                                'NOT NULL'=>true,
                                               ])
            ->addColumn('user_id','int',[
                                         'limit'=>11,
                                         'NOT NULL'=>true
                                         ])
            ->addColumn('colony_id','int',[
                                           'limit'=>11,
                                           'NOT NULL'=>true
                                           ])
            ->addColumn('text','text',[
                                       'NOT NULL'=>true
                                       ])
            ->addColumn('img','text')
            ->addColumn('create_at','datetime',[
                                                'NOT NULL'
                                                ])
            ->create();
   }
}

なにかありましたら、随時補足いたします。
よろしくお願いします。

3
  • CakePHP 3でのテーブル作成には、Phinxのラッパーであるmigrationsプラグインを使用します。 Migrations — CakePHP Cookbook 3.x ドキュメント book.cakephp.org/3.0/ja/migrations.html マイグレーションファイルは AbstractMigration を継承する必要があります。 コードを見るとなぜかAppControllerを継承していますが、コントローラーでテーブルを動的に生成したいとかいう意図だったりするのでしょうか。
    – nojimage
    2015年11月24日 3:04
  • コメントありがとうございます。 そうですね。Controllerでテーブルを動的に生成しようと思っています。
    – chicKer
    2015年11月25日 5:16
  • teratail.com/questions/20783 マルチポスト
    – htb
    2016年7月11日 14:50

0

この質問に回答するには、ログインする必要があります。

のタグが付いた他の質問を参照する。