src/Shell/BasicSeedShell.php
public function importTables(array $data) {
		$tableCount = count($data);
		$this->out("<info>Starting seed of {$tableCount} table(s).</info>");

		foreach ($data as $table => $records) {
			$this->out("<info>{$table}</info>");

			// Set default field values.
			$defaults = [];
			if (array_key_exists('_defaults', $records)) {
				$defaults = $records['_defaults'];
				unset($records['_defaults']);
				$this->verbose("<success>{$table}: Default values set.</success>");
			}

			// Set entity options, if present.
			$entityOptions = [];
			if (array_key_exists('_options', $records)) {
				$entityOptions = $records['_options'];
				unset($records['_options']);
				$this->verbose("<success>{$table}: Entity options set, but...</success>");
				$this->quiet("<warning>{$table}: Deprecation notice: Change [_options] to [_entityOptions].</warning>");
			} elseif (array_key_exists('_entityOptions', $records)) {
				$entityOptions = $records['_entityOptions'];
				unset($records['_entityOptions']);
				$this->verbose("<success>{$table}: Entity options set.</success>");
			}

			// Set save options, if present.
			$saveOptions = [];
			if (array_key_exists('_saveOptions', $records)) {
				$saveOptions = $records['_saveOptions'];
				unset($records['_saveOptions']);
				$this->verbose("<success>{$table}: Table save() options set.</success>");
			}

			// Truncate the table, if requested.
			$Table = $this->loadModel($table);
			if (array_key_exists('_truncate', $records) && $records['_truncate']) {
				$this->truncateTable($Table);
			}

			unset($records['_truncate']);

			// Create or update all defined records.
			$this->importTable(
				$Table,
				$this->entityGenerator($Table, $records, $defaults, $entityOptions),
				$saveOptions
			);
		}

		$this->out("<info>Seeding complete.</info>");
	}
public function importTables(array $data) {
		$tableCount = count($data);
		$this->out("<info>Starting seed of {$tableCount} table(s).</info>");

		foreach ($data as $table => $records) {
			$this->out("<info>{$table}</info>");

			// Set default field values.
			$defaults = [];
			if (array_key_exists('_defaults', $records)) {
				$defaults = $records['_defaults'];
				unset($records['_defaults']);
				$this->verbose("<success>{$table}: Default values set.</success>");
			}

			// Set entity options, if present.
			$entityOptions = [];
			if (array_key_exists('_options', $records)) {
				$entityOptions = $records['_options'];
				unset($records['_options']);
				$this->verbose("<success>{$table}: Entity options set, but...</success>");
				$this->quiet("<warning>{$table}: Deprecation notice: Change [_options] to [_entityOptions].</warning>");
			} elseif (array_key_exists('_entityOptions', $records)) {
				$entityOptions = $records['_entityOptions'];
				unset($records['_entityOptions']);
				$this->verbose("<success>{$table}: Entity options set.</success>");
			}

			// Set save options, if present.
			$saveOptions = [];
			if (array_key_exists('_saveOptions', $records)) {
				$saveOptions = $records['_saveOptions'];
				unset($records['_saveOptions']);
				$this->verbose("<success>{$table}: Table save() options set.</success>");
			}

			// Truncate the table, if requested.
			$Table = $this->loadModel($table);
			if (array_key_exists('_truncate', $records) && $records['_truncate']) {
				$this->truncateTable($Table);
			}

			unset($records['_truncate']);

			// Create or update all defined records.
			$this->importTable(
				$Table,
				$this->entityGenerator($Table, $records, $defaults, $entityOptions),
				$saveOptions
			);
		}

		$this->out("<info>Seeding complete.</info>");
	}
public function importTables ( array $ data ) { $ tableCount = count ( $ data ) ; $ this -> out ( "<info>Starting seed of {$tableCount} table(s).</info>" ) ; foreach ( $ data as $ table => $ records ) { $ this -> out ( "<info>{$table}</info>" ) ; // Set default field values. $ defaults = [ ] ; if ( array_key_exists ( '_defaults' , $ records ) ) { $ defaults = $ records [ '_defaults' ] ; unset ( $ records [ '_defaults' ] ) ; $ this -> verbose ( "<success>{$table}: Default values set.</success>" ) ; } // Set entity options, if present. $ entityOptions = [ ] ; if ( array_key_exists ( '_options' , $ records ) ) { $ entityOptions = $ records [ '_options' ] ; unset ( $ records [ '_options' ] ) ; $ this -> verbose ( "<success>{$table}: Entity options set, but...</success>" ) ; $ this -> quiet ( "<warning>{$table}: Deprecation notice: Change [_options] to [_entityOptions].</warning>" ) ; } elseif ( array_key_exists ( '_entityOptions' , $ records ) ) { $ entityOptions = $ records [ '_entityOptions' ] ; unset ( $ records [ '_entityOptions' ] ) ; $ this -> verbose ( "<success>{$table}: Entity options set.</success>" ) ; } // Set save options, if present. $ saveOptions = [ ] ; if ( array_key_exists ( '_saveOptions' , $ records ) ) { $ saveOptions = $ records [ '_saveOptions' ] ; unset ( $ records [ '_saveOptions' ] ) ; $ this -> verbose ( "<success>{$table}: Table save() options set.</success>" ) ; } // Truncate the table, if requested. $ Table = $ this -> loadModel ( $ table ) ; if ( array_key_exists ( '_truncate' , $ records ) && $ records [ '_truncate' ] ) { $ this -> truncateTable ( $ Table ) ; } unset ( $ records [ '_truncate' ] ) ; // Create or update all defined records. $ this -> importTable ( $ Table , $ this -> entityGenerator ( $ Table , $ records , $ defaults , $ entityOptions ) , $ saveOptions ) ; } $ this -> out ( "<info>Seeding complete.</info>" ) ; }
Helper method for seeds to use when loading data from many tables.

Will create or update records as necessary to make the database contain
*at least* what is specified in this Seed. (Although any additional
records will remain untouched unless the `_truncate` key is present and
true.)

Provided data must be in the following format:

$data = [
'TableName' => [ // Name of the table to import.
//'_truncate' => true, // Will remove all existing records when present and true.
//'_options' => [ // Define options to pass to newEntity(). Allows for disabling validation
//	'validate' => false,
//],
'_defaults' => [ // Define default values for fields.
'author' => 'Jane Doe',
'is_published' => true,
],
[ // Each record must only define *unique* fields.
'id' => 1,
'title' => 'Frequently Asked Questions',
'body' => 'Lorum ipsum.',
],
[
'id' => 2,
'title' => 'About Us',
'body' => 'Bacon fillet tenderloin.',
],
],
];

@param array $data Array of [TableName => [ [record 1], [record 2]] sets.
@return void
Helper method for seeds to use when loading data from many tables .