自定义存储库Symfony

时间:2017-12-09 22:32:44

标签: symfony repository

我有一系列ID(idExam)

我想创建一个自定义存储库,在WHERE上添加所有ID。

这是我的代码:

public function examNotValidated($idExam)
{
    return $this->createQueryBuilder('q')
        ->where('q.creadetby = :creadetby and q.id = :idExam')
        ->setParameter('creadetby', false)
        ->setParameter('idExam', $idExam)
        ->getQuery()
        ->getResult();
}

我想要这样的事情:

public function examNotValidated($idExam)
{
    return $this->createQueryBuilder('q')
        ->where('q.creadetby = :creadetby and (q.id = :idExams[0] or q.id = :idExams[1] or q.id = :idExams[3])')
        ->setParameter('creadetby', false)
        ->setParameter('idExams', $idExams)
        ->getQuery()
        ->getResult();
}

有没有这样做?

1 个答案:

答案 0 :(得分:2)

您可以使用public function examNotValidated(array $ids) { $qb = $this->createQueryBuilder('q'); return $qb->where('q.creadetby = :creadetby') ->andWhere($qb->expr()->in('q.id', $ids)) ->setParameter('creadetby', false) ->getQuery() ->getResult(); } 表达式:

public function examNotValidated(array $ids)
{
    $qb = $this->createQueryBuilder('q');

    $orX = $qb->expr()->orX();
    foreach ($ids as $id) {
        $orX->add('q.id = '.$id);
    }

    return $qb->where('q.creadetby = :creadetby')
        ->andWhere($orX);
        ->setParameter('creadetby', false)
        ->getQuery()
        ->getResult();
}

这是相反的逻辑(例如:q.id =:idExams [0]或q.id =:idExams [1]或q.id =:idExams [3])

from tkinter import *

root = Tk()

def  buttonsMake():
    for c in range(10):
        for r in range(3):
            movieSeats=Button(root, text="Empty", bg="green", fg="white", 
width=5, height=1, command=lambda c=c, r=r:[redClick(c, r)])
            movieSeats.grid(row=r,column=c)


def redClick(c, r):
    movieSeats.configure(bg="red")



buttonsMake()
root.mainloop()