2007-05-14 12:24:40 +00:00
< ? php
/* SVN FILE: $Id$ */
/**
* The ModelTask handles creating and updating models files .
*
* Long description for file
*
* PHP versions 4 and 5
*
* CakePHP ( tm ) : Rapid Development Framework < http :// www . cakephp . org />
* Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* 1785 E . Sahara Avenue , Suite 490 - 204
* Las Vegas , Nevada 89104
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice .
*
* @ filesource
* @ copyright Copyright 2005 - 2007 , Cake Software Foundation , Inc .
* @ link http :// www . cakefoundation . org / projects / info / cakephp CakePHP ( tm ) Project
* @ package cake
* @ subpackage cake . cake . console . libs . tasks
* @ since CakePHP ( tm ) v 1.2
* @ version $Revision $
* @ modifiedby $LastChangedBy $
* @ lastmodified $Date $
* @ license http :// www . opensource . org / licenses / mit - license . php The MIT License
*/
/**
* Task class for creating and updating model files .
*
* @ package cake
* @ subpackage cake . cake . console . libs . tasks
*/
class ModelTask extends Shell {
2007-06-03 23:56:33 +00:00
/**
* Execution method always used for tasks
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-03 23:56:33 +00:00
*/
2007-05-14 12:24:40 +00:00
function execute () {
2007-06-20 06:15:35 +00:00
if ( empty ( $this -> args )) {
2007-05-14 12:24:40 +00:00
$this -> __interactive ();
}
2007-09-10 11:18:52 +00:00
2007-07-25 04:38:28 +00:00
if ( ! empty ( $this -> args [ 0 ])) {
2007-07-05 08:50:56 +00:00
$model = $this -> args [ 0 ];
if ( $this -> __bake ( $model )) {
if ( $this -> _checkUnitTest ()) {
$this -> __bakeTest ( $model );
}
2007-09-10 11:18:52 +00:00
}
2007-07-05 08:50:56 +00:00
}
2007-05-14 12:24:40 +00:00
}
2007-06-03 23:56:33 +00:00
/**
* Handles interactive baking
*
* @ access private
*/
2007-05-14 12:24:40 +00:00
function __interactive () {
$this -> hr ();
$this -> out ( 'Model Bake:' );
$this -> hr ();
$this -> interactive = true ;
$useTable = null ;
$primaryKey = 'id' ;
$validate = array ();
2007-05-29 06:30:08 +00:00
$associations = array ( 'belongsTo' => array (), 'hasOne' => array (), 'hasMany' , 'hasAndBelongsToMany' => array ());
2007-09-30 20:47:37 +00:00
$useDbConfig = $this -> in ( 'Database Connection:' , null , 'default' );
2007-09-15 20:23:30 +00:00
$currentModelName = $this -> getName ( $useDbConfig );
2007-05-14 12:24:40 +00:00
$db =& ConnectionManager :: getDataSource ( $useDbConfig );
$tableIsGood = false ;
$useTable = Inflector :: tableize ( $currentModelName );
$fullTableName = $db -> fullTableName ( $useTable , false );
2007-06-20 06:15:35 +00:00
if ( array_search ( $useTable , $this -> __tables ) === false ) {
2007-05-14 12:24:40 +00:00
$this -> out ( " \n Given your model named ' $currentModelName ', Cake would expect a database table named ' " . $fullTableName . " '. " );
$tableIsGood = $this -> in ( 'do you want to use this table?' , array ( 'y' , 'n' ), 'y' );
}
if ( low ( $tableIsGood ) == 'n' || low ( $tableIsGood ) == 'no' ) {
$useTable = $this -> in ( 'What is the name of the table (enter "null" to use NO table)?' );
}
2007-06-20 06:15:35 +00:00
while ( $tableIsGood == false && low ( $useTable ) != 'null' ) {
2007-05-14 12:24:40 +00:00
if ( is_array ( $this -> __tables ) && ! in_array ( $useTable , $this -> __tables )) {
$fullTableName = $db -> fullTableName ( $useTable , false );
$this -> out ( $fullTableName . ' does not exist.' );
$useTable = $this -> in ( 'What is the name of the table (enter "null" to use NO table)?' );
$tableIsGood = false ;
} else {
$tableIsGood = true ;
}
}
$wannaDoValidation = $this -> in ( 'Would you like to supply validation criteria for the fields in your model?' , array ( 'y' , 'n' ), 'y' );
2007-06-20 06:15:35 +00:00
if ( in_array ( $useTable , $this -> __tables )) {
2007-11-17 17:37:23 +00:00
App :: import ( 'Model' );
2007-05-14 12:24:40 +00:00
$tempModel = new Model ( false , $useTable );
$modelFields = $db -> describe ( $tempModel );
2007-09-10 11:18:52 +00:00
if ( ! array_key_exists ( 'id' , $modelFields )) {
foreach ( $modelFields as $name => $field ) {
break ;
}
$primaryKey = $this -> in ( 'What is the primaryKey?' , null , $name );
2007-05-14 12:24:40 +00:00
}
}
$validate = array ();
if ( array_search ( $useTable , $this -> __tables ) !== false && ( low ( $wannaDoValidation ) == 'y' || low ( $wannaDoValidation ) == 'yes' )) {
2007-08-21 21:46:59 +00:00
foreach ( $modelFields as $fieldName => $field ) {
2007-05-14 12:24:40 +00:00
$this -> out ( '' );
2007-08-21 21:46:59 +00:00
$prompt = 'Name: ' . $fieldName . " \n " ;
2007-05-14 12:24:40 +00:00
$prompt .= 'Type: ' . $field [ 'type' ] . " \n " ;
$prompt .= '---------------------------------------------------------------' . " \n " ;
$prompt .= 'Please select one of the following validation options:' . " \n " ;
$prompt .= '---------------------------------------------------------------' . " \n " ;
$prompt .= " 1- VALID_NOT_EMPTY \n " ;
$prompt .= " 2- VALID_EMAIL \n " ;
$prompt .= " 3- VALID_NUMBER \n " ;
$prompt .= " 4- VALID_YEAR \n " ;
$prompt .= " 5- Do not do any validation on this field. \n \n " ;
$prompt .= " ... or enter in a valid regex validation string. \n \n " ;
2007-08-21 21:46:59 +00:00
if ( $field [ 'null' ] == 1 || $fieldName == $primaryKey || $fieldName == 'created' || $fieldName == 'modified' ) {
2007-05-14 12:24:40 +00:00
$validation = $this -> in ( $prompt , null , '5' );
} else {
$validation = $this -> in ( $prompt , null , '1' );
}
switch ( $validation ) {
case '1' :
2007-08-21 21:46:59 +00:00
$validate [ $fieldName ] = 'VALID_NOT_EMPTY' ;
2007-05-14 12:24:40 +00:00
break ;
case '2' :
2007-08-21 21:46:59 +00:00
$validate [ $fieldName ] = 'VALID_EMAIL' ;
2007-05-14 12:24:40 +00:00
break ;
case '3' :
2007-08-21 21:46:59 +00:00
$validate [ $fieldName ] = 'VALID_NUMBER' ;
2007-05-14 12:24:40 +00:00
break ;
case '4' :
2007-08-21 21:46:59 +00:00
$validate [ $fieldName ] = 'VALID_YEAR' ;
2007-05-14 12:24:40 +00:00
break ;
case '5' :
break ;
default :
2007-08-21 21:46:59 +00:00
$validate [ $fieldName ] = $validation ;
2007-05-14 12:24:40 +00:00
break ;
}
}
}
$wannaDoAssoc = $this -> in ( 'Would you like to define model associations (hasMany, hasOne, belongsTo, etc.)?' , array ( 'y' , 'n' ), 'y' );
2007-06-20 06:15:35 +00:00
if (( low ( $wannaDoAssoc ) == 'y' || low ( $wannaDoAssoc ) == 'yes' )) {
2007-05-14 12:24:40 +00:00
$this -> out ( 'One moment while I try to detect any associations...' );
$possibleKeys = array ();
//Look for belongsTo
$i = 0 ;
2007-08-21 21:46:59 +00:00
foreach ( $modelFields as $fieldName => $field ) {
$offset = strpos ( $fieldName , '_id' );
if ( $fieldName != $primaryKey && $offset !== false ) {
$tmpModelName = $this -> _modelNameFromKey ( $fieldName );
2007-05-14 12:24:40 +00:00
$associations [ 'belongsTo' ][ $i ][ 'alias' ] = $tmpModelName ;
$associations [ 'belongsTo' ][ $i ][ 'className' ] = $tmpModelName ;
2007-08-21 21:46:59 +00:00
$associations [ 'belongsTo' ][ $i ][ 'foreignKey' ] = $fieldName ;
2007-05-14 12:24:40 +00:00
$i ++ ;
}
}
//Look for hasOne and hasMany and hasAndBelongsToMany
$i = 0 ;
$j = 0 ;
2007-06-20 06:15:35 +00:00
foreach ( $this -> __tables as $otherTable ) {
2007-05-14 12:24:40 +00:00
$tempOtherModel = & new Model ( false , $otherTable );
$modelFieldsTemp = $db -> describe ( $tempOtherModel );
2007-09-10 11:18:52 +00:00
foreach ( $modelFieldsTemp as $fieldName => $field ) {
2007-06-20 06:15:35 +00:00
if ( $field [ 'type' ] == 'integer' || $field [ 'type' ] == 'string' ) {
2007-08-21 21:46:59 +00:00
$possibleKeys [ $otherTable ][] = $fieldName ;
2007-05-14 12:24:40 +00:00
}
2007-08-21 21:46:59 +00:00
if ( $fieldName != $primaryKey && $fieldName == $this -> _modelKey ( $currentModelName )) {
2007-05-14 12:24:40 +00:00
$tmpModelName = $this -> _modelName ( $otherTable );
$associations [ 'hasOne' ][ $j ][ 'alias' ] = $tmpModelName ;
$associations [ 'hasOne' ][ $j ][ 'className' ] = $tmpModelName ;
2007-08-21 21:46:59 +00:00
$associations [ 'hasOne' ][ $j ][ 'foreignKey' ] = $fieldName ;
2007-05-14 12:24:40 +00:00
$associations [ 'hasMany' ][ $j ][ 'alias' ] = $tmpModelName ;
$associations [ 'hasMany' ][ $j ][ 'className' ] = $tmpModelName ;
2007-08-21 21:46:59 +00:00
$associations [ 'hasMany' ][ $j ][ 'foreignKey' ] = $fieldName ;
2007-05-14 12:24:40 +00:00
$j ++ ;
}
}
$offset = strpos ( $otherTable , $useTable . '_' );
2007-06-20 06:15:35 +00:00
if ( $offset !== false ) {
2007-05-14 12:24:40 +00:00
$offset = strlen ( $useTable . '_' );
$tmpModelName = $this -> _modelName ( substr ( $otherTable , $offset ));
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] = $tmpModelName ;
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'className' ] = $tmpModelName ;
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'foreignKey' ] = $this -> _modelKey ( $currentModelName );
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'associationForeignKey' ] = $this -> _modelKey ( $tmpModelName );
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'joinTable' ] = $otherTable ;
$i ++ ;
}
$offset = strpos ( $otherTable , '_' . $useTable );
if ( $offset !== false ) {
$tmpModelName = $this -> _modelName ( substr ( $otherTable , 0 , $offset ));
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] = $tmpModelName ;
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'className' ] = $tmpModelName ;
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'foreignKey' ] = $this -> _modelKey ( $currentModelName );
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'associationForeignKey' ] = $this -> _modelKey ( $tmpModelName );
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'joinTable' ] = $otherTable ;
$i ++ ;
}
}
$this -> out ( 'Done.' );
$this -> hr ();
//if none found...
2007-06-20 06:15:35 +00:00
if ( empty ( $associations )) {
2007-05-14 12:24:40 +00:00
$this -> out ( 'None found.' );
} else {
$this -> out ( 'Please confirm the following associations:' );
$this -> hr ();
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'belongsTo' ])) {
2007-05-14 12:24:40 +00:00
$count = count ( $associations [ 'belongsTo' ]);
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i < $count ; $i ++ ) {
if ( $currentModelName == $associations [ 'belongsTo' ][ $i ][ 'alias' ]) {
2007-05-14 12:24:40 +00:00
$response = $this -> in ( " { $currentModelName } belongsTo { $associations [ 'belongsTo' ][ $i ][ 'alias' ] } \n This looks like a self join. Do you want to specify an alternate association alias? " , array ( 'y' , 'n' ), 'y' );
2007-06-20 06:15:35 +00:00
if ( 'y' == low ( $response ) || 'yes' == low ( $response )) {
2007-05-14 12:24:40 +00:00
$associations [ 'belongsTo' ][ $i ][ 'alias' ] = $this -> in ( " So what is the alias? " , null , $associations [ 'belongsTo' ][ $i ][ 'alias' ]);
}
2007-06-20 06:15:35 +00:00
if ( $currentModelName != $associations [ 'belongsTo' ][ $i ][ 'alias' ]) {
2007-05-14 12:24:40 +00:00
$response = $this -> in ( " $currentModelName belongsTo { $associations [ 'belongsTo' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
} else {
$response = 'n' ;
}
} else {
$response = $this -> in ( " $currentModelName belongsTo { $associations [ 'belongsTo' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
}
2007-06-20 06:15:35 +00:00
if ( 'n' == low ( $response ) || 'no' == low ( $response )) {
2007-05-14 12:24:40 +00:00
unset ( $associations [ 'belongsTo' ][ $i ]);
}
}
$associations [ 'belongsTo' ] = array_merge ( $associations [ 'belongsTo' ]);
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'hasOne' ])) {
2007-05-14 12:24:40 +00:00
$count = count ( $associations [ 'hasOne' ]);
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i < $count ; $i ++ ) {
if ( $currentModelName == $associations [ 'hasOne' ][ $i ][ 'alias' ]) {
2007-05-14 12:24:40 +00:00
$response = $this -> in ( " { $currentModelName } hasOne { $associations [ 'hasOne' ][ $i ][ 'alias' ] } \n This looks like a self join. Do you want to specify an alternate association alias? " , array ( 'y' , 'n' ), 'y' );
2007-06-20 06:15:35 +00:00
if ( 'y' == low ( $response ) || 'yes' == low ( $response )) {
2007-05-14 12:24:40 +00:00
$associations [ 'hasOne' ][ $i ][ 'alias' ] = $this -> in ( " So what is the alias? " , null , $associations [ 'hasOne' ][ $i ][ 'alias' ]);
}
2007-06-20 06:15:35 +00:00
if ( $currentModelName != $associations [ 'hasOne' ][ $i ][ 'alias' ]) {
2007-05-14 12:24:40 +00:00
$response = $this -> in ( " $currentModelName hasOne { $associations [ 'hasOne' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
} else {
$response = 'n' ;
}
} else {
$response = $this -> in ( " $currentModelName hasOne { $associations [ 'hasOne' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
}
2007-06-20 06:15:35 +00:00
if ( 'n' == low ( $response ) || 'no' == low ( $response )) {
2007-05-14 12:24:40 +00:00
unset ( $associations [ 'hasOne' ][ $i ]);
}
}
$associations [ 'hasOne' ] = array_merge ( $associations [ 'hasOne' ]);
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'hasMany' ])) {
2007-05-14 12:24:40 +00:00
$count = count ( $associations [ 'hasMany' ]);
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i < $count ; $i ++ ) {
if ( $currentModelName == $associations [ 'hasMany' ][ $i ][ 'alias' ]) {
2007-05-14 12:24:40 +00:00
$response = $this -> in ( " { $currentModelName } hasMany { $associations [ 'hasMany' ][ $i ][ 'alias' ] } \n This looks like a self join. Do you want to specify an alternate association alias? " , array ( 'y' , 'n' ), 'y' );
2007-06-20 06:15:35 +00:00
if ( 'y' == low ( $response ) || 'yes' == low ( $response )) {
2007-05-14 12:24:40 +00:00
$associations [ 'hasMany' ][ $i ][ 'alias' ] = $this -> in ( " So what is the alias? " , null , $associations [ 'hasMany' ][ $i ][ 'alias' ]);
}
2007-06-20 06:15:35 +00:00
if ( $currentModelName != $associations [ 'hasMany' ][ $i ][ 'alias' ]) {
2007-05-14 12:24:40 +00:00
$response = $this -> in ( " $currentModelName hasMany { $associations [ 'hasMany' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
} else {
$response = 'n' ;
}
} else {
$response = $this -> in ( " $currentModelName hasMany { $associations [ 'hasMany' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
}
2007-06-20 06:15:35 +00:00
if ( 'n' == low ( $response ) || 'no' == low ( $response )) {
2007-05-14 12:24:40 +00:00
unset ( $associations [ 'hasMany' ][ $i ]);
}
}
$associations [ 'hasMany' ] = array_merge ( $associations [ 'hasMany' ]);
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'hasAndBelongsToMany' ])) {
2007-05-14 12:24:40 +00:00
$count = count ( $associations [ 'hasAndBelongsToMany' ]);
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i < $count ; $i ++ ) {
if ( $currentModelName == $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ]) {
2007-05-14 12:24:40 +00:00
$response = $this -> in ( " { $currentModelName } hasAndBelongsToMany { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] } \n This looks like a self join. Do you want to specify an alternate association alias? " , array ( 'y' , 'n' ), 'y' );
2007-06-20 06:15:35 +00:00
if ( 'y' == low ( $response ) || 'yes' == low ( $response )) {
2007-05-14 12:24:40 +00:00
$associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] = $this -> in ( " So what is the alias? " , null , $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ]);
}
2007-06-20 06:15:35 +00:00
if ( $currentModelName != $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ]) {
2007-05-14 12:24:40 +00:00
$response = $this -> in ( " $currentModelName hasAndBelongsToMany { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
} else {
$response = 'n' ;
}
} else {
$response = $this -> in ( " $currentModelName hasAndBelongsToMany { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] } ? " , array ( 'y' , 'n' ), 'y' );
}
2007-06-20 06:15:35 +00:00
if ( 'n' == low ( $response ) || 'no' == low ( $response )) {
2007-05-14 12:24:40 +00:00
unset ( $associations [ 'hasAndBelongsToMany' ][ $i ]);
}
}
$associations [ 'hasAndBelongsToMany' ] = array_merge ( $associations [ 'hasAndBelongsToMany' ]);
}
}
$wannaDoMoreAssoc = $this -> in ( 'Would you like to define some additional model associations?' , array ( 'y' , 'n' ), 'n' );
2007-06-20 06:15:35 +00:00
while (( low ( $wannaDoMoreAssoc ) == 'y' || low ( $wannaDoMoreAssoc ) == 'yes' )) {
2007-05-29 06:30:08 +00:00
$assocs = array ( 1 => 'belongsTo' , 2 => 'hasOne' , 3 => 'hasMany' , 4 => 'hasAndBelongsToMany' );
2007-05-14 12:24:40 +00:00
$bad = true ;
2007-06-20 06:15:35 +00:00
while ( $bad ) {
2007-05-14 12:24:40 +00:00
$this -> out ( 'What is the association type?' );
$prompt = " 1- belongsTo \n " ;
$prompt .= " 2- hasOne \n " ;
$prompt .= " 3- hasMany \n " ;
$prompt .= " 4- hasAndBelongsToMany \n " ;
$assocType = intval ( $this -> in ( $prompt , null , null ));
2007-06-20 06:15:35 +00:00
if ( intval ( $assocType ) < 1 || intval ( $assocType ) > 4 ) {
2007-05-14 12:24:40 +00:00
$this -> out ( 'The selection you entered was invalid. Please enter a number between 1 and 4.' );
} else {
$bad = false ;
}
}
$this -> out ( 'For the following options be very careful to match your setup exactly. Any spelling mistakes will cause errors.' );
$this -> hr ();
$associationName = $this -> in ( 'What is the name of this association?' );
$className = $this -> in ( 'What className will ' . $associationName . ' use?' , null , $associationName );
$suggestedForeignKey = null ;
2007-06-20 06:15:35 +00:00
if ( $assocType == '1' ) {
2007-05-14 12:24:40 +00:00
$showKeys = $possibleKeys [ $useTable ];
$suggestedForeignKey = $this -> _modelKey ( $associationName );
} else {
$otherTable = Inflector :: tableize ( $className );
2007-06-20 06:15:35 +00:00
if ( in_array ( $otherTable , $this -> __tables )) {
if ( $assocType < '4' ) {
2007-05-14 12:24:40 +00:00
$showKeys = $possibleKeys [ $otherTable ];
} else {
$showKeys = null ;
}
} else {
$otherTable = $this -> in ( 'What is the table for this class?' );
$showKeys = $possibleKeys [ $otherTable ];
}
$suggestedForeignKey = $this -> _modelKey ( $currentModelName );
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $showKeys )) {
2007-05-14 12:24:40 +00:00
$this -> out ( 'A helpful List of possible keys' );
for ( $i = 0 ; $i < count ( $showKeys ); $i ++ ) {
$this -> out ( $i + 1 . " . " . $showKeys [ $i ]);
}
$foreignKey = $this -> in ( 'What is the foreignKey? Choose a number.' );
if ( intval ( $foreignKey ) > 0 && intval ( $foreignKey ) <= $i ) {
$foreignKey = $showKeys [ intval ( $foreignKey ) - 1 ];
}
}
2007-06-20 06:15:35 +00:00
if ( ! isset ( $foreignKey )) {
2007-05-14 12:24:40 +00:00
$foreignKey = $this -> in ( 'What is the foreignKey? Specify your own.' , null , $suggestedForeignKey );
}
2007-06-20 06:15:35 +00:00
if ( $assocType == '4' ) {
2007-05-14 12:24:40 +00:00
$associationForeignKey = $this -> in ( 'What is the associationForeignKey?' , null , $this -> _modelKey ( $currentModelName ));
$joinTable = $this -> in ( 'What is the joinTable?' );
}
$associations [ $assocs [ $assocType ]] = array_values ( $associations [ $assocs [ $assocType ]]);
$count = count ( $associations [ $assocs [ $assocType ]]);
$i = ( $count > 0 ) ? $count : 0 ;
$associations [ $assocs [ $assocType ]][ $i ][ 'alias' ] = $associationName ;
$associations [ $assocs [ $assocType ]][ $i ][ 'className' ] = $className ;
$associations [ $assocs [ $assocType ]][ $i ][ 'foreignKey' ] = $foreignKey ;
2007-06-20 06:15:35 +00:00
if ( $assocType == '4' ) {
2007-05-14 12:24:40 +00:00
$associations [ $assocs [ $assocType ]][ $i ][ 'associationForeignKey' ] = $associationForeignKey ;
$associations [ $assocs [ $assocType ]][ $i ][ 'joinTable' ] = $joinTable ;
}
$wannaDoMoreAssoc = $this -> in ( 'Define another association?' , array ( 'y' , 'n' ), 'y' );
}
}
$this -> out ( '' );
$this -> hr ();
$this -> out ( 'The following model will be created:' );
$this -> hr ();
$this -> out ( " Model Name: $currentModelName " );
$this -> out ( " DB Connection: " . $useDbConfig );
$this -> out ( " DB Table: " . $fullTableName );
2007-06-20 06:15:35 +00:00
if ( $primaryKey != 'id' ) {
2007-05-14 12:24:40 +00:00
$this -> out ( " Primary Key: " . $primaryKey );
}
$this -> out ( " Validation: " . print_r ( $validate , true ));
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations )) {
2007-05-14 12:24:40 +00:00
$this -> out ( " Associations: " );
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'belongsTo' ])) {
for ( $i = 0 ; $i < count ( $associations [ 'belongsTo' ]); $i ++ ) {
2007-05-14 12:24:40 +00:00
$this -> out ( " $currentModelName belongsTo { $associations [ 'belongsTo' ][ $i ][ 'alias' ] } " );
}
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'hasOne' ])) {
for ( $i = 0 ; $i < count ( $associations [ 'hasOne' ]); $i ++ ) {
2007-05-14 12:24:40 +00:00
$this -> out ( " $currentModelName hasOne { $associations [ 'hasOne' ][ $i ][ 'alias' ] } " );
}
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'hasMany' ])) {
for ( $i = 0 ; $i < count ( $associations [ 'hasMany' ]); $i ++ ) {
2007-05-14 12:24:40 +00:00
$this -> out ( " $currentModelName hasMany { $associations [ 'hasMany' ][ $i ][ 'alias' ] } " );
}
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'hasAndBelongsToMany' ])) {
for ( $i = 0 ; $i < count ( $associations [ 'hasAndBelongsToMany' ]); $i ++ ) {
2007-05-14 12:24:40 +00:00
$this -> out ( " $currentModelName hasAndBelongsToMany { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] } " );
}
}
}
$this -> hr ();
$looksGood = $this -> in ( 'Look okay?' , array ( 'y' , 'n' ), 'y' );
if ( low ( $looksGood ) == 'y' || low ( $looksGood ) == 'yes' ) {
if ( $useTable == Inflector :: tableize ( $currentModelName )) {
// set it to null...
// putting $useTable in the model
// is unnecessary.
$useTable = null ;
}
2007-06-20 06:15:35 +00:00
if ( $this -> __bake ( $currentModelName , $useDbConfig , $useTable , $primaryKey , $validate , $associations )) {
2007-05-14 12:24:40 +00:00
if ( $this -> _checkUnitTest ()) {
$this -> __bakeTest ( $currentModelName );
}
}
} else {
$this -> out ( 'Bake Aborted.' );
}
}
/**
* Assembles and writes a Model file .
*
2007-10-22 05:52:20 +00:00
* @ param string $name Model name
* @ param object $useDbConfig Database configuration setting to use
* @ param string $useTable Table to use
* @ param string $primaryKey Primary key to use
* @ param array $validate Validation rules
* @ param array $associations Model bindings
* @ access private
2007-05-14 12:24:40 +00:00
*/
2007-05-29 06:30:08 +00:00
function __bake ( $name , $useDbConfig = 'default' , $useTable = null , $primaryKey = 'id' , $validate = array (), $associations = array ()) {
2007-05-14 12:24:40 +00:00
$out = " <?php \n " ;
$out .= " class { $name } extends AppModel { \n \n " ;
$out .= " \t var \$ name = ' { $name } '; \n " ;
if ( $useDbConfig != 'default' ) {
$out .= " \t var \$ useDbConfig = ' $useDbConfig '; \n " ;
}
if ( $useTable != null ) {
$out .= " \t var \$ useTable = ' $useTable '; \n " ;
}
if ( $primaryKey != 'id' ) {
$out .= " \t var \$ primaryKey = ' $primaryKey '; \n " ;
}
if ( count ( $validate )) {
$out .= " \t var \$ validate = array( \n " ;
$keys = array_keys ( $validate );
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i < count ( $validate ); $i ++ ) {
2007-05-14 12:24:40 +00:00
$out .= " \t \t ' " . $keys [ $i ] . " ' => " . $validate [ $keys [ $i ]] . " , \n " ;
}
$out .= " \t ); \n " ;
}
$out .= " \n " ;
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations )) {
2007-09-16 19:56:55 +00:00
if ( ! empty ( $associations [ 'belongsTo' ]) || ! empty ( $associations [ '$hasOne' ]) || ! empty ( $associations [ 'hasMany' ]) || ! empty ( $associations [ 'hasAndBelongsToMany' ])) {
$out .= " \t //The Associations below have been created with all possible keys, those that are not needed can be removed \n " ;
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'belongsTo' ])) {
2007-05-14 12:24:40 +00:00
$out .= " \t var \$ belongsTo = array( \n " ;
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i < count ( $associations [ 'belongsTo' ]); $i ++ ) {
2007-05-14 12:24:40 +00:00
$out .= " \t \t \t ' { $associations [ 'belongsTo' ][ $i ][ 'alias' ] } ' => " ;
$out .= " array('className' => ' { $associations [ 'belongsTo' ][ $i ][ 'className' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'foreignKey' => ' { $associations [ 'belongsTo' ][ $i ][ 'foreignKey' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'fields' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'counterCache' => '' " ;
$out .= " ), \n " ;
}
$out .= " \t ); \n \n " ;
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'hasOne' ])) {
2007-05-14 12:24:40 +00:00
$out .= " \t var \$ hasOne = array( \n " ;
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i < count ( $associations [ 'hasOne' ]); $i ++ ) {
2007-05-14 12:24:40 +00:00
$out .= " \t \t \t ' { $associations [ 'hasOne' ][ $i ][ 'alias' ] } ' => " ;
$out .= " array('className' => ' { $associations [ 'hasOne' ][ $i ][ 'className' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'foreignKey' => ' { $associations [ 'hasOne' ][ $i ][ 'foreignKey' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'fields' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'dependent' => '' " ;
$out .= " ), \n " ;
}
$out .= " \t ); \n \n " ;
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'hasMany' ])) {
2007-05-14 12:24:40 +00:00
$out .= " \t var \$ hasMany = array( \n " ;
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i < count ( $associations [ 'hasMany' ]); $i ++ ) {
2007-05-14 12:24:40 +00:00
$out .= " \t \t \t ' { $associations [ 'hasMany' ][ $i ][ 'alias' ] } ' => " ;
$out .= " array('className' => ' { $associations [ 'hasMany' ][ $i ][ 'className' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'foreignKey' => ' { $associations [ 'hasMany' ][ $i ][ 'foreignKey' ] } ', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'fields' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'limit' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'offset' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'dependent' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'exclusive' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'finderQuery' => '', \n " ;
$out .= " \t \t \t \t \t \t \t \t 'counterQuery' => '' " ;
$out .= " ), \n " ;
}
$out .= " \t ); \n \n " ;
}
2007-06-20 06:15:35 +00:00
if ( ! empty ( $associations [ 'hasAndBelongsToMany' ])) {
2007-05-14 12:24:40 +00:00
$out .= " \t var \$ hasAndBelongsToMany = array( \n " ;
2007-06-20 06:15:35 +00:00
for ( $i = 0 ; $i < count ( $associations [ 'hasAndBelongsToMany' ]); $i ++ ) {
2007-05-14 12:24:40 +00:00
$out .= " \t \t \t ' { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'alias' ] } ' => " ;
$out .= " array('className' => ' { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'className' ] } ', \n " ;
$out .= " \t \t \t \t \t \t 'joinTable' => ' { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'joinTable' ] } ', \n " ;
$out .= " \t \t \t \t \t \t 'foreignKey' => ' { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'foreignKey' ] } ', \n " ;
$out .= " \t \t \t \t \t \t 'associationForeignKey' => ' { $associations [ 'hasAndBelongsToMany' ][ $i ][ 'associationForeignKey' ] } ', \n " ;
$out .= " \t \t \t \t \t \t 'conditions' => '', \n " ;
$out .= " \t \t \t \t \t \t 'fields' => '', \n " ;
$out .= " \t \t \t \t \t \t 'order' => '', \n " ;
$out .= " \t \t \t \t \t \t 'limit' => '', \n " ;
$out .= " \t \t \t \t \t \t 'offset' => '', \n " ;
$out .= " \t \t \t \t \t \t 'unique' => '', \n " ;
$out .= " \t \t \t \t \t \t 'finderQuery' => '', \n " ;
$out .= " \t \t \t \t \t \t 'deleteQuery' => '', \n " ;
$out .= " \t \t \t \t \t \t 'insertQuery' => '' " ;
$out .= " ), \n " ;
}
$out .= " \t ); \n \n " ;
}
}
$out .= " } \n " ;
$out .= " ?> " ;
$filename = MODELS . Inflector :: underscore ( $name ) . '.php' ;
return $this -> createFile ( $filename , $out );
}
/**
2007-10-22 05:52:20 +00:00
* Assembles and writes a unit test file
2007-05-14 12:24:40 +00:00
*
2007-10-22 05:52:20 +00:00
* @ param string $className Model class name
* @ access private
2007-05-14 12:24:40 +00:00
*/
function __bakeTest ( $className ) {
$out = '<?php ' . " \n \n " ;
2007-11-17 17:37:23 +00:00
$out .= " App::import('Model', ' $className '); \n \n " ;
2007-06-04 19:53:22 +00:00
$out .= " class { $className } TestCase extends CakeTestCase { \n " ;
2007-05-14 12:24:40 +00:00
$out .= " \t var \$ TestObject = null; \n \n " ;
$out .= " \t function setUp() { \n \t \t \$ this->TestObject = new { $className } (); \n " ;
$out .= " \t } \n \n \t function tearDown() { \n \t \t unset( \$ this->TestObject); \n \t } \n " ;
$out .= " \n \t /* \n \t function testMe() { \n " ;
$out .= " \t \t \$ result = \$ this->TestObject->findAll(); \n " ;
$out .= " \t \t \$ expected = 1; \n " ;
$out .= " \t \t \$ this->assertEqual( \$ result, \$ expected); \n \t } \n \t */ \n } " ;
$out .= " \n ?> " ;
$path = MODEL_TESTS ;
2007-11-07 19:26:31 +00:00
$filename = Inflector :: underscore ( $className ) . '.test.php' ;
2007-05-14 12:24:40 +00:00
$this -> out ( " Baking unit test for $className ... " );
2007-11-07 19:34:06 +00:00
return $this -> createFile ( $path . $filename , $out );
2007-05-14 12:24:40 +00:00
}
/**
* outputs the a list of possible models or controllers from database
*
2007-10-22 05:52:20 +00:00
* @ param string $useDbConfig Database configuration name
* @ access public
2007-05-14 12:24:40 +00:00
*/
2007-06-03 23:56:33 +00:00
function listAll ( $useDbConfig = 'default' ) {
2007-05-14 12:24:40 +00:00
$db =& ConnectionManager :: getDataSource ( $useDbConfig );
$usePrefix = empty ( $db -> config [ 'prefix' ]) ? '' : $db -> config [ 'prefix' ];
if ( $usePrefix ) {
$tables = array ();
foreach ( $db -> listSources () as $table ) {
if ( ! strncmp ( $table , $usePrefix , strlen ( $usePrefix ))) {
$tables [] = substr ( $table , strlen ( $usePrefix ));
}
}
} else {
$tables = $db -> listSources ();
}
$this -> __tables = $tables ;
$this -> out ( 'Possible Models based on your current database:' );
$this -> _modelNames = array ();
$count = count ( $tables );
for ( $i = 0 ; $i < $count ; $i ++ ) {
$this -> _modelNames [] = $this -> _modelName ( $tables [ $i ]);
$this -> out ( $i + 1 . " . " . $this -> _modelNames [ $i ]);
}
}
2007-05-26 09:00:54 +00:00
/**
* Forces the user to specify the model he wants to bake , and returns the selected model name .
*
2007-10-22 05:52:20 +00:00
* @ return string the model name
* @ access public
2007-05-26 09:00:54 +00:00
*/
2007-06-03 23:56:33 +00:00
function getName ( $useDbConfig ) {
$this -> listAll ( $useDbConfig );
2007-05-26 09:00:54 +00:00
$enteredModel = '' ;
while ( $enteredModel == '' ) {
$enteredModel = $this -> in ( 'Enter a number from the list above, or type in the name of another model.' );
if ( $enteredModel == '' || intval ( $enteredModel ) > count ( $this -> _modelNames )) {
$this -> out ( 'Error:' );
$this -> out ( " The model name you supplied was empty, or the number \n you selected was not an option. Please try again. " );
$enteredModel = '' ;
}
}
if ( intval ( $enteredModel ) > 0 && intval ( $enteredModel ) <= count ( $this -> _modelNames )) {
$currentModelName = $this -> _modelNames [ intval ( $enteredModel ) - 1 ];
} else {
$currentModelName = $enteredModel ;
}
2007-05-29 06:30:08 +00:00
2007-05-26 09:00:54 +00:00
return $currentModelName ;
}
2007-06-08 08:44:34 +00:00
/**
* Displays help contents
*
2007-10-22 05:52:20 +00:00
* @ access public
2007-06-08 08:44:34 +00:00
*/
function help () {
$this -> hr ();
$this -> out ( " Usage: cake bake model " );
$this -> hr ();
$this -> out ( " this task is currently only run in interactive mode " );
$this -> out ( " " );
exit ();
}
2007-05-14 12:24:40 +00:00
}
?>