如何获得每项工作下的参赛作品数量。让我们来看一个招聘教程,在这里我想展示每个职位的简历提交数量。
表
job : id title
submit : id job_id file
结果:
title submits
job a 5
old job 20
我正在使用 Doctrine 。那么,我该怎么做呢?
答案 0 :(得分:1)
类似的东西:
$job = Doctrine_Core::getTable('Job')->find(1);
// it will query the database to fetch the relation
$submit = $job->getSubmit()->count();
但如果您想要更具体的内容,请告诉我们您的schema.yml.
如果您想避免对数据库进行非新查询,可以使用leftJoin
获取关系:
$job = Doctrine_Core::getTable('Job')
->leftJoin('Job.Submit')
->find(1);
// the relation is already fetched, so it won't perform a new query
$submit = $job->getSubmit()->count();
顺便说一句,要小心->count()
方法,如果你有很多提交,这个查询将不会真正优化。最好的,是执行手动查询计数:
$submit = Doctrine_Core::getTable('Submit')
->createQuery('s')
->select('COUNT(s.id) as total')
->where('s.job_id = ?', array($job->getId()))
// HYDRATE_NONE won't hydrate the return object
->execute(array(), Doctrine_Core::HYDRATE_NONE);
// I guess the value is here but test yourself
var_dump($submit[0]);