Class ModelTask

Task class for creating and updating model files.

AppShell
Extended by BakeTask
Extended by ModelTask
Package: Cake\Console\Command\Task
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/Console/Command/Task/ModelTask.php

Properties summary

  • $_modelNames protected
    array
    Holds the model names
  • $_tables protected
    array
    Holds tables found on connection.
  • $_validations protected
    array
    Holds validation method map.
  • $path public
    string
    path to Model directory
  • $skipTables public
    array
    Tables to skip when running all()
  • $tasks public
    array
    tasks

Inherited Properties

Method Summary

  • Finds all possible keys to use on custom associations.
  • _getModelObject() protected
    Get a model object for a class name.
  • _interactive() protected
    Handles interactive baking
  • Print out all the associations of a particular type
  • all() public
    Bake all models at once.
  • bake() public
    Assembles and writes a Model file.
  • bakeFixture() public
    Interact with FixtureTask to automatically bake fixtures when baking models.
  • bakeTest() public
    Assembles and writes a unit test file
  • Interact with the user and confirm associations.
  • doActsAs() public
    Handles behaviors
  • Handles associations
  • Interact with the user and generate additional non-conventional associations
  • Handles Generation and user interaction for creating validation.
  • execute() public
    Execution method always used for tasks
  • Does individual field validation handling.
  • Find belongsTo relations and add them to the associations list.
  • interact with the user to find the displayField value for a model.
  • Find the hasAndBelongsToMany relations and add them to associations list
  • Find the hasOne and hasMany relations and add them to associations list
  • Finds a primary Key in a list of fields.
  • Get an Array of all the tables in the supplied connection will halt the script if no tables are found.

  • getName() public
    Forces the user to specify the model he wants to bake, and returns the selected model name.
  • Gets the option parser instance and configures it.
  • getTable() public
    Interact with the user to determine the table name of a particular model
  • inOptions() public
    Generate a key value list of options and a prompt.
  • Populate the _validations array
  • initialize() public
    Override initialize
  • listAll() public
    outputs the a list of possible models or controllers from database

Method Detail

_generatePossibleKeys()source protected

_generatePossibleKeys( )

Finds all possible keys to use on custom associations.

Returns

array
Array of tables and possible keys

_getModelObject()source protected

_getModelObject( string $className , string $table null )

Get a model object for a class name.

Parameters

string $className
Name of class you want model to be.
string $table optional null
Table name

Returns

Model
Model instance

_interactive()source protected

_interactive( )

Handles interactive baking

Returns

boolean

_printAssociation()source protected

_printAssociation( string $modelName , string $type , string $associations )

Print out all the associations of a particular type

Parameters

string $modelName
Name of the model relations belong to.
string $type
Name of association you want to see. i.e. 'belongsTo'
string $associations
Collection of associations.

all()source public

all( )

Bake all models at once.

bake()source public

bake( string|object $name , array|boolean $data array() )

Assembles and writes a Model file.

Parameters

string|object $name
Model name or object
array|boolean $data optional array()
if array and $name is not an object assume bake data, otherwise boolean.

Returns

string

bakeFixture()source public

bakeFixture( string $className , string $useTable null )

Interact with FixtureTask to automatically bake fixtures when baking models.

Parameters

string $className
Name of class to bake fixture for
string $useTable optional null
Optional table name for fixture to use.

See

FixtureTask::bake

bakeTest()source public

bakeTest( string $className )

Assembles and writes a unit test file

Parameters

string $className
Model class name

Returns

string

confirmAssociations()source public

confirmAssociations( Model $model , array $associations )

Interact with the user and confirm associations.

Parameters

Model $model
Temporary Model instance.
array $associations
Array of associations to be confirmed.

Returns

array
Array of confirmed associations

doActsAs()source public

doActsAs( Model $model )

Handles behaviors

Parameters

Model $model
The model object.

Returns

array
Behaviors

doAssociations()source public

doAssociations( Model $model )

Handles associations

Parameters

Model $model
The model object

Returns

array
Associations

doMoreAssociations()source public

doMoreAssociations( Model $model , array $associations )

Interact with the user and generate additional non-conventional associations

Parameters

