final class User extends AbstractMigration { /** * Change Method. * * Write your reversible migrations using this method. * * More information on writing migrations is available here: * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method * * Remember to call "create()" or "update()" and NOT "save()" when working * with the Table class. */ public function change(): void { $table = $this->table('users', ['signed' => false]); $table->addColumn('name', 'string', ['limit' => 16]) ->addColumn('email', 'string', ['limit' => 64]) ->addColumn('password', 'string', ['limit' => 64]) ->addColumn('created', 'timestamp', ['default' => 'CURRENT_TIMESTAMP']) ->addIndex(['email', 'password']) ->create(); } }
final class Article extends AbstractMigration { /** * Change Method. * * Write your reversible migrations using this method. * * More information on writing migrations is available here: * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method * * Remember to call "create()" or "update()" and NOT "save()" when working * with the Table class. */ public function change(): void { $table = $this->table('articles', ['signed' => false]); $table->addColumn('title', 'string', ['limit' => 32]) ->addColumn('classify', 'string', ['limit' => 32]) ->addColumn('cover', 'string', ['limit' => 255]) ->addColumn('user_id', 'integer', ['signed' => false]) ->addColumn('view_count', 'integer', ['signed' => false]) ->addColumn('created', 'timestamp', ['default' => 'CURRENT_TIMESTAMP']) ->addIndex(['user_id']) ->create(); } }
final class ArticleContent extends AbstractMigration { /** * Change Method. * * Write your reversible migrations using this method. * * More information on writing migrations is available here: * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method * * Remember to call "create()" or "update()" and NOT "save()" when working * with the Table class. */ public function change(): void { $table = $this->table('article_contents', ['signed' => false]); $table->addColumn('article_id', 'integer', ['signed' => false]) ->addColumn('content', 'text') ->addIndex(['article_id']) ->create(); } }
final class EmailCode extends AbstractMigration { /** * Change Method. * * Write your reversible migrations using this method. * * More information on writing migrations is available here: * https://book.cakephp.org/phinx/0/en/migrations.html#the-change-method * * Remember to call "create()" or "update()" and NOT "save()" when working * with the Table class. */ public function change(): void { $table = $this->table('email_codes', ['signed' => false]); $table->addColumn('email', 'string', ['limit' => 64]) ->addColumn('code', 'string', ['limit' => 8]) ->addColumn('created', 'timestamp', ['default' => 'CURRENT_TIMESTAMP']) ->addIndex(['email']) ->create(); } }
生成表
执行命令:vendor/bin/phinx migrate
然后打开数据库,可以看到:
生成了这些表之后,就可以直接用框架集成的 ORM 调用了。
Model
ORM 默认使用的表名即复数形式,也可以指定一个表名,只需要修改 $table 变量即可。
创建一个用来保存模型文件的目录 app/Http/Model。
User
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
<?php /** * Created by PhpStorm * Author:FireRabbit * Date:2021/2/19 * Time:14:00 **/
namespace App\Http\Model;
use Illuminate\Database\Eloquent\Model;
class User extends Model { protected $guarded = []; public $timestamps = false; }