我想在 symfony Doctrine 中运行以下查询。
SELECT p.id AS id FROM skiChaletPrice p WHERE ski_chalet_id = ? AND month = ?
我写了我的 Doctrine 查询如下。
$q = Doctrine_Query::create()
->select('p.id AS id')
->from('skiChaletPrice p')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from);
$result = $q->fetchOne();
if ($result->count() > 0) {
return $result->toArray();
} else {
return null;
}
但我的结果总是包括表中的所有列。什么问题?请帮我。
最佳答案
问题是 fetchOne()
将返回一个 Doctrine 对象,它隐含地包含表中的所有列。 $result->toArray()
正在将该 Doctrine 对象转换为数组,这就是您获取所有列的原因。
如果你只想要列的一个子集,不要水化一个对象,而是做这样的事情:
$q = Doctrine_Query::create()
->select('p.id AS id')
->from('skiChaletPrice p')
->andWhere('ski_chalet_id = ?', $chaletId)
->andWhere('month = ?', $from);
$results = $q->execute(array(), Doctrine::HYDRATE_SCALAR);
参见 http://docs.doctrine-project.org/projects/doctrine1/en/latest/en/manual/data-hydrators.html
关于mysql - 带有选定列的 Symfony 1.4 选择查询不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16100173/