0

結果セットは、グループ/カウント クエリから取得されます。

SELECT m.type, COUNT(m.type)
FROM sent_message m
WHERE m.user_id = :user_id
      AND (m.sent_at >= :start_date OR m.sent_at <= :end_date)
GROUP BY m.type

そして、別の配列にネストされた配列の形式になっています。

array (size=2)
  0 => 
    array (size=2)
      'type' => string 'sms' (length=3)
      'count' => string '1' (length=1)
  1 => 
    array (size=2)
      'type' => string 'email' (length=5)
      'count' => string '9' (length=1)

typeループせずに特定のインデックスが存在するかどうかを簡単に確認する方法はありますか? これを回避するには:

$resultSet = $repository->getAllCount(); // Returns the nested array
$viewBag   = array('sent_sms_count' => 0, 'sent_email_count' => 0); // Init

foreach($resultSet as $result) :

    if('sms' == strtolower($result['type'])
        $viewBag['sent_sms_count'] = $result['count'];

    if('email' == strtolower($result['type'])
        $viewBag['sent_email_count'] = $result['count'];

endforeach;
4

1 に答える 1

1

ループせずに気付く方法はありません。ただし、SQL の結果を好きな形式に再インデックスすることができます。

foreach ($resultSet as $result) {
    $viewBag['sent_' . strtolower($result['type']) . '_count'] = $result['count'];
}
于 2012-05-23T22:51:04.737 に答える