Symfony FOSRestController

时间:2015-09-03 11:28:20

标签: mysql rest symfony doctrine-orm

我有项目API,我按技能创建查找项目的功能,当我找到Project by skill = Java Query Builder给我结果项目时带有“Java脚本”,“Java ME”和所有具有部分单词的技能“ Java的”。

如何百分百匹配?

这是我的控制器和行动:

findForMainSkillProject

此功能findForSkillProject public function findForMainSkillProject($paramFetcher) { $qb = $this->getEntityManager()->createQueryBuilder('d'); $qb ->select('d') ->from('ArtelProfileBundle:Project', 'd') ->where('d.currentStatus != :identifier1', 'd.main_skill = :identifier') ->setParameter('identifier1', 'not_approved') ->setParameter('identifier', $paramFetcher->get('main_skill')); $count_by_skill = $qb->getQuery()->getResult(); $qb ->orderBy('d.'.$paramFetcher->get('sort_by'), $paramFetcher->get('sort_order')) ->setFirstResult($paramFetcher->get('count') * ($paramFetcher->get('page') - 1)) ->setMaxResults($paramFetcher->get('count')) ->getQuery() ->getResult() ; $query = $qb->getQuery(); $results = $query->getResult(); return [$results, $count_by_skill]; } public function findForSkillProject($paramFetcher) { $close = $paramFetcher->get('close'); $em = $this->getEntityManager(); $qb = $em->createQueryBuilder() ->from('ArtelProfileBundle:Project','d') ->select('d as projects') ->where('d.currentStatus != :identifier1') ->setParameter('identifier1', 'not_approved'); if (!empty($close)) { $qb ->where('d.close = :identifier') ->setParameter('identifier', $paramFetcher->get('close')); } $qb->andWhere($qb->expr()->like('d.skills', $qb->expr()->literal('%' . $paramFetcher->get('skill') . '%'))); $count_by_skill = $qb->getQuery()->getResult(); $qb ->orderBy('d.'.$paramFetcher->get('sort_by'), $paramFetcher->get('sort_order')) ->setFirstResult($paramFetcher->get('count') * ($paramFetcher->get('page') - 1)) ->setMaxResults($paramFetcher->get('count')) ->getQuery() ->getResult() ; $query = $qb->getQuery(); $results = $query->getResult(); $build_result = []; foreach($results as $result){ $build_result[] = $result['projects']; } return [$build_result, $count_by_skill]; }

  public function findForSkillProject($paramFetcher)
{
    $close = $paramFetcher->get('close');

    $em = $this->getEntityManager();

    $qb = $em->createQueryBuilder()
        ->from('ArtelProfileBundle:Project','d')
        ->select('d as projects')
        ->where('d.currentStatus != :identifier1')
        ->setParameter('identifier1', 'not_approved');

    if (!empty($close)) {
        $qb
            ->where('d.close = :identifier')
            ->setParameter('identifier', $paramFetcher->get('close'));
    }

     $qb->andWhere('d.skills LIKE :skills OR d.main_skill LIKE :skills')
        ->setParameter('skills', '%'.$paramFetcher->get('skill').'%');

    $count_by_skill = $qb->getQuery()->getResult();
    $qb
        ->orderBy('d.'.$paramFetcher->get('sort_by'), $paramFetcher->get('sort_order'))
        ->setFirstResult($paramFetcher->get('count') * ($paramFetcher->get('page') - 1))
        ->setMaxResults($paramFetcher->get('count'))
        ->getQuery()
        ->getResult()
    ;

    $query = $qb->getQuery();
    $results = $query->getResult();
    $build_result = [];
    foreach($results as $result){
        $build_result[] = $result['projects'];
    }

    return [$build_result, $count_by_skill];
}

更新 我将两个Query Builder合并为一个by skill或main_skill但是当我使用'%'时。$ paramFetcher-> get('skill')。'%'如果我使用的话,我有“Java ME”或“Java Script”技能$ paramFetcher-> get('skill')我QB通过mainskill查找项目而不是通过技能找到 如何百分百匹配?

   Threat task1(...);

1 个答案:

答案 0 :(得分:0)

来自您的代码: 这就是喜欢(例如,' Java'返回' Java ME'

$qb->andWhere($qb->expr()->like('d.skills', $qb->expr()->literal('%' . $paramFetcher->get('skill') . '%')));

这部分代码确保它完全相同:

    ->where('d.currentStatus != :identifier1', 'd.main_skill = :identifier')
    ->setParameter('identifier1', 'not_approved')
    ->setParameter('identifier', $paramFetcher->get('main_skill'));

我相信你能够自己完成剩下的工作。