我想用 映射一个 sql-view Doctrine2 .
此 View 是 临时表 包含一些统计信息,无需重写生成 View 的 sql 即可显示
我尝试像表一样映射,但更新架构删除 View 并创建表
我也尝试使用 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
最佳答案
(由 OP 在 aq 问题编辑中回答。转录到社区维基答案。见 Question with no answers, but issue solved in the comments (or extended in chat))
OP写道:
I SOLVED!!!!
public function getCentroMessaggi(\Project\Bundle\MyBundle\Entity\User $user) {
$connection = $this->getEntityManager()->getConnection();
$q = "SELECT * FROM message_stats_view WHERE user_id = :id";
$stmt = $connection->executeQuery($q, array('id' => $user->getId()));
return $stmt->fetch();
}
This return an array. PERFECT!
关于mapping - 如何使用 Doctrine 2 管理 VIEW?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10431922/