我想用Doctrine2映射一个 sql-view 。
此视图是一个TempTable,其中包含一些统计信息,无需重写生成视图的 sql 即可显示
我尝试像表一样映射,但更新架构会删除视图并创建表
我也尝试使用 NativeSQL ...
public function getMessages(\Project\Bundle\MyBundle\Entity\User $user) {
$rsm = new \Doctrine\ORM\Query\ResultSetMapping();
$rsm->addEntityResult('MessageCenter', 'v');
$rsm->addFieldResult('v', 'user_id', 'user_id');
$rsm->addFieldResult('v', 'tot', 'tot');
$rsm->addFieldResult('v', 'read', 'read');
$rsm->addFieldResult('v', 'to_read', 'to_read');
$rsm->addFieldResult('v', 'stored', 'stored');
$rsm->addFieldResult('v', 'spam', 'spam');
$q = "SELECT * FROM message_stats_view WHERE user_id = ?";
$rsm = new \Doctrine\ORM\Query\ResultSetMapping;
$query = $this->getEntityManager()->createNativeQuery($q, $rsm);
$query->setParameter(1, $user->getId());
echo $query->getSQL();
var_dump($query->execute());
exit;
}
我使用 getter 和 setter 创建实体 MessageCenter,但我的输出是:
SELECT * FROM message_stats_view WHERE user_id = ?
array
empty