Bài giảng Phát triển phần mềm nguồn mở (GV Nguyễn Hữu Thể) Bài 10

51 19 0
Bài giảng Phát triển phần mềm nguồn mở (GV Nguyễn Hữu Thể) Bài 10

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

PHÁT TRIỂN PHẦN MỀM NGUỒN MỞ DATABASE, MIGRATIONS & SEEDING Nguyễn Hữu Thể Database ❖ Introduction ❖ Configuration ❖ Read & Write Connections Giới thiệu − Laravel kết nối tới database thực thi query với nhiều database back-ends thơng qua sử dụng • raw SQL, • fluent query builder, • Eloquent ORM − Hiện tại, Laravel hỗ trợ sẵn database: • • • • MySQL Postgres SQLite SQL Server Cấu hình − Thư mục config/database.php • Trong file này: định nghĩa tất kết nối sở liệu, định connection mặc định ❖ Cấu hình SQL Server 'sqlsrv' => [ 'driver' => 'sqlsrv', 'host' => env('DB_HOST', 'localhost'), 'database' => env('DB_DATABASE', 'forge'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'charset' => 'utf8', 'prefix' => '', ], Đọc & ghi kết nối 'mysql' => [ 'read' => [ 'host' => '192.168.1.1', ], 'write' => [ 'host' => '196.168.1.2' ], 'driver' => 'mysql', 'database' => 'database', 'username' => 'root', 'password' => '', 'charset' => 'utf8', 'collation' => 'utf8_unicode_ci', 'prefix' => '', ], Thiết lập database file cấu hình chung env (Tên_Project/.env) APP_ENV=local APP_KEY=base64:SPqqJfE1ADzonR ot2o5g9J8Ix3iRVHsFOclr0KC1KHI= APP_DEBUG=true APP_LOG_LEVEL=debug APP_URL=http://localhost DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=ten_database DB_USERNAME=root DB_PASSWORD= BROADCAST_DRIVER=log CACHE_DRIVER=file SESSION_DRIVER=file QUEUE_DRIVER=sync REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 MAIL_DRIVER=smtp MAIL_HOST=mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null MAIL_PASSWORD=null MAIL_ENCRYPTION=null PUSHER_APP_ID= PUSHER_KEY= PUSHER_SECRET= Thực thi lệnh select namespace App\Http\Controllers; use Illuminate\Support\Facades\DB; use App\Http\Controllers\Controller; class UserController extends Controller { public function index() { $users = DB::select('select * from users where active = ?', [1]); return view('user.index', ['users' => $users]); } } Có thể thực thi câu query sử dụng liên kết đặt tên: $results = DB::select('select * from users where id = :id', ['id' => 1]); Thực thi lệnh select Syntax array select(string $query, array $bindings = array()) Parameters •$query(string) – query to execute in database •$bindings(array) – values to bind with queries Returns array Description Run a select statement against the database Thực thi câu lệnh insert − Hàm insert nhận câu raw SQL query tham số đầu tiên, bindings tham số thứ hai DB::insert('insert into users (id, name) values (?, ?)', [1, ‘Tom']); Syntax bool insert(string $query, array $bindings = array()) Parameters •$query(string) – query to execute in database •$bindings(array) – values to bind with queries Returns bool Description Run an insert statement against the database Thực thi câu lệnh update − Hàm update: update records có sở liệu Số lượng row ảnh hưởng câu lệnh trả qua hàm $affected = DB::update('update users set votes = 100 where name = ?', ['John']); Syntax int update(string $query, array $bindings = array()) Parameters •$query(string) – query to execute in database •$bindings(array) – values to bind with queries Returns int Description Run an update statement against the database 10 Cấu trúc migration use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateFlightsTable extends Migration { public function up() //đoạn lệnh thực migrate { Schema::create('flights', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('airline'); $table->timestamps(); }); } public function down() //đoạn lệnh thực Rollback { Schema::drop('flights'); } } 37 Thực thi Migrations − Để thực thi tất migration chương trình, sử dụng lệnh: php artisan migrate 38 Rolling Back Migrations - Hủy bỏ việc thực thi migrate trước php artisan migrate:rollback − Lệnh migrate:reset thực rollback lại tồn migration chương trình php artisan migrate:reset − Rollback & Migrate câu lệnh • Lệnh migrate:refresh rollback lại tồn migration chương trình, thực câu lệnh migrate Câu lệnh thực tái cấu trúc toàn database: php artisan migrate:refresh 39 Ví dụ demo: tạo tên database laravelk (trống, chưa có table) − Vào thư mục project: Nhấn Ctrl+Shift+ Chuột phải > Open Command Promt Here − Gõ vào CMD: C:\xampp\htdocs\laravelk>php artisan migrate Migration table created successfully Migrated: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_100000_create_password_resets_table − Xuất table: 40 Bảng lưu lại trình tạo bảng − batch = 1: tạo lần đầu 41 Tạo migration table_SanPham C:\xampp\htdocs\laravelk>php artisan make:migration table_SanPham Created Migration: 2016_12_19_130901_table_SanPham 42 Kiểm tra: database\migrations\2016_12_19_130901_table_SanPham.php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class TableSanPham extends Migration{ //Viết code cho migration public function up() { Schema::create('sanpham', function($table){ $table->increments('id'); $table->string('ten'); $table->integer('soluong'); }); } public function down() { Schema::drop('sanpham'); } } 43 Run Migrate C:\xampp\htdocs\laravelk>php artisan migrate Migrated: 2016_12_19_130901_table_SanPham Kiểm tra table Kiểm tra migrate 44 Hủy migrate, quay lại phiên trước php artisan migrate:rollback C:\xampp\htdocs\laravelk>php artisan migrate:rollback Rolled back: 2016_12_19_130901_table_SanPham Kiểm tra: xóa table_SanPham 45 Hủy bỏ hết cơng việc migrate php artisan migrate:reset C:\xampp\htdocs\laravelk>php artisan migrate:reset Rolled back: 2014_10_12_100000_create_password_resets_table Rolled back: 2014_10_12_000000_create_users_table Kiểm tra: tất table migration tạo 46 Seeding ❖ Introduction ❖ Writing Seeders ❖ Running Seeders 47 Seeding − Laravel có phương thức đơn giản để seed database với liệu test sử dụng seed class − Tất seed class lưu thư mục database/seeds − Viết Seeders • Để sinh seeder, gọi lệnh make:seeder Artisan Tất seeder sinh framework đặt thư mục database/seeds: php artisan make:seeder UsersTableSeeder 48 Seeding use Illuminate\Database\Seeder; use Illuminate\Database\Eloquent\Model; class DatabaseSeeder extends Seeder { public function run() //Viết code tạo table users { DB::table('users')->insert([ 'name' => str_random(10), 'email' => str_random(10).'@gmail.com', 'password' => bcrypt('secret'), ]); } } 49 Thực thi Seeders − Khi có seeder class • Có thể sử dụng câu lệnh db:seed Artisan để seed vào database • Mặc định, câu lệnh db:seed thực thi class DatabaseSeeder, mà bạn sử dụng để gọi seed class khác • Tuy nhiên, bạn sử dụng tuỳ chọn class để định thực seed class php artisan db:seed php artisan db:seed class=UsersTableSeeder 50 Ví dụ: tạo table users use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { public function run() { DB::table ( 'users' )->insert ( [ 'name' => 'Tom', 'email' => 'tom@gmail.com', 'password' => bcrypt('123456') ] ); } } Mở cửa sổ cmd : php artisan db:seed Kiểm tra liệu: 51 ... C:xampphtdocslaravelk>php artisan migrate Migration table created successfully Migrated: 2014 _10_ 12_000000_create_users_table Migrated: 2014 _10_ 12 _100 000_create_password_resets_table − Xuất table: 40 Bảng lưu lại trình... C:xampphtdocslaravelk>php artisan migrate:reset Rolled back: 2014 _10_ 12 _100 000_create_password_resets_table Rolled back: 2014 _10_ 12_000000_create_users_table Kiểm tra: tất table migration tạo... run() //Viết code tạo table users { DB::table('users')->insert([ 'name' => str_random (10) , 'email' => str_random (10) .'@gmail.com', 'password' => bcrypt('secret'), ]); } } 49 Thực thi Seeders − Khi

Ngày đăng: 30/10/2021, 11:09

Hình ảnh liên quan

Cấu hình - Bài giảng Phát triển phần mềm nguồn mở (GV Nguyễn Hữu Thể) Bài 10

u.

hình Xem tại trang 4 của tài liệu.
Thiết lập database trong file cấu hình chung .env (Tên_Project/.env) 6APP_ENV=localAPP_KEY=base64:SPqqJfE1ADzonRot2o5g9J8Ix3iRVHsFOclr0KC1KHI=APP_DEBUG=trueAPP_LOG_LEVEL=debugAPP_URL=http://localhostDB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306 - Bài giảng Phát triển phần mềm nguồn mở (GV Nguyễn Hữu Thể) Bài 10

hi.

ết lập database trong file cấu hình chung .env (Tên_Project/.env) 6APP_ENV=localAPP_KEY=base64:SPqqJfE1ADzonRot2o5g9J8Ix3iRVHsFOclr0KC1KHI=APP_DEBUG=trueAPP_LOG_LEVEL=debugAPP_URL=http://localhostDB_CONNECTION=mysqlDB_HOST=127.0.0.1DB_PORT=3306 Xem tại trang 6 của tài liệu.
echo "Đã tạo bảng loaisanpham" ; }); - Bài giảng Phát triển phần mềm nguồn mở (GV Nguyễn Hữu Thể) Bài 10

echo.

"Đã tạo bảng loaisanpham" ; }); Xem tại trang 29 của tài liệu.
echo "Đã tạo bảng" ; - Bài giảng Phát triển phần mềm nguồn mở (GV Nguyễn Hữu Thể) Bài 10

echo.

"Đã tạo bảng" ; Xem tại trang 30 của tài liệu.
echo "Đã tạo bảng liên kết" ; } ); - Bài giảng Phát triển phần mềm nguồn mở (GV Nguyễn Hữu Thể) Bài 10

echo.

"Đã tạo bảng liên kết" ; } ); Xem tại trang 31 của tài liệu.
$table-> dropColumn('TenCot'); Xóa cột trong bảng - Bài giảng Phát triển phần mềm nguồn mở (GV Nguyễn Hữu Thể) Bài 10

table.

> dropColumn('TenCot'); Xóa cột trong bảng Xem tại trang 32 của tài liệu.
Bảng lưu lại quá trình tạo bảng - Bài giảng Phát triển phần mềm nguồn mở (GV Nguyễn Hữu Thể) Bài 10

Bảng l.

ưu lại quá trình tạo bảng Xem tại trang 41 của tài liệu.

Tài liệu cùng người dùng

Tài liệu liên quan