如何在Zend框架中使用查询获取字段名称

时间:2022-09-24 00:17:43

How to Get Field Name With Query in Zend Framework Test "Select * From Test1,Test2" how to get all field name in this query Zend Frame work cam do it?

如何在Zend框架测试中使用查询获取字段名称“选择*来自Test1,Test2”如何在此查询中获取所有字段名称Zend Frame工作凸轮吗?

2 个解决方案

#1


2  

Untested, but I believe the query is returned as an associative array (where the column name is the key), and so you can loop through the first record and pick up the column names, e.g.

未经测试,但我相信查询是作为关联数组返回的(其中列名是键),因此您可以遍历第一条记录并选取列名称,例如

$sql = 'Select * From Test1,Test2';

$result = $db->fetchAll($sql, 2);

foreach ($result[0] as $key => $value) {
 echo $key;
 ...
}

#2


1  

You could also issue $db->describeTable('Test1'), etc. before or after the query which would provide you with all the meta information you need. This query is pretty expensive though, so make sure to cache the response.

您还可以在查询之前或之后发出$ db-> describeTable('Test1')等,这将为您提供所需的所有元信息。此查询非常昂贵,因此请确保缓存响应。

Also, if you're using a model which extends Zend_Db_Table_Abstract, then you should have all this information already. In this case, all you need to do is access the protected property $_metadata.

此外,如果您正在使用扩展Zend_Db_Table_Abstract的模型,那么您应该已经拥有所有这些信息。在这种情况下,您需要做的就是访问受保护的属性$ _metadata。

HTH

#1


2  

Untested, but I believe the query is returned as an associative array (where the column name is the key), and so you can loop through the first record and pick up the column names, e.g.

未经测试,但我相信查询是作为关联数组返回的(其中列名是键),因此您可以遍历第一条记录并选取列名称,例如

$sql = 'Select * From Test1,Test2';

$result = $db->fetchAll($sql, 2);

foreach ($result[0] as $key => $value) {
 echo $key;
 ...
}

#2


1  

You could also issue $db->describeTable('Test1'), etc. before or after the query which would provide you with all the meta information you need. This query is pretty expensive though, so make sure to cache the response.

您还可以在查询之前或之后发出$ db-> describeTable('Test1')等,这将为您提供所需的所有元信息。此查询非常昂贵,因此请确保缓存响应。

Also, if you're using a model which extends Zend_Db_Table_Abstract, then you should have all this information already. In this case, all you need to do is access the protected property $_metadata.

此外,如果您正在使用扩展Zend_Db_Table_Abstract的模型,那么您应该已经拥有所有这些信息。在这种情况下,您需要做的就是访问受保护的属性$ _metadata。

HTH