Thursday 16 January 2014

Get table name from model name Yii

Step1: Create one controller named "CommonController.php" or use any existing one.
Step2: Create a static method in that controller named "getTableNameFromModel()" and add the following code in it.

<?php
public static function getTableNameFromModel($model) {
       return $model::model()->tableSchema->name;
}
?>

Step3: Call function from your model/view/controller by passing model name as a parameter.
<?php
    $model=new User;
    echo CommonController::getTableNameFromModel($model);
?>

It'll return table name of the corresponding model.
Hope it'll help you. Thanks.

Read Next: Get column names from model Yii

1 comment: