Symfony Propel 模型层

发布于 2024-11-02 01:08:26 字数 746 浏览 2 评论 0原文

我有三个表,例如 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦醒时光 2024-11-09 01:08:26

如果当前用户已申请 10 个职位,则您当前会执行 10 + 1 次查询(一次查询获取所有申请,然后针对每个职位查询一次)。您可以通过两种方式改进这一点:

您可以首先收集数组中的所有 jid 值,然后执行 IN 查询,而不是在循环中执行 10 个查询,这样您就 可以进行 1 + 1 查询。

$this->jobapplieds = $this->getUser()->getUser()->getJobApplieds();
$jobapplieds_by_jid = array();
foreach ($jobapplieds as $ja)
{
    // If you can apply multiple times for the same job, this should be a second-level array
    $jobapplieds_by_jid[$ja->getJid()] = $ja;
}

$c = new Criteria();
$c->add(JobPeer::JID, array_keys($jobapplieds_by_jid), Criteria::IN);
$jobs = JobPeer::doSelect($c); 

foreach ($jobs as $job)
{
    echo $job->getTitle();
    echo $job->getDescription();
    echo $jobapplieds_by_jid[$job->getJid()]->getAppliedAt();
}

另一种选择是执行一个查询,从 job 表开始,将其与 job_applied 表连接,然后设置该表的 uidjob_applied 表添加到您当前的用户 ID。这应该只执行一个查询。

$c = new Criteria();
$c->add(JobAppliedPeer::UID, $this->getUser()->getUser()->getUid());
$jobs = JobPeer::doSelectJoinJobApplied($c);
foreach ($jobs as $job)
{
    echo $job->getTitle();
    echo $job->getDescription();
    echo $job->getJobApplied()->getAppliedAt();
}

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 an IN query, so you do 1 + 1 queries.

$this->jobapplieds = $this->getUser()->getUser()->getJobApplieds();
$jobapplieds_by_jid = array();
foreach ($jobapplieds as $ja)
{
    // If you can apply multiple times for the same job, this should be a second-level array
    $jobapplieds_by_jid[$ja->getJid()] = $ja;
}

$c = new Criteria();
$c->add(JobPeer::JID, array_keys($jobapplieds_by_jid), Criteria::IN);
$jobs = JobPeer::doSelect($c); 

foreach ($jobs as $job)
{
    echo $job->getTitle();
    echo $job->getDescription();
    echo $jobapplieds_by_jid[$job->getJid()]->getAppliedAt();
}

The other option is to do one query where you start with the job table, join it with the job_applied table, and set the uid of the job_applied table to your current user ID. This should execute only one query.

$c = new Criteria();
$c->add(JobAppliedPeer::UID, $this->getUser()->getUser()->getUid());
$jobs = JobPeer::doSelectJoinJobApplied($c);
foreach ($jobs as $job)
{
    echo $job->getTitle();
    echo $job->getDescription();
    echo $job->getJobApplied()->getAppliedAt();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文