获取yii中的所有表名 [英] Get all table names in yii

查看:251
本文介绍了获取yii中的所有表名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在yii中获取所有表名?

How to get all table names in yii?

mySQL中的sql查询为SHOW TABLES. 我试过了:

The sql query in mySQL is SHOW TABLES. I tried:

$sql = 'SHOW TABLES';
$tables = Yii::app()->db
         ->createCommand($sql)
         ->queryAll();
print_r($tables);

它抛出一个错误:

CDbCommand failed to execute the SQL statement:
CDbCommand failed to prepare the SQL statement:
SQLSTATE[HY000]: General error: 1 near "SHOW": syntax error.
The SQL statement executed was: SHOW TABLES 

推荐答案

尝试以下方法:

$connection = Yii::app()->db;//get connection
$dbSchema = $connection->schema;
//or $connection->getSchema();
$tables = $dbSchema->getTables();//returns array of tbl schema's
foreach($tables as $tbl)
{
    echo $tbl->rawName, ':<br/>', implode(', ', $tbl->columnNames), '<br/>';
}

引用:: 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