Zend_Db - 如何使用来自 Zend_Db_Table_Select 的结果
Zend_Db - How to use results from Zend_Db_Table_Select
我被迫在我当前的项目中使用 Zend-Framework,但我被困在一个特定的点上。不幸的是,该框架的文档很糟糕,目前对我没有真正的帮助。解决方案可能非常简单,但我自己想不出来。
我只是想从我的数据库中获取一些东西,我试图用这段代码来实现:
BS_Model_Subshops.php:
class BS_Model_Subshops extends BS_Lib_Model
{
protected $_name = 'shops';
protected $_username = '';
protected $_password = '';
protected $_primary = 'id';
}
LoginController.php:
public function authAction() {
$shop= new BS_Model_Subshops();
$id= filter_var($this->getRequest()->getPost('id'));
$db_content= $shop->select()->where('id= '.$id);
Zend_Debug::dump($db_content);
... more code ...
}
这给了我以下对象(警告:巨大的对象传入):
object(Zend_Db_Table_Select)#85 (7) {
["_info":protected] => array(10) {
["schema"] => NULL
["name"] => string(5) "shops"
["cols"] => array(8) {
[0] => string(2) "id"
[1] => string(4) "name"
[2] => string(8) "css_path"
[3] => string(3) "mmz"
[4] => string(17) "order_information"
[5] => string(11) "code_suffix"
}
["primary"] => array(1) {
[1] => string(2) "id"
}
["metadata"] => array(8) {
["id"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(2) "id"
["COLUMN_POSITION"] => int(1)
["DATA_TYPE"] => string(3) "int"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => NULL
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => bool(true)
["PRIMARY"] => bool(true)
["PRIMARY_POSITION"] => int(1)
["IDENTITY"] => bool(true)
}
["name"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(4) "name"
["COLUMN_POSITION"] => int(2)
["DATA_TYPE"] => string(7) "varchar"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => string(2) "60"
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => NULL
["PRIMARY"] => bool(false)
["PRIMARY_POSITION"] => NULL
["IDENTITY"] => bool(false)
}
["css_path"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(8) "css_path"
["COLUMN_POSITION"] => int(3)
["DATA_TYPE"] => string(7) "varchar"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => string(3) "100"
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => NULL
["PRIMARY"] => bool(false)
["PRIMARY_POSITION"] => NULL
["IDENTITY"] => bool(false)
}
["mmz"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(3) "mmz"
["COLUMN_POSITION"] => int(4)
["DATA_TYPE"] => string(14) "float unsigned"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => NULL
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => bool(true)
["PRIMARY"] => bool(false)
["PRIMARY_POSITION"] => NULL
["IDENTITY"] => bool(false)
}
["order_information"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(17) "order_information"
["COLUMN_POSITION"] => int(5)
["DATA_TYPE"] => string(4) "text"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => NULL
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => NULL
["PRIMARY"] => bool(false)
["PRIMARY_POSITION"] => NULL
["IDENTITY"] => bool(false)
}
["code_suffix"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(11) "code_suffix"
["COLUMN_POSITION"] => int(6)
["DATA_TYPE"] => string(7) "varchar"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => string(1) "6"
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => NULL
["PRIMARY"] => bool(false)
["PRIMARY_POSITION"] => NULL
["IDENTITY"] => bool(false)
}
... more unimportant stuff here ...
现在,例如,我只想要名称或 css 路径。我怎样才能得到其中之一?我对 Zend 不是很熟悉,尽管我以前使用过 laravel,但我在理解它方面遇到了很大的问题,因为它太复杂了,而且没有很好的示例文档。
Web 服务器使用 PHP 5.4 和 Zend-Framework 1.9(如果重要的话)。
您可以使用 fetchAll
获取名称或 css 路径
试试下面的代码。
public function authAction() {
$shop= new BS_Model_Subshops();
$id= filter_var($this->getRequest()->getPost('id'));
$db_content = $shop->select()->where('id= '.$id);
$result = $db_content->query()->fetchAll();
foreach($result as $key=>$value){
echo "Name : ". $value->name;
echo "</br>";
echo "Css Path : ". $value->css_path;
}
Zend_Debug::dump($db_content);
... more code ...
}
您还可以使用行集对象的 toArray
方法将行集作为数组检索。
$result = $db_content->query()->fetchAll();
$row_array = $result->toArray();
此致,
我被迫在我当前的项目中使用 Zend-Framework,但我被困在一个特定的点上。不幸的是,该框架的文档很糟糕,目前对我没有真正的帮助。解决方案可能非常简单,但我自己想不出来。
我只是想从我的数据库中获取一些东西,我试图用这段代码来实现:
BS_Model_Subshops.php:
class BS_Model_Subshops extends BS_Lib_Model
{
protected $_name = 'shops';
protected $_username = '';
protected $_password = '';
protected $_primary = 'id';
}
LoginController.php:
public function authAction() {
$shop= new BS_Model_Subshops();
$id= filter_var($this->getRequest()->getPost('id'));
$db_content= $shop->select()->where('id= '.$id);
Zend_Debug::dump($db_content);
... more code ...
}
这给了我以下对象(警告:巨大的对象传入):
object(Zend_Db_Table_Select)#85 (7) {
["_info":protected] => array(10) {
["schema"] => NULL
["name"] => string(5) "shops"
["cols"] => array(8) {
[0] => string(2) "id"
[1] => string(4) "name"
[2] => string(8) "css_path"
[3] => string(3) "mmz"
[4] => string(17) "order_information"
[5] => string(11) "code_suffix"
}
["primary"] => array(1) {
[1] => string(2) "id"
}
["metadata"] => array(8) {
["id"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(2) "id"
["COLUMN_POSITION"] => int(1)
["DATA_TYPE"] => string(3) "int"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => NULL
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => bool(true)
["PRIMARY"] => bool(true)
["PRIMARY_POSITION"] => int(1)
["IDENTITY"] => bool(true)
}
["name"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(4) "name"
["COLUMN_POSITION"] => int(2)
["DATA_TYPE"] => string(7) "varchar"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => string(2) "60"
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => NULL
["PRIMARY"] => bool(false)
["PRIMARY_POSITION"] => NULL
["IDENTITY"] => bool(false)
}
["css_path"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(8) "css_path"
["COLUMN_POSITION"] => int(3)
["DATA_TYPE"] => string(7) "varchar"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => string(3) "100"
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => NULL
["PRIMARY"] => bool(false)
["PRIMARY_POSITION"] => NULL
["IDENTITY"] => bool(false)
}
["mmz"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(3) "mmz"
["COLUMN_POSITION"] => int(4)
["DATA_TYPE"] => string(14) "float unsigned"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => NULL
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => bool(true)
["PRIMARY"] => bool(false)
["PRIMARY_POSITION"] => NULL
["IDENTITY"] => bool(false)
}
["order_information"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(17) "order_information"
["COLUMN_POSITION"] => int(5)
["DATA_TYPE"] => string(4) "text"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => NULL
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => NULL
["PRIMARY"] => bool(false)
["PRIMARY_POSITION"] => NULL
["IDENTITY"] => bool(false)
}
["code_suffix"] => array(14) {
["SCHEMA_NAME"] => NULL
["TABLE_NAME"] => string(5) "shops"
["COLUMN_NAME"] => string(11) "code_suffix"
["COLUMN_POSITION"] => int(6)
["DATA_TYPE"] => string(7) "varchar"
["DEFAULT"] => NULL
["NULLABLE"] => bool(false)
["LENGTH"] => string(1) "6"
["SCALE"] => NULL
["PRECISION"] => NULL
["UNSIGNED"] => NULL
["PRIMARY"] => bool(false)
["PRIMARY_POSITION"] => NULL
["IDENTITY"] => bool(false)
}
... more unimportant stuff here ...
现在,例如,我只想要名称或 css 路径。我怎样才能得到其中之一?我对 Zend 不是很熟悉,尽管我以前使用过 laravel,但我在理解它方面遇到了很大的问题,因为它太复杂了,而且没有很好的示例文档。
Web 服务器使用 PHP 5.4 和 Zend-Framework 1.9(如果重要的话)。
您可以使用 fetchAll
获取名称或 css 路径
试试下面的代码。
public function authAction() {
$shop= new BS_Model_Subshops();
$id= filter_var($this->getRequest()->getPost('id'));
$db_content = $shop->select()->where('id= '.$id);
$result = $db_content->query()->fetchAll();
foreach($result as $key=>$value){
echo "Name : ". $value->name;
echo "</br>";
echo "Css Path : ". $value->css_path;
}
Zend_Debug::dump($db_content);
... more code ...
}
您还可以使用行集对象的 toArray
方法将行集作为数组检索。
$result = $db_content->query()->fetchAll();
$row_array = $result->toArray();
此致,