仕事で基本的なWebアプリを開発しています。私はいくつかのSQLサーバービューで作業する必要があります。私はネイティブ クエリを試すことにしました。その機能をテストしたら、すべてのクエリをコーディングするクラスをいくつか作成してみて、それらの実装をちょっと忘れてしまいました。
私の問題は、Acme/MyBundle/Entity/View1.php にエンティティがあることです。このエンティティには、テーブルに一致するすべての属性があり、ゲッターとセッターでもあります。このエンティティは DB に適切にマッピングされていると思います (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()
ですよね?そして、何が私を殺しているのですか、どうすればattribute1、attriute2、attriute2にアクセスできますか?
$returned_atts = array(
"att1" => $result[0]->getAttribute1(), //getter from the entity
"att2" => $result[1]->getAttribute2(), //getter from the entity
);
return $returned_atts;`