how can I access any table from database in my model?
For example, I have Indexcontroller and code inside it:
$results = $this->Index->query("SELECT COUNT(*) FROM my_own_table");
Error: Database table indices for model Index was not found.
So, as I understand, I can access only table with naming related to model/controller name. But what to do if I can’t modify the table naming and I want to access it’s data?
You’re not limited to using a model that’s directly associated with your controller (this is just default behaviour); you can use any model.
To achieve what you want, create a new model for this table, eg.
MyOwnTable, and in your controller, you can add this property to the class:Now you can access
MyOwnTableusing CakePHP’s built in ActiveRecord functionality:If you have other tables you want to access, simply create models for those and add them to the
$usesproperty. (You can also use$this->loadModel('Model')inside the action if you prefer).If you have a table name that isn’t very readable (eg.
my_tb_own_1_xor some such), you can call the model class something human readable (eg.MyTable), and add the$useTableproperty to the model:See the CakePHP manual for more info on how to change default model and controller behaviour:
1.3 – Model Attributes
2.0 – Model Attributes
1.3 – Controller Attributes
2.0 – Controller Attributes