php - How to get all table and column names from database in Yii Framework -
i working on module want dynamic dependent dropdown table , column name functionality.
ex. fetch table names , display in dropdown fields , after selection of particular table want display column name again in dropdown field.
the issues are:
1) how fetch table name db?
2) , how fetch column name table?
i tried few articles , forums http://www.yiiframework.com/forum/index.php/topic/5920-how-can-i-get-the-actual-full-table-name/ not working.
any appreciated.
thanks
it's quite simple, using instance of cdbtableschema
class:
echo 'name: ', $tbl->name, ' (raw: ', $tbl->rawname, ')'; echo 'fields: ', implode(', ', $tbl->columnnames);
and on. there's lot of methods , properties this
all tables, use cdbschema
class docs here.
the cdbschema
class has both public tablenames
property (an array of tbl namnes) , tables
property, containing meta-data. that's all, really.
to these instances, following code should suffice:
$connection = yii::app()->db;//get connection $dbschema = $connection->schema; //or $connection->getschema(); $tables = $dbschema->gettables();//returns array of tbl schema's foreach($tables $tbl) { echo $tbl->rawname, ':<br/>', implode(', ', $tbl->columnnames), '<br/>'; }
to create dropdown list, use standard chtml
object:
$options = array(); foreach($tables $tbl) {//for example $options[$tbl->rawname] = $tbl->name; } $dropdown = chtml::dropdownlist('tables',$tables[0]->rawname, $options);
please, spend time reading manual, it's there. haven't used yii
extensively, well, haven't used @ all, honest, yet took me 5 minutes work out. @ source! each , every method/class/property has link exact line in corresponding file!
before asking others figure out you, put in effort.
Comments
Post a Comment