Object of class Zend_Db_Table_Row 无法转换为字符串
Object of class Zend_Db_Table_Row could not be converted to string
当我 运行 以下内容时,我在标题中收到错误消息:
$year = date('Y');
$month = date('n');
$day = date('j') - 3;
$week = date('W') - 1;
$select = "SELECT IF( station_name = '', 'Unknown', station_name ) station_name, station_name as db_name, sum(case when year(print_date) = $year then print_pages end) as yearpages, sum(case when (month(print_date) = $month and year(print_date) = $year) then print_pages end) as monthpages, sum(case when (day(print_date) = $day and year(print_date) = $year and month(print_date) = $month) then print_pages end) as daypages, sum(case when (week(print_date) = $week AND year(print_date) = $year AND month(print_date) = $month) then print_pages end) as weekpages
FROM `file_prints`
GROUP BY station_name;";
$rows = $_userTable->getAdapter()->fetchAll($select);
foreach($rows as $key => $row)
{
$oModelStation = new Application_Model_PrintStation();
$fPrintsModel = new Application_Model_FilePrints();
$station = $oModelStation->fetchRow($oModelStation->select()->where('name = ?', $row['station_name']));
$row['weekpages'] = $fPrintsModel->fetchRow($fPrintsModel
->select(array('sum(print_pages) as weekpages'))
->where('station_name = ?', $row['station_name']));
->where('year(print_date) = ?', $year)
->where('week(print_date) = ?', $week));
echo $row['weekpages'];
错误追溯到最后一行就是这个示例代码(echo $row['weekpages'])。我已经通过初始 $select SQL 查询的回声测试了结果并且它看起来令人满意,并且我已经尝试删除 fetchRow 执行中的各种 where 条件以查看是否应该归咎于一个条件但是我不断收到相同的错误消息。
此外,当我使用此代码执行 var_dump($row) 时,我看到 weekpages 的键指向 Zend objects 而所有其他键用于 daypages、monthpages , 和 yearpages 指向包含数值的字符串。
错误消息告诉您问题出在哪里。 fetchRow() 返回一个 Zend_Db_Table_Row 对象,然后您将其存储在索引 'weekpages' 的数组中。我不确定您打算如何处理检索到的行中的数据,但您可以尝试使用 ->toArray() 将行对象转换为数组,或者像这样访问行对象的各个字段:
echo $row['weekpages']->weekpages;
假设您打算让 $row['weekpages'] 保存行对象,尽管您为索引选择的字符串让我认为情况可能并非如此。
为了进一步说明,$row['weekpages'] 的值是行对象,您在查询中使用别名的列也称为 "weekpages"。
当我 运行 以下内容时,我在标题中收到错误消息:
$year = date('Y');
$month = date('n');
$day = date('j') - 3;
$week = date('W') - 1;
$select = "SELECT IF( station_name = '', 'Unknown', station_name ) station_name, station_name as db_name, sum(case when year(print_date) = $year then print_pages end) as yearpages, sum(case when (month(print_date) = $month and year(print_date) = $year) then print_pages end) as monthpages, sum(case when (day(print_date) = $day and year(print_date) = $year and month(print_date) = $month) then print_pages end) as daypages, sum(case when (week(print_date) = $week AND year(print_date) = $year AND month(print_date) = $month) then print_pages end) as weekpages
FROM `file_prints`
GROUP BY station_name;";
$rows = $_userTable->getAdapter()->fetchAll($select);
foreach($rows as $key => $row)
{
$oModelStation = new Application_Model_PrintStation();
$fPrintsModel = new Application_Model_FilePrints();
$station = $oModelStation->fetchRow($oModelStation->select()->where('name = ?', $row['station_name']));
$row['weekpages'] = $fPrintsModel->fetchRow($fPrintsModel
->select(array('sum(print_pages) as weekpages'))
->where('station_name = ?', $row['station_name']));
->where('year(print_date) = ?', $year)
->where('week(print_date) = ?', $week));
echo $row['weekpages'];
错误追溯到最后一行就是这个示例代码(echo $row['weekpages'])。我已经通过初始 $select SQL 查询的回声测试了结果并且它看起来令人满意,并且我已经尝试删除 fetchRow 执行中的各种 where 条件以查看是否应该归咎于一个条件但是我不断收到相同的错误消息。
此外,当我使用此代码执行 var_dump($row) 时,我看到 weekpages 的键指向 Zend objects 而所有其他键用于 daypages、monthpages , 和 yearpages 指向包含数值的字符串。
错误消息告诉您问题出在哪里。 fetchRow() 返回一个 Zend_Db_Table_Row 对象,然后您将其存储在索引 'weekpages' 的数组中。我不确定您打算如何处理检索到的行中的数据,但您可以尝试使用 ->toArray() 将行对象转换为数组,或者像这样访问行对象的各个字段:
echo $row['weekpages']->weekpages;
假设您打算让 $row['weekpages'] 保存行对象,尽管您为索引选择的字符串让我认为情况可能并非如此。
为了进一步说明,$row['weekpages'] 的值是行对象,您在查询中使用别名的列也称为 "weekpages"。