Symfony Propel 模型层
我有三个表,例如 user、job 和 job_applied。 user 表有 uid ,job 表有 jid ,job_applied 有 uid 和 jid 。我需要根据job_applied表的uid和jid获取职位表的职位名称、描述、职位。 我可以从下面的代码中获取值,但我认为这是错误的方式(粗略的方式)。
$this->jobapplieds = $this->getUser()->getUser()->getJobApplieds();
foreach($jobapplieds as $ja)
{
$c = new Criteria();
$c->clearSelectColumns();
$c->addSelectColumn(JobPeer::TITLE);
$c->addSelectColumn(JobPeer::DESCRIPTION);
$c->addSelectColumn(JobPeer::STATUS);
$c->add(JobPeer::JID,$ja->getJid());
$rs = JobPeer::doSelectRS($c);
while($rs->next())
{
echo $rs->getString(1);
print $rs->getString(2);
}
echo $ja->getAppliedAt();
}
I have three tables such as user, job and job_applied. user table has uid , job table has jid and job_applied has uid and jid . I need to get job title, description, position of job table according to uid and jid of job_applied table.
I'm able to get values from below code, But i think this is the wrong way (crude way) .
$this->jobapplieds = $this->getUser()->getUser()->getJobApplieds();
foreach($jobapplieds as $ja)
{
$c = new Criteria();
$c->clearSelectColumns();
$c->addSelectColumn(JobPeer::TITLE);
$c->addSelectColumn(JobPeer::DESCRIPTION);
$c->addSelectColumn(JobPeer::STATUS);
$c->add(JobPeer::JID,$ja->getJid());
$rs = JobPeer::doSelectRS($c);
while($rs->next())
{
echo $rs->getString(1);
print $rs->getString(2);
}
echo $ja->getAppliedAt();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果当前用户已申请 10 个职位,则您当前会执行 10 + 1 次查询(一次查询获取所有申请,然后针对每个职位查询一次)。您可以通过两种方式改进这一点:
您可以首先收集数组中的所有
jid
值,然后执行IN
查询,而不是在循环中执行 10 个查询,这样您就 可以进行 1 + 1 查询。另一种选择是执行一个查询,从
job
表开始,将其与job_applied
表连接,然后设置该表的uid
将job_applied
表添加到您当前的用户 ID。这应该只执行一个查询。If the current user has applied for 10 jobs, you currently do 10 + 1 queries (one to get all applications and then one per job). You can improve this in two ways:
Instead of doing 10 queries in your loop, you can first collect all
jid
values in an array and then do anIN
query, so you do 1 + 1 queries.The other option is to do one query where you start with the
job
table, join it with thejob_applied
table, and set theuid
of thejob_applied
table to your current user ID. This should execute only one query.