@@ -179,6 +179,14 @@ class Doctrine_Import_Builder extends Doctrine_Builder
179
179
*/
180
180
protected $ _phpDocEmail = '##EMAIL## ' ;
181
181
182
+
183
+ /**
184
+ * Contains the actAs columns after running buildSetUp
185
+ *
186
+ * @var array
187
+ */
188
+ private $ _actAsColumns = array ();
189
+
182
190
/**
183
191
* _tpl
184
192
*
@@ -396,9 +404,7 @@ public function buildTableDefinition(array $definition)
396
404
/**
397
405
* buildSetUp
398
406
*
399
- * @param array $options
400
- * @param array $columns
401
- * @param array $relations
407
+ * @param array $definition
402
408
* @return string
403
409
*/
404
410
public function buildSetUp (array $ definition )
@@ -857,21 +863,33 @@ public function buildPhpDocs(array $definition)
857
863
return $ ret ;
858
864
}
859
865
866
+ /**
867
+ * find class matching $name
868
+ *
869
+ * @param $name
870
+ * @return string
871
+ */
872
+ private function findTemplateClassMatchingName ($ name )
873
+ {
874
+ $ classname = $ name ;
875
+ if (class_exists ("Doctrine_Template_ $ name " , true )) {
876
+ $ classname = "Doctrine_Template_ $ name " ;
877
+ }
878
+
879
+ return $ classname ;
880
+ }
881
+
860
882
/**
861
883
* emit a behavior assign
862
884
*
863
885
* @param int $level
864
886
* @param string $name
865
887
* @param string $option
888
+ * @param string $classname
866
889
* @return string assignation code
867
890
*/
868
- private function emitAssign ($ level , $ name , $ option )
891
+ private function emitAssign ($ level , $ name , $ option, $ classname )
869
892
{
870
- // find class matching $name
871
- $ classname = $ name ;
872
- if (class_exists ("Doctrine_Template_ $ name " , true )) {
873
- $ classname = "Doctrine_Template_ $ name " ;
874
- }
875
893
return " \$" . strtolower ($ name ) . "$ level = new $ classname( $ option); " . PHP_EOL ;
876
894
}
877
895
@@ -943,6 +961,7 @@ private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emi
943
961
$ currentParent = $ parent ;
944
962
if (is_array ($ actAs )) {
945
963
foreach ($ actAs as $ template => $ options ) {
964
+ $ className = $ this ->findTemplateClassMatchingName ($ template );
946
965
if ($ template == 'actAs ' ) {
947
966
// found another actAs
948
967
$ build .= $ this ->innerBuildActAs ($ options , $ level + 1 , $ parent , $ emittedActAs );
@@ -959,7 +978,8 @@ private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emi
959
978
}
960
979
961
980
$ optionPHP = $ this ->varExport ($ realOptions );
962
- $ build .= $ this ->emitAssign ($ level , $ template , $ optionPHP );
981
+ $ build .= $ this ->emitAssign ($ level , $ template , $ optionPHP , $ className );
982
+ $ this ->determineActAsColumns ($ className , $ realOptions );
963
983
if ($ level == 0 ) {
964
984
$ emittedActAs [] = $ this ->emitActAs ($ level , $ template );
965
985
} else {
@@ -969,7 +989,8 @@ private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emi
969
989
$ parent = $ template ;
970
990
$ build .= $ this ->innerBuildActAs ($ leftActAs , $ level , $ template , $ emittedActAs );
971
991
} else {
972
- $ build .= $ this ->emitAssign ($ level , $ template , null );
992
+ $ build .= $ this ->emitAssign ($ level , $ template , null , $ className );
993
+ $ this ->determineActAsColumns ($ className , array ($ options ));
973
994
if ($ level == 0 ) {
974
995
$ emittedActAs [] = $ this ->emitActAs ($ level , $ template );
975
996
} else {
@@ -979,7 +1000,9 @@ private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emi
979
1000
}
980
1001
}
981
1002
} else {
982
- $ build .= $ this ->emitAssign ($ level , $ actAs , null );
1003
+ $ className = $ this ->findTemplateClassMatchingName ($ actAs );
1004
+ $ build .= $ this ->emitAssign ($ level , $ actAs , null , $ className );
1005
+ $ this ->determineActAsColumns ($ className , array ());
983
1006
if ($ level == 0 ) {
984
1007
$ emittedActAs [] = $ this ->emitActAs ($ level , $ actAs );
985
1008
} else {
@@ -990,6 +1013,55 @@ private function innerBuildActAs($actAs, $level = 0, $parent = null, array &$emi
990
1013
return $ build ;
991
1014
}
992
1015
1016
+ /**
1017
+ * Adds the columns of the used actAs behaviors to the comment block.
1018
+ *
1019
+ * @param string $className
1020
+ * @param array $instanceOptions
1021
+ *
1022
+ * @throws Doctrine_Import_Builder_Exception
1023
+ */
1024
+ private function determineActAsColumns ($ className , $ instanceOptions )
1025
+ {
1026
+ // No class specified or class does not exist.
1027
+ if (!$ className || !class_exists ($ className )) {
1028
+ return ;
1029
+ }
1030
+
1031
+ $ actAsInstance = new $ className ($ instanceOptions );
1032
+ $ options = $ actAsInstance ->getOptions ();
1033
+
1034
+ if (count ($ options ) == 0 ) {
1035
+ return ;
1036
+ }
1037
+
1038
+ // Some behaviors do not contain an array of columns, e.g. SoftDelete.
1039
+ if (!is_array (reset ($ options ))) {
1040
+ $ options = array ($ options );
1041
+ }
1042
+
1043
+ foreach ($ options as $ name => $ column ) {
1044
+ if (!is_array ($ column ) || !array_key_exists ('name ' , $ column ) || !array_key_exists ('type ' , $ column )) {
1045
+ // 'name' or 'type' not found. Unfortunately there is no logger. What is the best way to abort here?
1046
+ continue ;
1047
+ }
1048
+
1049
+ if (array_key_exists ('disabled ' , $ column ) && $ column ['disabled ' ]) {
1050
+ // Column has been disabled.
1051
+ continue ;
1052
+ }
1053
+
1054
+ // Add field, if it does not exist already.
1055
+ if (
1056
+ !array_key_exists ($ name , $ this ->_actAsColumns )
1057
+ && !array_key_exists ($ column ['name ' ], $ this ->_actAsColumns )
1058
+ ) {
1059
+ $ this ->_actAsColumns [$ name ] = $ column ;
1060
+ }
1061
+ }
1062
+ }
1063
+
1064
+
993
1065
/**
994
1066
* Build php code for adding record listeners
995
1067
*
@@ -1122,6 +1194,8 @@ public function buildDefinition(array $definition)
1122
1194
$ className = $ definition ['className ' ];
1123
1195
$ extends = isset ($ definition ['inheritance ' ]['extends ' ]) ? $ definition ['inheritance ' ]['extends ' ]:$ this ->_baseClassName ;
1124
1196
1197
+ // Clear actAsColumns
1198
+ $ this ->_actAsColumns = array ();
1125
1199
if ( ! (isset ($ definition ['no_definition ' ]) && $ definition ['no_definition ' ] === true )) {
1126
1200
$ tableDefinitionCode = $ this ->buildTableDefinition ($ definition );
1127
1201
$ setUpCode = $ this ->buildSetUp ($ definition );
@@ -1136,6 +1210,7 @@ public function buildDefinition(array $definition)
1136
1210
1137
1211
$ setUpCode .= $ this ->buildToString ($ definition );
1138
1212
1213
+ $ definition ['columns ' ] = array_merge ($ definition ['columns ' ], $ this ->_actAsColumns );
1139
1214
$ docs = PHP_EOL . $ this ->buildPhpDocs ($ definition );
1140
1215
1141
1216
$ content = sprintf (self ::$ _tpl , $ docs , $ abstract ,
0 commit comments