私は zend Framework でプロジェクトに取り組んでおり、$result
ここに私のクエリがあるので、SQL クエリの結果を 1 つの配列にマージしようとしています:
public function getOpen()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('e' => 'emails'), array(
'isp' => new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"),
'count' => new Zend_Db_Expr('COUNT( * )')))
->join(array('to' => 'track_open'), 'e.email_id = to.email_id', array(''))
->where('e.email_id = to.email_id')
//->where('DATE( open_date ) = ? ', $date )
->group(new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
public function getUnsubscribe()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('e' => 'emails'), array(
'isp' => new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"),
'count' => new Zend_Db_Expr('COUNT( * )')))
->join(array('tu' => 'track_unsubscribe'), 'e.email_id = tu.email_id', array(''))
->where('e.email_id = tu.email_id')
//->where('DATE( unsubscribe_date ) = ? ', $date )
->group(new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
public function getClick()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('e' => 'emails'), array(
'isp' => new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"),
'count' => new Zend_Db_Expr('COUNT( * )'),
'tc.click_date',))
->join(array('tc' => 'track_click'), 'e.email_id = tc.email_id', array(''))
->where('e.email_id = tc.email_id')
//->where('DATE( click_date )= ? ', $date )
->group(new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
public function getlogs()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('ee' => 'email_errors'),
array( 'isp' => new Zend_Db_Expr('ee.FAI'),
'sent' => new Zend_Db_Expr("SUM( ee.STATUS =0 )"),
'deferred' => new Zend_Db_Expr("SUM( ee.STATUS =1 )"),
'bounced' => new Zend_Db_Expr("SUM( ee.STATUS =2 )")))
->group(new Zend_Db_Expr("FAI"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
public function getSB()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('e' => 'emails'), array(
'isp' => new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"),
'count' => new Zend_Db_Expr('COUNT( * )')))
->join(array('tb' => 'track_bounce'), 'e.email_id = tb.email_id', array(''))
->where('e.email_id = tb.email_id')
//->where('DATE( bounce_date ) = ? ', $date )
->where('tb.bounce_type = ?', "soft")
->group(new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
public function getHB()
{
$query = $this->select()
->setIntegrityCheck(false)
->from(array('e' => 'emails'), array(
'isp' => new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"),
'count' => new Zend_Db_Expr('COUNT( * )')))
->join(array('tb' => 'track_bounce'), 'e.email_id = tb.email_id')
->where('e.email_id = tb.email_id')
//->where('DATE( bounce_date ) = ? ', $date )
->where('tb.bounce_type = ?', "hard")
->group(new Zend_Db_Expr("SUBSTRING_INDEX( e.email_value, '@', -1 )"));
$result = $this->fetchAll($query);
if($result)
{
return $result->toArray();
}
return false;
}
そして、マージを行うために私が行ったこの機能:
function array_merge_rec($arrays)
{
$result = array();
$keys = array('logs','open', 'click', 'unsubscribe', 'soft', 'hard', );
foreach ($arrays as $key => $value) {
if (is_array($value)) {
foreach ($value as $item) {
$result[$item['isp']][$key] = $item['count'];
}
}
}
$results = array();
foreach($result as $key => $value)
{
foreach($keys as $k)
{
if(!isset($result[$key][$k])) $result[$key][$k] = 0;
}
}
return $result;
}
必要に応じて、これは私のアクション コントローラーです。
public function logsAction()
{
$log_review = new Dashboard_Model_DbTable_logs();
$open = $log_review->getOpen();
$click = $log_review->getClick();
$unsubscribe = $log_review->getUnsubscribe();
$logs = $log_review->getlogs();
$SB = $log_review->getSB();
$HB = $log_review->getHB();
$result = $this->array_merge_rec(array('logs'=>$logs,'open' => $open, 'click' => $click, 'unsubscribe' => $unsubscribe, 'hard' => $HB, 'soft' => $SB));
$this->view->data = $result;
}
私が言ったように、私がやろうとしているのは、クエリのすべての結果を ISP に基づいて 1 つの配列 "$result" にマージすることです。そして、getlogsクエリなしですべてのクエリをマージすると、関数は正常に機能しますが、getlogsクエリを関数パラメーターに追加すると機能せず、正常です。したがって、マージ関数を変更して、すべてのクエリの共通の ISP 列にマージされたすべての列を含む配列を取得する方法を知るために助けが必要です