loadsys / cakephp-basic-seed
为将数据注入 CakePHP 应用程序的数据库提供了一个简单的机制。
2.1.1
2015-10-26 17:12 UTC
Requires
- php: >=5.4.16
- cakephp/cakephp: ~3.0
Requires (Dev)
This package is not auto-updated.
Last update: 2024-09-14 15:47:38 UTC
README
为您的 CakePHP 应用程序的数据库提供了一种简单的方法来注入数据。
要求
- PHP 5.6+
- CakePHP 3.0+
⚠️ 请检查 cake-2.x 分支 以获取 CakePHP v2.x 兼容版本。semver 系列 1.x.x
保持与 CakePHP 2 的兼容性,而 ~2
跟踪 CakePHP 3。
安装
Composer
$ composer require loadsys/cakephp-basic-seed:~2.0
用法
- 通过在您的
bootstrap.php
中添加此行将此插件添加到您的应用程序:
Plugin::load('BasicSeed', ['bootstrap' => false, 'routes' => false]);
这是一个命令行插件。要使用它
bin/cake BasicSeed.basic_seed # Runs the `config/seed.php` seed file. # or bin/cake BasicSeed.basic_seed --dev # Runs `config/seed_dev.php` seed file.
您也可以指定 --file
并使用指定的文件(在 config/
内)
bin/cake BasicSeed.basic_seed --file seed_staging.php # Will use the file located at `config/seed_staging.php`. # This option always overrides --dev.
要创建一个 seed.php
文件,运行 init
命令
bin/cake BasicSeed.basic_seed init
要创建一个 seed_dev.php
文件
bin/cake BasicSeed.basic_seed init --dev
要创建一个自定义的种子文件,您可以使用 --file
参数。
bin/cake BasicSeed.basic_seed init --file seed_staging.php
编写种子文件
插件提供了一些辅助方法,以帮助您更有效地将数据输入到应用程序中。其中最值得注意的是 $this->importTables($data)
。此方法接受一个数组结构,遍历它,将数组数据转换为实体并将其保存到每个命名表中。数组的结构如下
<?php /** * Example BasicSeed plugin data seed file. * * Typically in `config/seed.php` or `config/seed_dev.php`. */ namespace App\Config\BasicSeed; // Write your data import statements here. $data = [ /** * Each key in the top-level of the array must be the proper name of a * Table into which the contained records will be imported. */ 'TableName' => [ /** * When _truncate is enabled, ALL existing records will be removed * from the table before loading! */ //'_truncate' => true, /** * The _entityOptions array is passed to Table::newEntity() and * Table::patchEntity(). It can be used to disable validation. * * Also be aware that the Shell sets * `['accessibleFields' => ['*' => true]]` by default in order to * more easily "prime" new Entities with all of the values * specified in $data, including fixed primary keys. This bypasses * your normal Entity `::$_accessible` settings, so it's good to * be aware of this if you're using a seed to "refresh" existing * data. */ //'_entityOptions' => [ // 'validate' => false, //], /** * The _saveOptions array is passed to Table::save(). It can be * used to disable rules checking. */ //'_saveOptions' => [ // 'checkRules' => false, //], /** * You can provide default values that will be merged into each * record before the Entity is created. Can be used to reduce * unnecessary repetition in imported records. */ '_defaults' => [ 'is_active' => true, ], /** * Everything else is counted as a separate record to import. * Remember that combined with [_defaults], you only need to specify * the **unique** fields for each record. */ [ /** * Existing DB records will be matched and updated using the * primary key, if provided. Otherwise, the Shell will simply * attempt to insert every record, so be mindful of fields * that require uniqueness. */ 'id' => 1, 'name' => 'record 1', ], ], ]; /** * Perform the data import using the array structure above. */ $this->importTables($data); /** * If you want to import another seed file in addition to this one (say * for example that in development, you want all of your seed_dev data, * **plus** all of your seed data from production), you can call the * import yourself directly: */ $this->hr(); $this->out('<info>Loading production data in addition to dev data...</info>'); $this->includeFile($this->absolutePath('seed.php'));
请记住,种子文件仅仅是 BasicSeedShell 的扩展,种子文件不一定需要符合上述结构。您可以访问 Shell 内部可以执行的所有操作,例如,这也是一个有效的种子文件
<?php /** * Another example BasicSeed plugin data seed file. */ namespace App\Config\BasicSeed; $Posts = $this->loadModel('Posts'); $posts = [ ['id' => 1, 'title' => 'Foo', 'body' => 'Lorem ipsum.'], ['id' => 2, 'title' => 'Bar', 'body' => 'The meaning of life is 42.'], ]; foreach ($posts as $p) { $entity = $Posts->newEntity($p); // Careful, validation is still on! if($Posts->save($entity)) { $this->out("Saved {$entity->id}"); } else { $this->warning("Save failed where title = {$entity->title}"); } }
...尽管值得指出的是,::importTables()
为您执行了更健壮的此过程版本,包括在遇到验证错误时导出验证错误。
贡献
报告问题
请使用 GitHub Issues 列出任何已知的缺陷或问题。
开发
在开发此插件时,请进行分支并提交 PR 以进行任何新开发。