我正在为我的工作开发一个基本的网络应用程序。我必须使用一些 sql server 视图。我决定尝试原生查询,一旦测试了它的功能,就尝试编写一些类来对所有查询进行编码,然后忘记它们的实现。
所以我的问题是,我在 Acme/MyBundle/Entity/View1.php 中有一个实体。该实体具有与表匹配的所有属性以及它的 getter 和 setter。我猜这个实体很好地映射到数据库(Doctrine 不能轻松地使用视图)。
我的目标是让控制器能够从这些视图(SQL SERVER)中获取一些数据并将其返回给视图(树枝),以便它可以显示信息。
$returned_atts = array(
"att1" => $result[0]->getAttribute1(), //getter from the entity
"att2" => $result[1]->getAttribute2(), //getter from the entity
);
return $returned_atts;`$sql = "SELECT [Attribute1],[Attribute2],[Attribute3] FROM [TEST].[dbo].[TEST_VIEW1]"; //THIS IS THE SQL SERVER QUERY
$rsm = new ResultSetMapping($em); //result set mappin object
$rsm->addEntityResult('Acme\MyBundle\Entity\View1', 'view1'); //entity which is based on
$rsm->addFieldResult('view1', 'Attribute1', 'attribute1'); //only choose these 3 attributes among the whole available
$rsm->addFieldResult('view1', 'Attribute2', 'attribute2');
$rsm->addFieldResult('view1', 'Attribute3', 'attribute3');
//rsm built
$query = $em->createNativeQuery($sql, $rsm); //execute the query
$result = $query->getResult(); //get the array
应该可以直接从getResult()
方法返回数组不是吗?什么让我很生气,我怎样才能访问attriute1、attriute2和attriute2?
$returned_atts = array(
"att1" => $result[0]->getAttribute1(), //getter from the entity
"att2" => $result[1]->getAttribute2(), //getter from the entity
);
return $returned_atts;`