首页 > 解决方案 > find() 和 fetchAll() 在 Zend Framework 1 中不起作用

问题描述

有人可以建议我的代码有什么问题吗?虽然insert()、update() 和 delete()工作得非常好。这是我在 Zend 框架上的第一个项目,过去两天我一直在搞乱它,但没有弄清楚它到底出了什么问题。我正在使用 Zend 框架 1。

这是我的函数体。

public function getMenuCategoryById(Application_Model_MenuCategories $MenuCategory) {
        $where = array(
            'mc_id' => (int) $MenuCategory->__get('mc_id')
        );
        $result = $this->_db_table->find($where);
        echo '<pre';
        print_r($result);
        die();
        if (count($result) == 0) {
            return false;
        }
        $row = $result->current();
        $menuCategory = new Application_Model_MenuCategories($row);
        return $menuCategory;
    }

这不是返回行。相反,它给Zend_Db_Table_Rowset bject如下:

Zend_Db_Table_Rowset Object (
     [_data:protected] => Array
         (
         )

     [_table:protected] => Application_Model_DbTable_MenuCategories Object
         (
             [_name:protected] => menu_categories
             [_definition:protected] => 
             [_definitionConfigName:protected] => 
             [_db:protected] => Zend_Db_Adapter_Pdo_Mysql Object
                 (
                     [_pdoType:protected] => mysql
                     [_numericDataTypes:protected] => Array
                         (
                             [0] => 0
                             [1] => 1
                             [2] => 2
                             [INT] => 0
                             [INTEGER] => 0
                             [MEDIUMINT] => 0
                             [SMALLINT] => 0
                             [TINYINT] => 0
                             [BIGINT] => 1
                             [SERIAL] => 1
                             [DEC] => 2
                             [DECIMAL] => 2
                             [DOUBLE] => 2
                             [DOUBLE PRECISION] => 2
                             [FIXED] => 2
                             [FLOAT] => 2
                         )

                     [_defaultStmtClass:protected] => Zend_Db_Statement_Pdo
                     [_config:protected] => Array
                         (
                             [dbname] => test_db
                             [host] => localhost
                             [username] => root
                             [password] => Password@123
                             [charset] => 
                             [persistent] => 
                             [options] => Array
                                 (
                                     [caseFolding] => 0
                                     [autoQuoteIdentifiers] => 1
                                     [fetchMode] => 2
                                 )

                             [driver_options] => Array
                                 (
                                 )

                         )

                     [_fetchMode:protected] => 2
                     [_profiler:protected] => Zend_Db_Profiler Object
                         (
                             [_queryProfiles:protected] => Array
                                 (
                                 )

                             [_enabled:protected] => 
                             [_filterElapsedSecs:protected] => 
                             [_filterTypes:protected] => 
                         )

                     [_defaultProfilerClass:protected] => Zend_Db_Profiler
                     [_connection:protected] => PDO Object
                         (
                         )

                     [_caseFolding:protected] => 0
                     [_autoQuoteIdentifiers:protected] => 1
                     [_allowSerialization:protected] => 1
                     [_autoReconnectOnUnserialize:protected] => 
                 )

             [_schema:protected] => 
             [_cols:protected] => Array
                 (
                     [0] => mc_id
                     [1] => category_name
                 )

             [_primary:protected] => Array
                 (
                     [1] => mc_id
                 )

             [_identity:protected] => 1
             [_sequence:protected] => 1
             [_metadata:protected] => Array
                 (
                     [mc_id] => Array
                         (
                             [SCHEMA_NAME] => 
                             [TABLE_NAME] => menu_categories
                             [COLUMN_NAME] => mc_id
                             [COLUMN_POSITION] => 1
                             [DATA_TYPE] => int
                             [DEFAULT] => 
                             [NULLABLE] => 
                             [LENGTH] => 
                             [SCALE] => 
                             [PRECISION] => 
                             [UNSIGNED] => 
                             [PRIMARY] => 1
                             [PRIMARY_POSITION] => 1
                             [IDENTITY] => 1
                         )

                     [category_name] => Array
                         (
                             [SCHEMA_NAME] => 
                             [TABLE_NAME] => menu_categories
                             [COLUMN_NAME] => category_name
                             [COLUMN_POSITION] => 2
                             [DATA_TYPE] => varchar
                             [DEFAULT] => 
                             [NULLABLE] => 
                             [LENGTH] => 255
                             [SCALE] => 
                             [PRECISION] => 
                             [UNSIGNED] => 
                             [PRIMARY] => 
                             [PRIMARY_POSITION] => 
                             [IDENTITY] => 
                         )

                 )

             [_metadataCache:protected] => 
             [_metadataCacheInClass:protected] => 1
             [_rowClass:protected] => Zend_Db_Table_Row
             [_rowsetClass:protected] => Zend_Db_Table_Rowset
             [_referenceMap:protected] => Array
                 (
                 )

             [_dependentTables:protected] => Array
                 (
                 )

             [_defaultSource:protected] => defaultNone
             [_defaultValues:protected] => Array
                 (
                 )

         )

     [_connected:protected] => 1
     [_tableClass:protected] => Application_Model_DbTable_MenuCategories
     [_rowClass:protected] => Zend_Db_Table_Row
     [_pointer:protected] => 0
     [_count:protected] => 0
     [_rows:protected] => Array
         (
         )

     [_stored:protected] => 1
     [_readOnly:protected] =>  )

标签: phpzend-framework

解决方案


一切正常 - fetchAll() 返回 Zend_Db_Table_Rowset 对象。你可以直接迭代它:

foreach($result as $row) {
    // $row is Zend_Db_Table_Row object
}

或转换为数组:

$arrayResult = $result->toArray();

但是,您的转储$result显示您的查询与表中的任何行都不匹配 - 检查您的$where条件。


推荐阅读