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->table
indeed contains the table n开发者_运维问答ame. 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.
You're looking for the Inflector
class.
Inflector::tableize($this->name)
(tableize
calls two Inflector methods to generate the table name: underscore() and pluralize())
Edit:
According to the source code, $this->table
should contain the name of the table that CakePHP will use for the model, but in my experience this isn't always set. I'm not sure why.
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.
CakePHP's Inflector
function get_table_name() {
$tbl_name = Inflector::pluralize($this->name);
}
OR the tableize
method
function get_table_name() {
$tbl_name = Inflector::tableize($this->name);
}
Edit
This also addresses the apparent "ghost" issue with $this->table
in the Model.
Digging around in the __construct
for Model
I discovered two things:
Cake uses
Inflector::tableize()
to get the table name. This alone is enough to warrant usingtableize
overpluralize
. You'll get consistent results.$this->table
is not set by theModel::__construct()
unless$this->useTable === false
AND$this->table === false
.
It appears that if you know you haven't set $this->useTable
to false
you should be able to use this over $this->table
. Admittedly though I only briefly scanned the source and I haven't really dug deep enough to say why $this->table
isn't working sometimes.
To get the full table name for a model you have to take the table prefix into account.
$table = empty($this->table) ? Inflector::tableize($this->name) : $this->table;
$fullTableName = $this->tablePrefix . $table;
I used to use inflector to get the table name from model's name
$tableName = Inflector::pluralize(Inflector::underscore($model));
but this is not really universal, using useTable
looks better, by default it will contain table's name by convention, and if you have a table that does not match the conventions, then you should manually specify it by useTable
. So, in both cases the result will be correct
$this->User->useTable
精彩评论