Question

Users have many Instruments, but Instruments are static so I'd like to use them by referring to a defined constant array rather than retrieving them from the database. This array needs to be accessible by Views and Controllers.

I also feel that stuffing a bunch of random unrelated constants in bootstrap is mess.

What is the best way to do this - where should I put this array?

Was it helpful?

Solution

If you want to use some kind of enum I guess you are looking for that: http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/

public static function instruments {}

But in your case Instruments shouldnt be static but a real table with data (Instrument belongsTo User - e.g. multiple rows with the same user_id). Enums only work the other way around (on belongsTo relations).

Or is there a specific reason it has to be static? The main problem with your question is how the data can be properly stored. What instruments belong to what users?

If you really want to store such an array list in a single column of the user (which I don't recommend in your case), you could use a bitmask: http://www.dereuromark.de/2012/02/26/bitmasked-using-bitmasks-in-cakephp/

OTHER TIPS

Use the Array Datasource. It lets you define static arrays of data and still use associations.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top