如何使用Doctrine 2管理VIEW?

时间:2012-05-03 13:00:51

标签: mapping doctrine-orm sql-view temp-tables nativequery

我想用 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

1 个答案:

答案 0 :(得分:1)

(OP在aq问题编辑中回答。转录为社区wiki答案。请参阅Question with no answers, but issue solved in the comments (or extended in chat)

OP写道:

  

我解决了!!!!

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();
}
  

这返回一个数组。 PERFECT!