migro/php-core-migration
Composer 安装命令:
composer require migro/php-core-migration
包简介
A PHP library providing a code-first approach for database migrations. Designed for simplicity and ease of use, it allows developers to define and manage database schema changes directly in code.
README 文档
README
Description
This repository contains examples and best practices for using a code-first approach in Core PHP. It is intended for developers who are looking to understand and implement this methodology in their PHP projects.
Table of Contents
Installation
To install and run this project, you need to have PHP and MySQL installed on your machine.
- Clone this repository.
- Navigate to the project directory.
- Update the
config.phpfile with your database credentials.
The config.php file should look like this:
<?php $config = [ "host" => "localhost", "username" => "root", "password" => "", "database" => "php_code_first_approach_db", ];
Usage
To connect to the database, you need to call the connectToDatabase function from the connection.php file. Here's how the connection.php file looks:
<?php function connectToDatabase($config) { $conn = mysqli_connect($config['host'], $config['username'], $config['password'], $config['database'])or die("lost"); if (!$conn) { die('Connection failed: ' . mysqli_connect_error()); } return $conn; }
To create a table in the database, you can use the createTable function from the BaseMigration class in the migration folder. Here's how the BaseMigration class looks:
<?php class BaseMigration { public static function createTable($conn, $sql) { if ($conn->query($sql) === TRUE) { echo "Table created successfully"; } else { echo "Error creating table: " . $conn->error; } } }
The BaseMigration class is extended by other classes to create specific tables. For example, the RoleMigration class creates a tbl_userRoles table if it doesn't exist. Here's how the RoleMigration class looks:
<?php class RoleMigration extends BaseMigration { public static function up($conn) { $sql = "SHOW TABLES LIKE 'tbl_userRoles'"; $result = $conn->query($sql); if ($result->num_rows == 0) { $sql = "CREATE TABLE tbl_userRoles ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(30) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )"; self::createTable($conn, $sql); echo "tbl_userRoles has created"; }else{ echo "tbl_userRoles already exist"; } } }
After setting up the database connection and defining your migration classes, you need to execute the migrations to create the tables in your database. This is done in the Migrate.php file. Here's how the Migrate.php file looks:
<?php require_once '../config/config.php'; require_once '../config/connection.php'; require_once 'BaseMigration.php'; require_once 'RoleMigration.php'; require_once 'UserMigration.php'; $conn = connectToDatabase($config); $roleMigration = RoleMigration::up($conn); $userMigration = UserMigration::up($conn); ?>
Alternatively, if you have a local server environment set up (like XAMPP, WAMP, or MAMP), you can also navigate to the Migrate.php file through your web browser by typing the local server URL followed by the path to the Migrate.php file. For example:
http://localhost/your_project_directory/database/migrations/Migrate.php
if your table will not be exist that will be created
for further guidance just visit my article https://getsmartsolution.com/code-first-approach-into-core-php/
Contributing
Currently, this project only supports creating tables. I am working on developing a full-fledged migration system for Core PHP. If you have any ideas or improvements, feel free to make a pull request.
migro/php-core-migration 适用场景与选型建议
migro/php-core-migration 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 03 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 migro/php-core-migration 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 migro/php-core-migration 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 24
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-03-01