2011-09-21 4 views

Odpowiedz

17

Użyj operatora INSTANCE OF w zapytaniu DQL następująco (gdzie User to klasa bazowa):

$em->createQuery(' 
    SELECT u 
    FROM Entity\User u 
    WHERE (u INSTANCE OF Entity\Manager OR u INSTANCE OF Entity\Customer) 
'); 

Doktryna przekłada się to w zapytaniu SQL w stanie WHERE user.type = '...'.

Więcej informacji na temat składni zapytania dql można znaleźć pod numerem here.

+3

Można sprawdzić wiele wystąpień za pomocą polecenia '' WHERE (u INSTANCE OF (Entity \ Manager, Entity \ Customer)) ''. Jeśli masz '' QueryBuilder'' możesz użyć '' $ qb-> andWhere ('u INSTANCE OF: classes') -> setParameter ('classes', array ('Entity \ Manager', 'Entity \ Customer')); ''. – flu

+0

Pamiętaj, że podczas pracy z mapą dyskryminatora możesz potrzebować użyć wartości dyskryminatora, możesz to zrobić w swoim repozytorium w ten sposób: '$ this-> getClassMetadata() -> discriminatorValue' –

1

Jak commented przez flu, jeśli chcesz odzyskać niektóre podmioty z różnych przypadkach z QueryBuilder zamiast zapytania DQL, można użyć tablicę jako parametr:

$qb = $this->createQueryBuilder('u'); 
    ->where('u.id > 10') //an arbitrary condition, to show it can be combined with multiple instances tests 
    ->andWhere('u INSTANCE OF :classes') 
    ->setParameter('classes', ['Entity\Manager', 'Entity\Customer']) 
; 
4

odpowiedź dla wielu przypadkach faktycznie robi nie działa. Trzeba by zrobić coś takiego, aby sprawdzić wiele instancji.

$classes = ['Entity\Manager', 'Entity\Customer']; 
$qb = $this->createQueryBuilder('u'); 
->where('u.id > 10') //an arbitrary condition, to show it can be combined with multiple instances tests 
->andWhere("u INSTANCE OF ('" . implode("','", $classes) . "')"); 
Powiązane problemy