Model $model
Temporary model instance
array $associations
Array of associations.

Returns

array
Array of associations.

doValidation()source public

doValidation( Model $model )

Handles Generation and user interaction for creating validation.

Parameters

Model $model
Model to have validations generated for.

Returns

array
validate Array of user selected validations.

execute()source public

execute( )

Execution method always used for tasks

Overrides

BakeTask::execute()

fieldValidation()source public

fieldValidation( string $fieldName , array $metaData , string $primaryKey 'id' )

Does individual field validation handling.

Parameters

string $fieldName
Name of field to be validated.
array $metaData
metadata for field
string $primaryKey optional 'id'
The primary key field.

Returns

array
Array of validation for the field.

findBelongsTo()source public

findBelongsTo( Model $model , array $associations )

Find belongsTo relations and add them to the associations list.

Parameters

Model $model
Model instance of model being generated.
array $associations
Array of in progress associations

Returns

array
Associations with belongsTo added in.

findDisplayField()source public

findDisplayField( array $fields )

interact with the user to find the displayField value for a model.

Parameters

array $fields
Array of fields to look for and choose as a displayField

Returns

mixed
Name of field to use for displayField or false if the user declines to choose

findHasAndBelongsToMany()source public

findHasAndBelongsToMany( Model $model , array $associations )

Find the hasAndBelongsToMany relations and add them to associations list

Parameters

Model $model
Model instance being generated
array $associations
Array of in-progress associations

Returns

array
Associations with hasAndBelongsToMany added in.

findHasOneAndMany()source public

findHasOneAndMany( Model $model , array $associations )

Find the hasOne and hasMany relations and add them to associations list

Parameters

Model $model
Model instance being generated
array $associations
Array of in progress associations

Returns

array
Associations with hasOne and hasMany added in.

findPrimaryKey()source public

findPrimaryKey( array $fields )

Finds a primary Key in a list of fields.

Parameters

array $fields
Array of fields that might have a primary key.

Returns

string
Name of field that is a primary key.

getAllTables()source public

getAllTables( string $useDbConfig null )

Get an Array of all the tables in the supplied connection will halt the script if no tables are found.

Parameters

string $useDbConfig optional null
Connection name to scan.

Returns

array
Array of tables in the database.

getName()source public

getName( string $useDbConfig null )

Forces the user to specify the model he wants to bake, and returns the selected model name.

Parameters

string $useDbConfig optional null
Database config name

Returns

string
The model name

getOptionParser()source public

getOptionParser( )

Gets the option parser instance and configures it.

Returns

ConsoleOptionParser

getTable()source public

getTable( string $modelName , string $useDbConfig null )

Interact with the user to determine the table name of a particular model

Parameters

string $modelName
Name of the model you want a table for.
string $useDbConfig optional null
Name of the database config you want to get tables from.

Returns

string
Table name

inOptions()source public

inOptions( array $options , string $prompt null , integer $default null )

Generate a key value list of options and a prompt.

Parameters

array $options
Array of options to use for the selections. indexes must start at 0
string $prompt optional null
Prompt to use for options list.
integer $default optional null
The default option for the given prompt.

Returns

integer
Result of user choice.

initValidations()source public

initValidations( )

Populate the _validations array

initialize()source public

initialize( )

Override initialize

listAll()source public

listAll( string $useDbConfig null )

outputs the a list of possible models or controllers from database

Parameters

string $useDbConfig optional null
Database configuration name

Returns

array

Methods inherited from BakeTask

getPath()source public

getPath( )

Gets the path for output. Checks the plugin property and returns the correct path.

Returns

string
Path to output.

startup()source public

startup( )

Disable caching and enable debug for baking. This forces the most current database schema to be used.

Properties detail

$_modelNamessource

protected array

Holds the model names

array()

$_tablessource

protected array

Holds tables found on connection.

array()

$_validationssource

protected array

Holds validation method map.

array()

$pathsource

public string

path to Model directory

null

$skipTablessource

public array

Tables to skip when running all()

array('i18n')

$taskssource

public array

tasks

array('DbConfig', 'Fixture', 'Test', 'Template')

© 2005–2017 The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/2.10/class-ModelTask.html