In my CakePHP application, I have a model like this:
class Duck extends AppModel {
var $name = 'Duck';
function get_table_name() {
$tbl_name = //compute default table name for this model
}
}
I would like to write the function get_table_name() that outputs the default table name for the model. For the example above, it should output ducks.
EDIT:
Several people have pointed out the use of $this->table.
I did small testing and found out the following:
- In the question as I have put above,
$this->tableindeed contains the table name. -
However, actually, my code looked more like this:
class Duck extends Bird { var $name = 'Duck'; function get_table_name(){ $tbl_name = //comput default table name for this model } } class Bird extends AppModel { }In this case $this->table is empty string.
I went with this approach because I wanted to share some code between two of my models. Looks like this is not a good way to share code between models which need some common functionality.
To get the name of the table that the model is currently using, you can use:
$this->table. If you don’t manually change the model’s table conventions, this may be the most useful in the case of CakePHP ever changing its conventions to use table names using something other than Inflector.