3

Does anyone knows how to get table name from model name? Also I want to get all column names and its types of that model/table name. Is it possible to get such details of given model name?

Thanks.

1 Answer 1

9

Table Name

To get the table, see

$this->Model->table 

Or check the model for the class variable $useTable. If that's undefined, then you can infer it from the name of the model:

$tableName = Inflector::tableize($this->Model->alias); 

See the Inflector documentation for similarly useful methods.

Columns

Take a look at the schema method of the Model class. For example:

var_dump($this->Model->schema()); 
Sign up to request clarification or add additional context in comments.

3 Comments

I think for table name, you can just use $this->Model->table
So you can! nice :) Included in answer, left Inflector stuff in because it's still handy to know
In order to include the table prefix ready to be used for a raw query you will have to call the dbo source model: $this->getDataSource()->fullTableName($this)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.