使用doctrine而不是sql

时间:2014-02-17 11:04:12

标签: mysql symfony doctrine

我必须在我的symfony-web-controller中关注函数:

private function findResponsibleDepartment( $suche )
{

    $doctrine = $this->getDoctrine();
    $em = $doctrine->getManager();
    $arrTerm = explode(" ", $suche);
    for ($i = 0; $i < count($arrTerm); $i++)
    {
        $arrWhere[] = "gs.plz LIKE '" . $arrTerm[$i] . "'";
        //$arrParameter[] = "\"value".$i . "\" => \"%" .$arrTerm[$i]. "%\"";
    }

    $where = implode($arrWhere, " OR ");
    //$param = implode($arrParameter, ",");

    $query = $em->createQuery('
            SELECT nl.email
            FROM MbsNiederlassungBundle:GebietStadt gs
            INNER JOIN MbsNiederlassungBundle:Niederlassung nl WITH nl.id = gs.idNiederlassung
            WHERE '. $where);
    $result = $query->getResult();
    if( count($result) > 0 )
    {
        $email = $result[0]["email"];
        return $email;
    }
    else
    {
        return false;
    }
}

我想使用更多的学说声明。我如何更改类似学说的查询?

或者我应该更好地制作自定义存储库?

1 个答案:

答案 0 :(得分:1)

您可以使用query builder

构建查询
$qb = $em->createQueryBuilder()
    ->select('nl.email')
    ->from('MbsNiederlassungBundle:GebietStadt', 'gs')
    ->innerJoin('MbsNiederlassungBundle:Niederlassung', 'nl', 'WITH', 'nl.id = gs.idNiederlassung');

for ($i = 0; $i < count($arrTerm); $i++) {
    $ph = 'plz' . $i;
    $qb->orWhere('gs.plz LIKE ' . $ph);
    $qb->setParameter($ph, $arrTerm[$i]);
}