FuelPHP 1.6.1 で SQLite を使い Scafold する

SQLite でも Scafold したコードが動きましたので、手順を書いておきます。

あまりきちんと検証していないので、SQLite の設定など改善すべき点がありましたら、お知らせ頂けるとありがたいです。


config.php で orm を always_load するようにします。

データベース設定を SQLite にします。

--- a/fuel/app/config/development/db.php
+++ b/fuel/app/config/development/db.php
@@ -5,10 +5,15 @@
 
 return array(
    'default' => array(
-       'connection'  => array(
-           'dsn'        => 'mysql:host=localhost;dbname=fuel_dev',
-           'username'   => 'root',
-           'password'   => 'root',
+     'connection' => array(
+         'dsn' => 'sqlite:' . APPPATH . 'tmp/sample.sqlite3',
+         'persistent' => false,
+         'compress' => false,
        ),
+     'identifier' => '"',
+     'table_prefix' => '',
+     'enable_cache' => true,
+     'profiling' => true,
+     'charset' => '',
    ),
 );

oil コマンドで scaffold します。

$ php oil generate scaffold bbs post_date:date message:varchar[100] --singular

migration ファイルがそのままでは動かないので修正します。

--- a/fuel/app/migrations/001_create_bbs.php
+++ b/fuel/app/migrations/001_create_bbs.php
@@ -7,13 +7,12 @@ class Create_bbs
    public function up()
    {
        \DBUtil::create_table('bbs', array(
-           'id' => array('constraint' => 11, 'type' => 'int', 'auto_increment' => true, 'unsigned' => true),
-           'post_date' => array('type' => 'date'),
-           'message' => array('constraint' => 100, 'type' => 'varchar'),
-           'created_at' => array('constraint' => 11, 'type' => 'int', 'null' => true),
-           'updated_at' => array('constraint' => 11, 'type' => 'int', 'null' => true),
-
-       ), array('id'));
+         'id' => array('type' => 'integer primary key', 'autoincrement' => true),
+         'post_date' => array('type' => 'text'),
+         'message' => array('type' => 'text'),
+         'created_at' => array('type' => 'integer', 'null' => true),
+         'updated_at' => array('type' => 'integer', 'null' => true),
+     ));
    }
 
    public function down()

oil コマンドで migrate します。

$ php oil refine migrate

views/bbs/index.php がバグっているので修正します。これは、--singular オプションを付けたときに発生するバグです。

--- a/fuel/app/views/bbs/index.php
+++ b/fuel/app/views/bbs/index.php
@@ -10,14 +10,14 @@
        </tr>
    </thead>
    <tbody>
-<?php foreach ($bbs as $bbs): ?>       <tr>
+<?php foreach ($bbs as $bb): ?>      <tr>
 
-           <td><?php echo $bbs->post_date; ?></td>
-           <td><?php echo $bbs->message; ?></td>
+         <td><?php echo $bb->post_date; ?></td>
+         <td><?php echo $bb->message; ?></td>
            <td>
-               <?php echo Html::anchor('bbs/view/'.$bbs->id, '<i class="icon-eye-open" title="View"></i>'); ?> |
-               <?php echo Html::anchor('bbs/edit/'.$bbs->id, '<i class="icon-wrench" title="Edit"></i>'); ?> |
-               <?php echo Html::anchor('bbs/delete/'.$bbs->id, '<i class="icon-trash" title="Delete"></i>', array('onclick' => "return confirm('Are you sure?')")); ?>
+             <?php echo Html::anchor('bbs/view/'.$bb->id, '<i class="icon-eye-open" title="View"></i>'); ?> |
+             <?php echo Html::anchor('bbs/edit/'.$bb->id, '<i class="icon-wrench" title="Edit"></i>'); ?> |
+             <?php echo Html::anchor('bbs/delete/'.$bb->id, '<i class="icon-trash" title="Delete"></i>', array('onclick' => "return confirm('Are you sure?')")); ?>
 
            </td>
        </tr>