1. Go to this page and download the library: Download loadsys/cakephp-basic-seed library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
/**
* 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
/**
* 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}");
}
}