Doctrine 1.2: Can't reproduce query with Doctrine_Query

515 Views Asked by At

I have this SQL query:

select * 
from tblapplicant AS a 
WHERE a.napplicantid 
not in (
select napplicantid 
from tblcontract 
where dstart BETWEEN '2011-10-27' AND '2012-01-26' 
OR dend BETWEEN '2011-10-27' AND '2012-01-26')

And I want to build this query in Doctrine 1.2:

$Query = Doctrine_Query::create()
                    ->select('a')
                    ->from('tblapplicant a')
                    ->innerJoin('a.tblintermediair i')
                    ->where('i.nintermediairid = ? ', $intermediairid)
                    ->addWhere('a.napplicantid NOT IN (select c.napplicantid from tblcontract c WHERE c.dstart BETWEEN ? AND ? OR c.dend BETWEEN ? AND ?)', array($this->tbljobavailable->getFirst()->dday, $this->tbljobavailable->getLast()->dday, $this->tbljobavailable->getFirst()->dday, $this->tbljobavailable->getLast()->dday));

but somehow it keeps complaining: Couldn't find class c

Any ideas?

1

There are 1 best solutions below

0
Donatas Olsevičius On

Just had this issue a few days ago.

One of the possible solutions is adding a one to one relation for a tblapplicant table itself. I did not like this one, so just created additional query to get ids for exclusion. In your case that would be like this:

$notIn = Doctrine_Query::create()->(put your subselect query here)->execute(array(), Doctrine_Core::HYDRATE_SINGLE_SCALAR);

$Query = Doctrine_Query::create()
                ->select('a')
                ->from('tblapplicant a')
                ->innerJoin('a.tblintermediair i')
                ->where('i.nintermediairid = ? ', $intermediairid)
                ->whereNotIn('a.napplicantid', $notIn);