On this page
Class ModelTask
Task class for creating and updating model files.
- AppShell
- BakeTask 
- ModelTask 
Copyright: Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
License: MIT License
Location: Cake/Console/Command/Task/ModelTask.php
Properties summary
- 
    $_modelNamesprotectedHolds the model namesarray
- 
    $_tablesprotectedHolds tables found on connection.array
- 
    $_validationsprotectedHolds validation method map.array
- 
    $pathpublicpath to Model directorystring
- 
    $skipTablespublicTables to skip when running all()array
- 
    $taskspublictasksarray
Inherited Properties
Method Summary
- 
    _generatePossibleKeys() protectedFinds all possible keys to use on custom associations.
- 
    _getModelObject() protectedGet a model object for a class name.
- 
    _interactive() protectedHandles interactive baking
- 
    _printAssociation() protectedPrint out all the associations of a particular type
- 
    all() publicBake all models at once.
- 
    bake() publicAssembles and writes a Model file.
- 
    bakeFixture() publicInteract with FixtureTask to automatically bake fixtures when baking models.
- 
    bakeTest() publicAssembles and writes a unit test file
- 
    confirmAssociations() publicInteract with the user and confirm associations.
- 
    doActsAs() publicHandles behaviors
- 
    doAssociations() publicHandles associations
- 
    doMoreAssociations() publicInteract with the user and generate additional non-conventional associations
- 
    doValidation() publicHandles Generation and user interaction for creating validation.
- 
    execute() publicExecution method always used for tasks
- 
    fieldValidation() publicDoes individual field validation handling.
- 
    findBelongsTo() publicFind belongsTo relations and add them to the associations list.
- 
    findDisplayField() publicinteract with the user to find the displayField value for a model.
- 
    findHasAndBelongsToMany() publicFind the hasAndBelongsToMany relations and add them to associations list
- 
    findHasOneAndMany() publicFind the hasOne and hasMany relations and add them to associations list
- 
    findPrimaryKey() publicFinds a primary Key in a list of fields.
- 
    getAllTables() publicGet an Array of all the tables in the supplied connection will halt the script if no tables are found. 
- 
    getName() publicForces the user to specify the model he wants to bake, and returns the selected model name.
- 
    getOptionParser() publicGets the option parser instance and configures it.
- 
    getTable() publicInteract with the user to determine the table name of a particular model
- 
    inOptions() publicGenerate a key value list of options and a prompt.
- 
    initValidations() publicPopulate the _validations array
- 
    initialize() publicOverride initialize
- 
    listAll() publicoutputs 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
arrayArray 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 $tableoptional null
- Table name
Returns
ModelModel instance
_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.
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 $dataoptional array()
- if array and $name is not an object assume bake data, otherwise boolean.
Returns
stringbakeFixture()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 $useTableoptional null
- Optional table name for fixture to use.
See
FixtureTask::bakebakeTest()source public
bakeTest( string $className )Assembles and writes a unit test file
Parameters
- 
     string $className
- Model class name
Returns
stringconfirmAssociations()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
arrayArray of confirmed associations
doActsAs()source public
doActsAs( Model $model )Handles behaviors
Parameters
- Model- $model
- The model object.
Returns
arrayBehaviors
doAssociations()source public
doAssociations( Model $model )Handles associations
Parameters
- Model- $model
- The model object
Returns
arrayAssociations
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
arrayArray 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
arrayvalidate 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 $primaryKeyoptional 'id'
- The primary key field.
Returns
arrayArray 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
arrayAssociations 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
mixedName 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
arrayAssociations 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
arrayAssociations 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
stringName 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 $useDbConfigoptional null
- Connection name to scan.
Returns
arrayArray 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 $useDbConfigoptional null
- Database config name
Returns
stringThe model name
getOptionParser()source public
getOptionParser( )Gets the option parser instance and configures it.
Returns
ConsoleOptionParsergetTable()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 $useDbConfigoptional null
- Name of the database config you want to get tables from.
Returns
stringTable 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 $promptoptional null
- Prompt to use for options list.
- 
     integer $defaultoptional null
- The default option for the given prompt.
Returns
integerResult of user choice.
listAll()source public
listAll( string $useDbConfig null )outputs the a list of possible models or controllers from database
Parameters
- 
     string $useDbConfigoptional null
- Database configuration name
Returns
arrayMethods inherited from BakeTask
getPath()source public
getPath( )Gets the path for output. Checks the plugin property and returns the correct path.
Returns
stringPath 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
© 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