Yesterday I was checking how can we get table meta data information, like table name, columns names etc, and was surprised to see that how easy It is to have these information using Zend_Db component of Zend Framework.
Let us see how we can get table name and columns names using a simple example.
Consider you have a table with name “company” with following information.
• Id
• Company_name
• Address
• Phone
First create a model class as
class Company extends Zend_Db_Table
{
protected $_name= “company”;
}
Next in your controller/action.
Create an instance of your newly created class as
$company = new Company();
And now get meta data information as
$info = $company->info();
This will return an array of information.
You can check the information return by simply writing
Zend_Debug::dump($info);
The will show the entire information return by info().
If you want to have column names simple write
foreach($info["cols"] as $col) {
echo $col . “\t”;
}
This will show the name of the colums of your table.
That’s it. Enjoy.