2

I would like to implement an autoslug-function for two models in CakePHP3 and use the beforeSave-callback for that which works fine. However the slug structure is a bit different than in Inflector::slug, so I wrote a small function for the different structure which finally leads to my question

In Cake2 I would have placed this helper function in AppModel which is not existing anymore. What's the best way to do that now? A behaviour (which seems a bit biggish for 2 lines of code) or class AppTable extends Table or ..?

harpax
  • 5,986
  • 5
  • 35
  • 49
  • 2
    Well, both would do it, and reasons for choosing one over the other are mostly personal preferences and voodoo wisdoms, so the question tends to be a little OT on SO. Personally I'd probably go with a behavior. – ndm Oct 17 '14 at 09:38
  • 1
    As you would have done in any cake version prior to 3.0 :) See https://github.com/dereuromark/cakephp-tools/blob/cake3/src/Model/Behavior/SluggedBehavior.php for a 3.0 version by the way. – mark Oct 17 '14 at 21:59
  • Thanks all for answers.. The slug thing was just an example.. I do somewhat miss AppModel, but then again a behavior makes more sense and is probably more lightweight.. – harpax Oct 18 '14 at 16:55
  • @mark nice indeed! :) – harpax Oct 18 '14 at 17:15

1 Answers1

5

Make it a behavior and use it where needed instead of putting it in a super model class. If you put that into a plugin and repository you can simply add your plugin as package via composer for every app that needs this plugin.

nIcO
  • 5,001
  • 24
  • 36
floriank
  • 25,546
  • 9
  • 42
  • 66