CakePHP: how do I provide hard coded data for a model instead of a table to provide it?
cakephp, model
Solution
Update: Full multi-field records in a model without a database
After the author made his goal more clear, this should be the solution:
How to use models without database on CakePHP and have associations?
Don't get the title wrong, read it first, the answer describes exactly what would be the solution here as well.
Solution for single field, enum like data:
If you don't use a DB table and entries are limited it is always good to use constants because you can't do a typo without causing an error somewhere and UserLevel::USER is much more clear than a random 'user' string somewhere that could mean anything.
class UserLevel extends AppModel {
public $useTable = false;
const ADMIN = 'admin';
const USER = 'user';
/* ... */
public function getUserLevels() {
return [
UserLevel::ADMIN => __('Admin'),
/* ... */
];
}
}
Problem
I presently have a model called UserLevels which defines some user levels (user, premium user, admin, etc) and some properties of them (number, description, color, etc). I've decided I would rather hard code this, and be able to use the __() method on the names and descriptions. How do I go about providing data to a model so that it doesn't use a database? Is there a better way to approach this? Thank you.