Poniżej przedstawiam zapytanie:
Kod w php:
$recordsObjs = Doctrine_Query::create() ->select('c.id as clientid, e.id as eventid') ->addSelect('(select name_surname from client_contact where client = c.id limit 1) as name_surname') [b]->addSelect('(select count(*) from reservation where event = e.id and client = c.id and dict_reservation_status = 2) as unconfirmed_count')[/b] ->from('Reservation r') ->innerJoin('r.Client c') ->innerJoin('r.Event e') ->groupBy('c.id, e.id');
SELECT c.id AS c__0, e.id AS e__1, (SELECT r.name_surname FROM client_contact WHERE r.client = c.id LIMIT 1) AS r__8, [b](SELECT COUNT(*) FROM reservation WHERE r.event = e.id AND r.client = c.id AND r.dict_reservation_status = 2) AS r__11 FROM reservation r [/b] INNER JOIN client c ON r.client = c.id INNER JOIN event e ON r.event = e.id GROUP BY c.id, e.id ORDER BY c__2 ASC
Chodzi o wyróżnione linijki nie chcę aby Doctrine traktował tego
event = e.id and client = c.id
tak
r.event = e.id AND r.client = c.id
tylko jako oddzielny model. Próbowałem już tak:
->addSelect('(select count(*) from reservation r2 where r2.event = e.id and r2.client = c.id and r2.dict_reservation_status = 2) as unconfirmed_count')
ale wyrzuca błąd:
Couldn't find class r2
czyli tak się nie da....Chodzi o to żeby zastosować tak jakby zewnętrzne zapytanie (r2) stosując łączenie z istniejącym modelem (r2.event(wyniki z zewntątrz) = e.id(zadeklarowane modele) and r2.client(wyniki z zewnątrz) = c.id(zadeklarowane modele).
Problem jest w tym, że doctrine traktuje de facto drugą tabelę reservation jako jedną i tą samą reservation, a powinien traktować jako coś zupełnie nowego.
Gdyby ktoś chciał mi pomóc to proszę o odpowiedź.
Rozwiązałem problem....
Zamieniłem na takie coś....po prostu dodałem alias:
->addSelect('(select count(*) from reservation r where r.event = e.id and r.client = c.id and r.dict_reservation_status = 2) as confirmed_count')