Reputation: 4944
CakePHP: 2.3.5
class Table extends AppModel {
const STATUS_FREE = 0;
const STATUS_BUSY = 1;
const STATUS_INCHECK = 2;
const STATUS_LINK = 3;
const STATUS_CLEAN = 4;
const STATUS_CASHIER = 9;
I'm in TablesController, and I need access this constants. The only way I founded for do that, is:
$this->loadModel('Table');
$free = Table::STATUS_FREE;
I try too:
self::STATUS_FREE
Why I need use $this->loadModel
, if I in TablesController?
Upvotes: 0
Views: 83
Reputation: 21743
You dont need loadModel. You just need the class included. You can also achieve that via App::uses() - which kind of works as a lazy loaded require().
Just include the classes you need the constants from above your main class. Always.
App::uses('Table', 'Model');
App::uses('OtherModelWithContantsYouNeed', 'Model');
class TablesController extends AppController {}
Now you can use your constants anywhere in your controller code as well as all its views!
This is also how I do it for my class constants in my enums ( http://www.dereuromark.de/2010/06/24/static-enums-or-semihardcoded-attributes/ ). There is also explained in more detail what is going on.
Upvotes: 1