@@ -40,31 +40,13 @@ class TrashBehavior extends Behavior
4040 ];
4141
4242 /**
43- * Constructor
43+ * Initialize the behavior.
4444 *
45- * Merges config with the default and store in the config property
46- *
47- * @param \Cake\ORM\Table $table The table this behavior is attached to.
4845 * @param array $config The config for this behavior.
46+ * @return void
4947 */
50- public function __construct ( Table $ table , array $ config = [] )
48+ public function initialize ( array $ config )
5149 {
52- $ columns = $ table ->schema ()->columns ();
53- foreach (['deleted ' , 'trashed ' ] as $ name ) {
54- if (in_array ($ name , $ columns , true )) {
55- $ this ->_defaultConfig ['field ' ] = $ name ;
56- break ;
57- }
58- }
59-
60- if (empty ($ this ->_defaultConfig ['field ' ]) &&
61- $ field = Configure::read ('Muffin/Trash.field ' )
62- ) {
63- $ this ->_defaultConfig ['field ' ] = $ field ;
64- }
65-
66- parent ::__construct ($ table , $ config );
67-
6850 if (!empty ($ config ['events ' ])) {
6951 $ this ->config ('events ' , $ config ['events ' ], false );
7052 }
@@ -306,6 +288,26 @@ public function getTrashField($aliased = true)
306288 {
307289 $ field = $ this ->config ('field ' );
308290
291+ if (empty ($ field )) {
292+ $ columns = $ this ->_table ->schema ()->columns ();
293+ foreach (['deleted ' , 'trashed ' ] as $ name ) {
294+ if (in_array ($ name , $ columns , true )) {
295+ $ field = $ name ;
296+ break ;
297+ }
298+ }
299+
300+ if (empty ($ field )) {
301+ $ field = Configure::read ('Muffin/Trash.field ' );
302+ }
303+
304+ if (empty ($ field )) {
305+ throw new RuntimeException ('TrashBehavior: "field" config needs to be provided. ' );
306+ }
307+
308+ $ this ->config ('field ' , $ field );
309+ }
310+
309311 if ($ aliased ) {
310312 return $ this ->_table ->aliasField ($ field );
311313 }
0 commit comments