这个错误对我来说似乎是一个错误,因为我的查询中没有位置参数。这是方法:
public function getAll(User $user, DateTime $start = null, DateTime $end = null)
{
$params = array('user_id' => $user->getId());
$rsm = new \Doctrine\ORM\Query\ResultSetMapping(); // Result set mapping
$rsm->addScalarResult('subtype', 'subtype');
$rsm->addScalarResult('count', 'count');
$sms_sql =
"SELECT CONCAT('sms_', IF(is_auto = 0, 'user' , 'auto')) AS subtype, " .
"SUM(messages_count * (customers_count + recipients_count)) AS count " .
"FROM outgoing_message AS m INNER JOIN small_text_message AS s ON " .
"m.id = s.id WHERE status <> 'pending' AND user_id = :user_id";
$news_sql =
"SELECT CONCAT('news_', IF(is_auto = 0, 'user' , 'auto')) AS subtype, " .
"SUM(customers_count + recipients_count) AS count " .
"FROM outgoing_message AS m JOIN newsletter AS n ON m.id = n.id " .
"WHERE status <> 'pending' AND user_id = :user_id";
if($start) :
$sms_sql .= " AND sent_at >= :start";
$news_sql .= " AND sent_at >= :start";
$params['start'] = $start->format('Y-m-d');
endif;
$sms_sql .= ' GROUP BY type, is_auto';
$news_sql .= ' GROUP BY type, is_auto';
return $this->_em->createNativeQuery("$sms_sql UNION ALL $news_sql", $rsm)
>setParameters($params)->getResult();
}
这引发了异常:
SQLSTATE[HY093]:无效参数号:混合命名参数和位置参数
数组$params
没问题,因此生成了 SQL:
var_dump($params);
array (size=2)
'user_id' => int 1
'start' => string '2012-01-01' (length=10)
最奇怪的是它只适用于"$sms_sql"
!
更新
又发现了一件奇怪的事。如果我只更改名称(改为start_date
而不是start
):
if($start) :
$sms_sql .= " AND sent_at >= :start_date";
$news_sql .= " AND sent_at >= :start_date";
$params['start_date'] = $start->format('Y-m-d');
endif;
发生的事情是 Doctrine/PDO 说:
SQLSTATE [42S22]:找不到列:1054 'where 子句'中的未知列'sent1rt_date'
...因为1rt
在列名中间添加了字符串!