Doctrine query returns extra field in result

2019-08-26 02:29发布

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条回答
Juvenile、少年°
2楼-- · 2019-08-26 02:41

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.

查看更多
登录 后发表回答