Re: DataObjects: joinAdd problem
| From: | Justin Patrin | Date: | Mon, 24 Jan 2005 17:52:00 +0000 |
| Subject: | Re: DataObjects: joinAdd problem | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-17132@lists.php.net to get a copy of this message | ||
On Mon, 24 Jan 2005 13:01:17 +0100, Toni <toni@pangea.org> wrote:
> Hi,
>
> i'm coding a search page with DB_DO and I have a problem.
>
> My tables are:
> Noticia: (id, titulo, ...); Tema: (id, ...); Not_Tema: (idn,idt);
>
> and my .links.ini:
> [Not_Tema]
> idn = Noticia:id
> idt = Tema:id
>
> when I search from within Noticia class:
>
> $this->selectAdd('Noticia.id,titulo');
> $this->orderBy('titulo');
>
> // search Noticias with a word within field "titulo":
> if ($palabra!=''){
> $this->whereAdd("(titulo LIKE '%".$palabra."%')");
> }
>
> // but only these ones that have a pair (idn,$v) in Not_Temas:
> if (!empty($temas)) {
> foreach ($temas as $v) {
> $nt = new DataObjects_Not_Tema;
> $nt->idt = $v;
Instead of the above line, use whereAdd on $this and tell it to use OR
instead of AND. Alternatively, you could just create the whole OR SQL
yourself and whereAdd it after the loop. Or, my personal favorite,
create an SQL list ('1', '2', '3') and use IN. "col IN
('1', '2',
'3')" etc.
> $this->joinAdd($nt,"");
> }
> }
>
> With only one value $v on pairs, this works, but how can I make that
> works with more $v?
>
> This code generate ($v=3,$v=4): (DBDO error)
> WHERE (titulo LIKE '%Salvador%')
> AND Not_Not_Tema.idn=Not_Noticia.id AND Not_Not_Tema.idt = 3
> AND Not_Not_Tema.idn=Not_Noticia.id AND Not_Not_Tema.idt = 4
>
> while I expect:
> WHERE (titulo LIKE '%Salvador%')
> AND (Not_Not_Tema.idn=Not_Noticia.id AND Not_Not_Tema.idt = 3
> OR Not_Not_Tema.idn=Not_Noticia.id AND Not_Not_Tema.idt = 4)
>
> Can I do this with joinAdd?
> If not, how can I do? Only with plain sql?
>
> Thanks in advance for your help.
> Toni.
>
--
Justin Patrin