symfony-users  

[symfony-users] Re: unexpected sql from criteria...

Kiril Angov
Thu, 04 Oct 2007 04:36:44 -0700

Jonathan Franks wrote:
> Thanks, you're right. I managed to get the result I wanted by combining 
> criterion. From the manual...
>
> You cannot combine Criteria objects directly, but you can combine 
> Criterion objects in order to specify logical relationships between clauses.
>
> And the code...
>
>     $c = New Criteria;
>
>     
>
>     $criterion_1 = $c->getNewCriterion(ItemPeer::GOANTIQUES, 1);
>
>     $criterion_2 = $c->getNewCriterion(ItemPeer::PUBLISHED, 1);
>
>     $criterion_2->addAnd($c->getNewCriterion(ItemPeer::SOLD, 0));
>
>     
>
>     $criterion_1->addOr($criterion_2);
>
>     
>
>     $c->add($criterion_1);
>
>     
>
>     $this->items = FeedPeer::doSelectFeed($c, null, 'GoAntiquesItem');
>
>
> Thanks for your help.
>
>
>
> Kiril Angov wrote:
>   
>> Jonathan Franks wrote:
>>   
>>     
>>> my code...
>>>
>>>     $c = New Criteria;
>>>
>>>     $criterion = $c->getNewCriterion(ItemPeer::PUBLISHED, 1);
>>>
>>>     $criterion->addAnd($c->getNewCriterion(ItemPeer::SOLD, 0));
>>>
>>>     $c->add($criterion);
>>>
>>>     $c->addOr(ItemPeer::GOANTIQUES, 1);
>>>
>>>     $this->items = FeedPeer::doSelectFeed($c, null, 'GoAntiquesItem');
>>>
>>>
>>> I'm expecting ....
>>>
>>> ..... WHERE (holloware_item.PUBLISHED=1 AND holloware_item.SOLD=0) *OR* 
>>> holloware_item.GOANTIQUES=1
>>>
>>> but I'm getting ...
>>>
>>> ..... WHERE (holloware_item.PUBLISHED=1 AND holloware_item.SOLD=0) *AND* 
>>> holloware_item.GOANTIQUES=1
>>>
>>> Can anyone see my mistake??
>>>
>>>
>>>
>>>     
>>>   
>>>     
>>>       
>> It is not a mistake but how propel works. I also thought that writing 
>> code like yours I will get an OR but $c->addOr() is when you want to 
>> include a rule about the same database field. By default $c->add() will 
>> overwrite the previous rules if the first argument (the table field)
>>  is the same, thus $c->addOr will make an OR statement for this field. 
>> The same is true for $c->addAnd() but you thought it works correct 
>> because by default it uses AND between fields, but the purpose of 
>> $c->addAnd is when you define different criteria for the same field and 
>> you want them connected with and AND. Also why are you using Criterion 
>> object directly? Your code can look like:
>>
>> $c = new Criteria();
>> $c->add(ItemPeer::PUBLISHED, 1);
>> $c->add($c->getNewCriterion(ItemPeer::SOLD, 0)
>> $c->add(ItemPeer::GOANTIQUES, 1);
>> $this->items = FeedPeer::doSelectFeed($c, null, 'GoAntiquesItem');
>>
>>
>> The code above will achive the same results. Now, to answer your quest, 
>> I faced this problem some time ago but do not remember if I found I work 
>> around or found a right way to do it so I hope somebody else can give 
>> you the right way to achieve that.
>>
>> Kupo
>>
>>     
>>   
>>     
>
> >
>
>   
Thanks for the follow up, I will keep your findings in mind the next 
time I need to do that (or I can just switch to Doctrine ;))

Kupo

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---