我可以合并运行插入的 .sql 文件和仅在 2 列上创建虚拟数据的 Laravel 工厂播种机吗?
Can I merge an .sql file that runs insert into and a Laravel factory seeder that creates dummy data on 2 only columns?
我有一个为 2 列创建虚拟数据的工厂文件:代码和条形码。但是,我想 运行 一个 .sql 文件来替换我 运行 工厂命令后的数据。这样一来,我想合并将会发生并且可能会删除一些记录或添加新记录。是否可以改为合并数据。喜欢自动映射?
这是我的 .sql 文件的内容:
-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Apr 23, 2021 at 12:14 PM
-- Server version: 10.1.28-MariaDB
-- PHP Version: 7.1.11
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `db_zachmatic`
--
--
-- Dumping data for table `products`
--
REPLACE INTO `products` (`id`, `code`, `name`, `description`, `unit`, `cost`, `srp`, `supplier`, `qty_on_hand`, `category`, `delivery_date`, `created_at`) VALUES
(12, 'P-824830', 'magnum-v', '45/90-17 MV-360 tube type tires only', 'Pieces', '', '1246', 'HH All Ventures', 3, 'Tires', '', ''),
(13, 'P-232033', 'magnum-v', '50/100-17 MV-360 tube type tire only', 'Select Product ', '', '1246', 'HH All Ventures', 1, 'Tires', '', ''),
(14, 'P-73032309', 'magnum-v', '60/90-17 MV-360 Tube Type Tire Only', 'Pieces', '', '1341', 'HH All Ventures', 0, 'Tires', '', ''),
(15, 'P-0022252', 'magnum-v', '45/90-17 MV-329 Tube Type with Tube', 'Pieces', '', '1120', 'HH All Ventures', 9, 'Tires', '', ''),
(16, 'P-323694', 'magnum-v', '50/100-17 MV-329 tube type with tube', 'Select Product ', '', '1120', 'HH All Ventures', 0, 'Tires', '', ''),
(17, 'P-023202', 'magnum-v', '60/90-17 MV-329 tube type with tube', 'Pieces', '', '1499', 'HH All Ventures', 0, 'Tires', '', ''),
(18, 'P-4729320', 'magnum-v', '70/90-17 MV-329 tube type with tube', 'Pieces', '', '1678', 'HH All Ventures', 0, 'Tires', '', ''),
(19, 'P-3020323', 'magnum-v', '120/70-13 MV-119C Tubeless', 'Pieces', '', '1829', 'HH All Ventures', 5, 'Tires', '', ''),
(20, 'P-3220830', 'magnum-v', '130/70-13 MV-119C tubeless', 'Pieces', '', '2062', 'HH All Ventures', 0, 'Tires', '', ''),
--
-- AUTO_INCREMENT for table `products`
--
ALTER TABLE `products` DROP COLUMN `id`;
ALTER TABLE `products` ADD COLUMN `id` INT AUTO_INCREMENT UNIQUE FIRST;
--
-- Drop `code` column to delete old barcodes
-- then re-add `code` column, then add `barcode` column
--
ALTER TABLE `products` DROP COLUMN `code`;
ALTER TABLE `products` ADD COLUMN code VARCHAR(255) AFTER id;
--
-- Drop `delivery_date` column to delete old column
-- then re-add `delivery_date` column, then add `barcode` column
--
ALTER TABLE `products` DROP COLUMN `delivery_date`;
ALTER TABLE `products` ADD COLUMN delivery_date DATETIME;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
然后我的工厂档案:
<?php
namespace Database\Factories;
use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;
use Picqer;
class ProductFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Product::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$code = $this->faker->bothify('PHZM-##########');
$barcode = $this->generateBarcode($code);
return [
'code' => $code,
'barcode' => $barcode,
];
}
private function generateBarcode($code_to_convert) {
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
$barcode = $generator->getBarcode($code_to_convert, $generator::TYPE_CODE_128, 1, 15);
return $barcode;
}
}
感谢任何帮助。
我想您必须为 运行 创建如下脚本文件的播种器:
<?php
use Illuminate\Database\Seeder;
class SqlFileSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$path = public_path('sql/File.sql');
$sql = file_get_contents($path);
DB::unprepared($sql);
}
}
在你的工厂调用的另一个播种机中,你在调用工厂
后也运行这个播种机
<?php
use Illuminate\Database\Seeder;
class FooSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Product::factory()->count(20)->create();
$this->call([SqlFileSeeder::class]);
}
}
谢谢@alirezadp10 的指导!我已经按照他的回答解决了这个问题,并在 phpmyadmin 中使用 Update
查询导出 products
table 而不是默认的 Insert Into
作为 custom
。
Screenshot: Export as Custom
- 然后取消选中以下内容:
- 添加 DROP TABLE / TRIGGER 语句
- 添加 CREATE VIEW 语句
Screenshot: Uncheck these settings
- Select
Function to use when dumping data
更新
下拉菜单。
Screenshot: Select Update on the dropdown
- 然后按开始。
在那之后,我刚刚在 Sublime Text 3 中使用一些正则表达式替换了字段值。
Link: Regular expression search replace in Sublime Text 2
并根据我的需要重命名字段。对于@alirezadp10 的回答,我 运行 php artisan db:seed
.
我有一个为 2 列创建虚拟数据的工厂文件:代码和条形码。但是,我想 运行 一个 .sql 文件来替换我 运行 工厂命令后的数据。这样一来,我想合并将会发生并且可能会删除一些记录或添加新记录。是否可以改为合并数据。喜欢自动映射?
这是我的 .sql 文件的内容:
-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: Apr 23, 2021 at 12:14 PM
-- Server version: 10.1.28-MariaDB
-- PHP Version: 7.1.11
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `db_zachmatic`
--
--
-- Dumping data for table `products`
--
REPLACE INTO `products` (`id`, `code`, `name`, `description`, `unit`, `cost`, `srp`, `supplier`, `qty_on_hand`, `category`, `delivery_date`, `created_at`) VALUES
(12, 'P-824830', 'magnum-v', '45/90-17 MV-360 tube type tires only', 'Pieces', '', '1246', 'HH All Ventures', 3, 'Tires', '', ''),
(13, 'P-232033', 'magnum-v', '50/100-17 MV-360 tube type tire only', 'Select Product ', '', '1246', 'HH All Ventures', 1, 'Tires', '', ''),
(14, 'P-73032309', 'magnum-v', '60/90-17 MV-360 Tube Type Tire Only', 'Pieces', '', '1341', 'HH All Ventures', 0, 'Tires', '', ''),
(15, 'P-0022252', 'magnum-v', '45/90-17 MV-329 Tube Type with Tube', 'Pieces', '', '1120', 'HH All Ventures', 9, 'Tires', '', ''),
(16, 'P-323694', 'magnum-v', '50/100-17 MV-329 tube type with tube', 'Select Product ', '', '1120', 'HH All Ventures', 0, 'Tires', '', ''),
(17, 'P-023202', 'magnum-v', '60/90-17 MV-329 tube type with tube', 'Pieces', '', '1499', 'HH All Ventures', 0, 'Tires', '', ''),
(18, 'P-4729320', 'magnum-v', '70/90-17 MV-329 tube type with tube', 'Pieces', '', '1678', 'HH All Ventures', 0, 'Tires', '', ''),
(19, 'P-3020323', 'magnum-v', '120/70-13 MV-119C Tubeless', 'Pieces', '', '1829', 'HH All Ventures', 5, 'Tires', '', ''),
(20, 'P-3220830', 'magnum-v', '130/70-13 MV-119C tubeless', 'Pieces', '', '2062', 'HH All Ventures', 0, 'Tires', '', ''),
--
-- AUTO_INCREMENT for table `products`
--
ALTER TABLE `products` DROP COLUMN `id`;
ALTER TABLE `products` ADD COLUMN `id` INT AUTO_INCREMENT UNIQUE FIRST;
--
-- Drop `code` column to delete old barcodes
-- then re-add `code` column, then add `barcode` column
--
ALTER TABLE `products` DROP COLUMN `code`;
ALTER TABLE `products` ADD COLUMN code VARCHAR(255) AFTER id;
--
-- Drop `delivery_date` column to delete old column
-- then re-add `delivery_date` column, then add `barcode` column
--
ALTER TABLE `products` DROP COLUMN `delivery_date`;
ALTER TABLE `products` ADD COLUMN delivery_date DATETIME;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
然后我的工厂档案:
<?php
namespace Database\Factories;
use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;
use Picqer;
class ProductFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Product::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
$code = $this->faker->bothify('PHZM-##########');
$barcode = $this->generateBarcode($code);
return [
'code' => $code,
'barcode' => $barcode,
];
}
private function generateBarcode($code_to_convert) {
$generator = new Picqer\Barcode\BarcodeGeneratorHTML();
$barcode = $generator->getBarcode($code_to_convert, $generator::TYPE_CODE_128, 1, 15);
return $barcode;
}
}
感谢任何帮助。
我想您必须为 运行 创建如下脚本文件的播种器:
<?php
use Illuminate\Database\Seeder;
class SqlFileSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$path = public_path('sql/File.sql');
$sql = file_get_contents($path);
DB::unprepared($sql);
}
}
在你的工厂调用的另一个播种机中,你在调用工厂
后也运行这个播种机<?php
use Illuminate\Database\Seeder;
class FooSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Product::factory()->count(20)->create();
$this->call([SqlFileSeeder::class]);
}
}
谢谢@alirezadp10 的指导!我已经按照他的回答解决了这个问题,并在 phpmyadmin 中使用 Update
查询导出 products
table 而不是默认的 Insert Into
作为 custom
。
Screenshot: Export as Custom
- 然后取消选中以下内容:
- 添加 DROP TABLE / TRIGGER 语句
- 添加 CREATE VIEW 语句
Screenshot: Uncheck these settings
- Select
Function to use when dumping data
更新 下拉菜单。
Screenshot: Select Update on the dropdown
- 然后按开始。
在那之后,我刚刚在 Sublime Text 3 中使用一些正则表达式替换了字段值。 Link: Regular expression search replace in Sublime Text 2
并根据我的需要重命名字段。对于@alirezadp10 的回答,我 运行 php artisan db:seed
.