62434

CakePHP - Model behaviours with variables

Question:

I want two of my models to inherit a property and a couple of methods. I know I could put the methods in a behaviour but I'm not sure about the property. Can a behaviour have variables that are inherited by the model?

Or should I just create another model that extends AppModel, and have the two models extend that one?

Answer1:

As long as both methods and variables are made public you can put your code into a behavior.

In general I would go for a behavior, I guess. You could then create some defaults in it and override them later from the Models if necessary. Another benefit is that you keep your code clean: callbacks (beforeSave, etc.) are ran by the models having the behavior attached and do not clutter your model code like a parent model would (you would need e.g. a parent::beforeSave() when you add callbacks to your models and still want the callbacks to run from the parent model class.

If you provide more information, maybe we can give you a more specific answer.

Recommend

  • GetActiveObject() vs. GetObject() — MK_E_UNAVAILABLE Error
  • Problem with altering model attributes in controller
  • CakePHP Auth Component Not logging in when using $this->Auth->login();
  • Jasmine 2.0 async beforeEach not waiting for async to finish
  • Scala: Function returning an unknown type
  • LiveData is abstract android
  • Compare struct to a constant in C
  • Wrong labels when plotting a time series pandas dataframe with matplotlib
  • Why use database factory in asp.net mvc?
  • Android application: how to use the camera and grab the image bytes?
  • Content-Length header not returned from Pylons response
  • Sencha Touch 2.0 Controller refs attribute not working?
  • Play WS (2.2.1): post/put large request
  • Highlight and Bold text in JTextPane
  • Asynchronous UI Testing in Xcode With Swift
  • How to access EntityManager inside Entity class in EJB3
  • how to do an event when i swipe from fragment to the other
  • QLineEdit password safety
  • vba code to select only visible cells in specific column except heading
  • How can I use Kendo UI with Razor?
  • ActionScript 2 vs ActionScript 3 performance
  • Akka Routing: Reply's send to router ends up as dead letters
  • Cannot Parse HTML Data Using Android / JSOUP
  • Comma separated Values
  • Error creating VM instance in Google Compute Engine
  • Acquiring multiple attributes from .xml file in c#
  • Hits per day in Google Big Query
  • JTable with a ScrollPane misbehaving
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • how does django model after text[] in postgresql [duplicate]
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?