これが私のモデルです:
<?php
namespace Object\Model;
use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Sql;
class SiteVisitsTable {
protected $tableGateway;
protected $sql;
public function __construct(TableGateway $tableGateway) {
$this->tableGateway = $tableGateway;
$this->sql = new Sql($this->tableGateway->adapter);
}
public function getVisits($field = '', $value = '') {
if ($field = '' || $value = ''):
$rowset = $this->tableGateway->select(array($field => $value));
else:
$rowset = $this->tableGateway->select();
endif;
$row = $rowset->current();
return $row;
}
public function getAllVisits() {
$adapter = $this->tableGateway->adapter;
$select = $this->sql->select();
$select->from($this->tableGateway->getTable());
$select->columns(array(
new \Zend\Db\Sql\Expression('SUM(`unique`) as `unique`'),
new \Zend\Db\Sql\Expression('SUM(`impressions`) as `impressions`'),
));
$selectString = $this->sql->getSqlStringForSqlObject($select);
$resultSet = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
return $resultSet;
}
public function addVisit($date) {
$currentVisits = $this->getVisits('date', $date);
if ($currentVisits) {
$data = array(
'unique' => $currentVisits + 1,
);
$this->tableGateway->update($data, array('date' => $date));
} else {
$data = array(
'unique' => 1,
'impressions' => 1,
);
$this->tableGateway->insert($data);
}
}
}
getAllVisits public メソッドには、SELECT CUM MySQL クエリがあります。クエリ自体はすべて問題ありませんが、クエリから値を出力できません。$resultSet のダンプは次のとおりです。
Zend\Db\ResultSet\ResultSet Object
(
[allowedReturnTypes:protected] => Array
(
[0] => arrayobject
[1] => array
)
[arrayObjectPrototype:protected] => ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
)
)
[returnType:protected] => arrayobject
[buffer:protected] =>
[count:protected] => 1
[dataSource:protected] => Zend\Db\Adapter\Driver\Pdo\Result Object
(
[statementMode:protected] => forward
[resource:protected] => PDOStatement Object
(
[queryString] => SELECT SUM(`unique`) as `unique`, SUM(`impressions`) as `impressions` FROM `site_visits`
)
[options:protected] =>
[currentComplete:protected] =>
[currentData:protected] =>
[position:protected] => -1
[generatedValue:protected] => 0
[rowCount:protected] => 1
)
[fieldCount:protected] => 2
[position:protected] =>
)
queryString キーが表示される場合があります。phpmyadmin で「生の」クエリを実行すると、MySQL データベースの値が表示されますが、ZF2 で表示しようとしても機能しないようです。
これが私のコントローラーです:
return new ViewModel(array(
'todayVisits' => $this->getSiteVisitsTable()->getVisits('date', date('Y-m-d')),
'allVisits' => $this->getSiteVisitsTable()->getAllVisits(),
'yesterdayVisits' => $this->getSiteVisitsTable()->getVisits('date', ''),
'inactiveBlogs' => $this->getBlogsTable()->getInactiveBlogs(),
'inactiveUsers' => $this->getUsersTable()->getUsers('is_active', '0'),
'notes' => $this->getAdminNotesTable()->getAllNotes(),
'form' => $form,
));
そして私の見解:
<article class="stats_overview">
<div class="overview_today">
<p class="overview_day">Днес</p>
<p class="overview_count"><?php echo $todayVisits->unique; ?></p>
<p class="overview_type">посещения</p>
<p class="overview_count"><?php echo $todayVisits->impressions; ?></p>
<p class="overview_type">импресии</p>
</div>
<div class="overview_previous">
<p class="overview_day">Общо</p>
<p class="overview_count"><?php echo $allVisits->unique; ?></p>
<p class="overview_type">посещения</p>
<p class="overview_count"><?php echo $allVisits->impressions; ?></p>
<p class="overview_type">импресии</p>
</div>
</article>
PS JOIN クエリにも同じ問題があります。:) 前もって感謝します。