1
jmass
Criteria Compo...
  • 2004/10/27 17:17

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


A big thanks to Mith and Ackbarr in pointing this poweful feature out to me.

A couple of concerns:

I build a query with the Criteria Object that comes out like this:
Quote:
WHERE (ticket.StatusID = 1 OR ticket.StatusID = 2 OR ticket.StatusID = 3 OR ticket.StatusID = 4 OR ticket.StatusID = 5 AND ticket.EntryDate >= '2004-10-17' AND ticket.EntryDate <= '2004-10-17')


I need it to look like this:
Quote:
WHERE (ticket.StatusID = 1 OR ticket.StatusID = 2 OR ticket.StatusID = 3 OR ticket.StatusID = 4 OR ticket.StatusID = 5) AND ticket.EntryDate >= '2004-10-17' AND ticket.EntryDate <= '2004-10-17'


Because if the parenthesis is not moved to close the OR statements, it ruins the filtering of the rest of the query.

Here is the whole query:
Quote:
$sql = "SELECT ticket.TicketID , users.uname , status.StatusName , client.ClientName , category.CategoryName , ticket.EntryDate , SUM(lineitems.LineItemHours) AS TicketTotalHours FROM obsps2_obsclient_tt_ticketdata AS ticket LEFT JOIN $lineitems_table AS lineitems ON lineitems.ParentTicketID = ticket.TicketID LEFT JOIN $client_table AS client ON client.ClientID = ticket.ClientID LEFT JOIN $status_table AS status ON status.StatusID = ticket.StatusID LEFT JOIN $category_table AS category ON category.CategoryID = ticket.CategoryID LEFT JOIN $users_table AS users ON users.uid = ticket.UID WHERE (ticket.StatusID = 1 OR ticket.StatusID = 2 OR ticket.StatusID = 3 OR ticket.StatusID = 4 OR ticket.StatusID = 5) AND ticket.EntryDate >= '2004-10-17' AND ticket.EntryDate <= '2004-10-17' GROUP BY ticket.TicketID ORDER BY EntryDate";


How do I get Criteria to close the parenthesis? Do I need more than one object? If so how do I build without the "WHERE" on the second instance of the object?

Any pointers would be much appreciated.

2
jmass
Re: Criteria Compo...
  • 2004/10/27 17:54

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


OK... I figured is out.

Instead of renderWhere I just used render. Creating multiple instances of the object works great now.

What a powerful feature to do flexible queries.

Thanks again,

3
Mithrandir
Re: Criteria Compo...

Each CriteriaCompo encloses its Criteria objects in a parenthesis - but you can add several CriteriaCompo's to one "super" CriteriaCompo and use one renderWhere() on that "super" object

4
jmass
Re: Criteria Compo...
  • 2004/10/27 21:25

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


That is what I figured out. Thaks for the better explanation of it.

Currently I have a static first portion of my query and the Criteria Compo fills in the WHERE on back. It is very flexible.

Can I do better? Are their construct to add flexibility to the first portion of the SQL statement?

5
jmass
Re: Criteria Compo...
  • 2004/10/27 22:34

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


I have run into another stumpper.

I pass several vars in an array via GET.

I know the array is comming through.

On my PHP page I have:

Quote:

// GET Vars
$Users = $_GET['users'];

// Build Objects
$crit_user = new CriteriaCompo();

// loop over it and print array elements
foreach ($Users as $a)
{
echo $a;
$addfilter = new Criteria('ticket.UID', $a, '=');
$crit_user->add($addfilter, 'OR');
}
echo <br>;
echo $crit_user->render();


The echo is there for debugging. When I pass this url
Quote:
er_ticket.php?userall=3&users%5B%5D=1&users%5B%5D=2&users%5B%5D=3&users%5B%5D=4


I get this result:
Quote:
1234
(ticket.UID = 4 OR ticket.UID = 4 OR ticket.UID = 4 OR ticket.UID = 4)


I can see that my vars (1,2,3,4) are being passed via GET. The array is being run through by the foreach loop... but no joy on the CriteriaCampo.

I am very stumpped. Any help is appreciated.

Thanks,

6
jmass
Re: Criteria Compo...
  • 2004/10/27 23:04

  • jmass

  • Friend of XOOPS

  • Posts: 524

  • Since: 2003/12/18


I fixed it, but still can not see why my original code did not work....

For those that care (and my future ref) here is the working code
Quote:
foreach ($Users as $a)
{
$crit_user->add(new Criteria('ticket.UID', $a, '='), 'OR');
}

Login

Who's Online

155 user(s) are online (84 user(s) are browsing Support Forums)


Members: 0


Guests: 155


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: May 31
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits