前端兼职笔记:PHP

127 阅读1分钟

介绍

Hyperf框架是一个高性能、高灵活性的渐进式PHP协程框架。拥有便捷的开发体验,提供了路由功能中间件事件机制数据库ORM等组件,满足各种开发需求‌,上手也比较easy。

数据库操作

  • 增加表
php bin/hyperf.php gen:migration create_table_name --table=table_name
  • 增加字段
php bin/hyperf.php gen:migration create_your_table_column --table=your_table_name
  1. 编辑迁移文件
use Hyperf\DbSchema\Blueprint;
use Hyperf\Migration\Migration;

class YourMigrationName extends Migration
{
    /**
     * Run the migrations.
     */
    public function up()
    {
        Schema::table('your_table_name', function (Blueprint $table) {
            $table->string('remember_token')->nullable()->default(null);
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down()
    {
        Schema::table('your_table_name', function (Blueprint $table) {
            $table->dropColumn('remember_token');
        });
    }
}
  1. 运行迁移文件
php bin/hyperf.php migrate
  1. 编辑数据库操作modal,/modal目录下的文件是定义某个表的操作,按需修改即可
  2. 向数据库插入一条数据,updateOrCreate的作用是如果不存在则新建,其中username是我在迁移文件中定义的主键
YourModel::updateOrCreate(
    ['username' => $username],
    ['code' => $code]
);
  1. 查询数据
$code = YourModel::where('username', $username)->value('code');

重启服务

1.1 停止服务端:ps -ef | grep -v grep | grep Exchange | awk '{print $2}'|xargs kill -9

1.2 测试启动:php bin/hyperf.php start (仅异常查看,执行后ctrl+c结束后才能继续)

1.3 正式启动:nohup php bin/hyperf.php start >/dev/null 2>&1 &