Skip to content

Bake command

Generate factory classes from your existing schema. Recommended over hand-writing them.

Loading the plugin

In src/Application.php:

php
$this->addPlugin('CakephpFixtureFactories');

See the cookbook for how to load a plugin.

Usage

bash
bin/cake bake fixture_factory -h

shows all options. Common patterns:

bash
# One factory at a time
bin/cake bake fixture_factory Articles

# All models in your app
bin/cake bake fixture_factory -a

# Include association helpers (->withAuthors(), ->withCountry(), …)
bin/cake bake fixture_factory Articles -m

# Bake into a plugin's namespace
bin/cake bake fixture_factory Articles -p MyPlugin

Options

FlagEffect
-aBake every model in the app
-mAdd with…() helpers based on associations
-p PluginBake into a plugin's namespace
-hShow all available options

Customizing the generated code

Two configuration keys influence what bake writes:

  • defaultDataMap — map column types to generator method calls.
  • columnPatterns — map column-name regexes to generator method calls.

Both let you teach bake what phone, zip, email, etc. should look like in your domain. See the Configuration Reference.

php
// config/app.php
'FixtureFactories' => [
    'columnPatterns' => [
        '/^phone/' => '$generator->phoneNumber()',
        '/^zip/'   => '$generator->postcode()',
    ],
],

After regenerating, fields named phone_* and zip_* will be filled with the matching generator method instead of a generic string.

Released under the MIT License.