2017 © Pedro Peláez
 

cakephp-plugin cakephp-basic-seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

image

loadsys/cakephp-basic-seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  • Tuesday, June 19, 2018
  • by jtyost2
  • Repository
  • 9 Watchers
  • 3 Stars
  • 37,479 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 4 Open issues
  • 14 Versions
  • 6 % Grown

The README.md

CakePHP-Basic-Seed

Latest Version Software License Total Downloads, (*1)

Provides a simple mechanism for seeding data into your CakePHP application's database., (*2)

Requirements

  • PHP 5.6+
  • CakePHP 3.0+

:warning: Check the cake-2.x branch for the CakePHP v2.x compatible version. The 1.x.x semver series maintains compatibility with CakePHP 2, while ~2 tracks CakePHP 3., (*3)

Installation

Composer

$ composer require loadsys/cakephp-basic-seed:~2.0

Usage

  • Add this plugin to your application by adding this line to your bootstrap.php:
Plugin::load('BasicSeed', ['bootstrap' => false, 'routes' => false]);

This is a command line plugin. To use it:, (*4)

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.

You can also specify --file and it will use the file specified (inside of config/), (*5)

bin/cake BasicSeed.basic_seed --file seed_staging.php
# Will use the file located at `config/seed_staging.php`.
# This option always overrides --dev.

To create a seed.php file, run the init command:, (*6)

bin/cake BasicSeed.basic_seed init

To create a seed_dev.php file:, (*7)

bin/cake BasicSeed.basic_seed init --dev

To create a custom seed file, you can use the --file parameter., (*8)

bin/cake BasicSeed.basic_seed init --file seed_staging.php

Writing a Seed

The plugin provides a number of helper methods to help get data into your application more efficiently. The most notable of these is $this->importTables($data). This method takes an array structure and iterates over it, converting the array data into Entities and saving them into each named table. The generate structure of the array is as follows:, (*9)

<?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'));


Remember that the seed file is just an extension of the BasicSeedShell and that seeds do not have to conform to the above structure. You have access to anything you could normally do from inside a Shell, so for example, this is also a valid seed file:, (*10)

<?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}");
    }
}

...although it's worth point out that ::importTables() performs a more robust version of this exact process for you, including dumping validation errors when they are encountered., (*11)

Contributing

Reporting Issues

Please use GitHub Isuses for listing any known defects or issues., (*12)

Development

When developing this plugin, please fork and issue a PR for any new development., (*13)

License

MIT, (*14)

Loadsys Web Strategies 2015, (*15)

The Versions

19/06 2018

dev-f/deprecation-notice

dev-f/deprecation-notice http://github.com/loadsys/CakePHP-Basic-Seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin database testing cakephp data shell seed import

15/06 2016

dev-e/2.x-wip

dev-e/2.x-wip http://github.com/loadsys/CakePHP-Basic-Seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin database testing cakephp data shell seed import

26/10 2015

dev-master

9999999-dev http://github.com/loadsys/CakePHP-Basic-Seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin database testing cakephp data shell seed import

26/10 2015

2.1.1

2.1.1.0 http://github.com/loadsys/CakePHP-Basic-Seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin database testing cakephp data shell seed import

14/10 2015

2.1.0

2.1.0.0 http://github.com/loadsys/CakePHP-Basic-Seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin database testing cakephp data shell seed import

13/10 2015

2.0.1

2.0.1.0 http://github.com/loadsys/CakePHP-Basic-Seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin database testing cakephp data shell seed import

11/10 2015

2.0.0

2.0.0.0 http://github.com/loadsys/CakePHP-Basic-Seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  Sources   Download

MIT

The Requires

 

The Development Requires

plugin database testing cakephp data shell seed import

09/04 2015

dev-cake-2.x

dev-cake-2.x http://github.com/loadsys/CakePHP-Basic-Seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  Sources   Download

MIT

The Requires

 

testing cakephp shell

09/04 2015

dev-f/travis-builds

dev-f/travis-builds http://github.com/loadsys/CakePHP-Basic-Seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  Sources   Download

MIT

The Requires

 

testing cakephp shell

09/04 2015

1.0.4

1.0.4.0 http://github.com/loadsys/CakePHP-Basic-Seed

Provides a simple mechanism for seeding data into a CakePHP application's database.

  Sources   Download

MIT

The Requires

 

testing cakephp shell

07/04 2015

1.0.3

1.0.3.0 http://github.com/loadsys/CakePHP-Basic-Seed

CakePHP Basic Seed shell

  Sources   Download

MIT

The Requires

  • php >=5.3

 

testing cakephp shell

07/04 2015

1.0.2

1.0.2.0 http://github.com/loadsys/CakePHP-Basic-Seed

CakePHP Basic Seed shell

  Sources   Download

MIT

The Requires

  • php >=5.3

 

testing cakephp shell

30/06 2014

1.0.1

1.0.1.0 http://github.com/loadsys/CakePHP-Basic-Seed

CakePHP Basic Seed shell

  Sources   Download

MIT

The Requires

  • php >=5.3

 

testing cakephp shell

30/06 2014

1.0.0

1.0.0.0 http://github.com/loadsys/CakePHP-Basic-Seed

CakePHP Basic Seed shell

  Sources   Download

MIT

The Requires

  • php >=5.3

 

testing cakephp shell