Doctrine query returns extra field in result

不问归期 提交于 2020-01-06 20:57:49

问题


I have a Doctrine query;

    $q = Doctrine_Query::create()->select('s.monthly_volume')
    ->from('SearchVolume s')
    ->innerJoin('s.Keywords k')
    ->where('k.group_id = ?',array($group_id));

I just want it to return the monthly_volume value in the result array. It currently returns monthly_volume and id, I don't want it to return the id in result.


回答1:


Doctrine automatically adds the primary key field to the results in almost every type of hydration mode.

In a case like this where you want a simple array and only have a single field being selected, the answer is the Single Scalar Hydration mode. Use it like this:

$q = Doctrine_Query::create()->select('s.monthly_volume')
    ->from('SearchVolume s')
    ->innerJoin('s.Keywords k')
    ->where('k.group_id = ?');

$monthly_volumes = $q->execute(array($group_id), Doctrine_Core::HYDRATE_SINGLE_SCALAR);

You should find that $monthly_volumes is a simple one-dimensional array containing only the value(s) you wanted.



来源:https://stackoverflow.com/questions/6032854/doctrine-query-returns-extra-field-in-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!