我已經表所示:product_id->名稱,描述,價格Symfony的3學說如何建立一個沒有列索引後綴查詢
我想要檢索的所有產品,所以這是我的代碼檢索產品
$doc = $this->getDoctrine();
$em = $doc->getEntityManager();
$conn = $em->getConnection();
$repo = $doc->getRepository('AppBundle:Products');
$query = $repo->createQueryBuilder('p')->getQuery();
$sql = $query->getSql();
$stmt = $conn->prepare($sql);
$stmt->execute();
$products = $stmt->fetchAll(\PDO::FETCH_ASSOC);
問題是構建查詢其列名作爲別名後總是添加列索引後綴時
那麼結果會是這樣的:
[
{"product_id_0":"1","name_1":"Name1","description_2":"Desc1","price_3":"100"},
{"product_id_0":"2","name_1":"Name2","description_2":"Desc2","price_3":"200"}
]
注意到他們都喜歡_0,_1後綴後,我不想要它, 我怎樣才能建立查詢,而無需像別名列名?
替換您最後用$查詢 - > fetchArray線。更好的是,使用fetchAll並使用對象。 http://doctrine-orm.readthedocs.org/projects/doctrine-dbal/en/latest/reference/data-retrieval-and-manipulation.html – Cerad
@Cerad它們在類Doctrine \ ORM \ Query中沒有名爲fetchArray的方法,學說\ DBAL \聲明。 –
我輸入得太快了。 $查詢 - > getArrayResult()。 http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/dql-doctrine-query-language.html#query-result-formats – Cerad