We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 16348
    • 64 Posts
    Hi

    I have two custom tables with a defined relationship. In the first table, called Categories, I store categories for running competitions. In the second table, called Results, I store the acutal competitors. The field that links the tables together is called categoryid.
    We have a voucher system combined with credit card payments. To sort out the compeitors who has completed their payments to the race I want to use this criteria:
    WHERE `Results`.`voucher` != ’’ AND `Results`.`voucher` NOT LIKE ’INVALID’ OR `Results`.`paymentref` != ’’

    To clarify, I want to retrieve the competitors who either has a voucher or a paymentref. I want to keep track of the people who tries to enter an invalid voucher and keeps track of them by enter INVALID in the voucher field.

    To count all competitors I use this query:
    $c = $modx->newQuery('Results');
    $c->where(array(
      'voucher:!=' => ''
    ));
    $c->andCondition(array(
      'voucher:!=' => 'INVALID'
    ));
    $c->orCondition(array(
      'paymentref:!=' => ''
    ));
      
    $numEntrants = $modx->getCount('Results',$c);
    


    This works and I get the correct results.

    Now to my problem. When I want to iterate over all my categories to output a list over all competitors per category the above criteria doesn’t work. Here is my code:
    $c = $modx->newQuery('Results');
    $c->where(array(
      'voucher:!=' => ''
    ));
    $c->andCondition(array(
      'voucher:!=' => 'INVALID'
    ));
    $c->orCondition(array(
      'paymentref:!=' => ''
    ));
      
    $categories = $modx->getCollection('Categories');
    foreach ($categories as $category) {
      $competitors = $category->getMany('Entries',$c);
      foreach ($entrants as $entrant) {
        ...
    


    This code seems to destroy the relationsship in some way.

    I have tried different solutions, for example this one, which doesn’t work either:
    $c = $modx->newQuery('Results');
    $c->where(array(
      'voucher:!=' => '',
      'AND:voucher:!=' => 'INVALID',
      'OR:paymentref:!=' => '',
    ));
    

    In this case only one of the conditions for the ticket field is used. The above solutions doesn’t work whit getCount either.

    Can anyone see what I’m doing wrong?

    Best regards
    Kristoffer
      • 6228
      • 249 Posts
      I see a could issues right away:
      $categories = $modx->getCollection('Categories', $c);

      The criteria was missing from the statement

      foreach ($categories as $category) {
        $competitors = $category->getMany('Entries',$criteria);
        foreach ($entrants as $entrant) {
      

      What is [tt]$criteria[/tt]? Also, while this design pattern may work, it is horribly inefficient. You may be better off building a single request with joins, or use [tt]getCollectionGraph[/tt] that will give you nested objects in a single query.

      To help pinpoint what is going on, you can always (temporarily) add this right after your criteria:

      $c->prepare();
      $modx->log(modX::LOG_LEVEL_ERROR, $c->toSQL());

      After execution, you can check the modx logs and see the exact SQL statement(s) which often makes debugging easier.
        lo9on.com

        MODx Evolution/Revolution | Remote Desktop Training | Development
        • 16348
        • 64 Posts
        cyclissmo: You found a typo in my post, i shortened the variable name to make it more readable in my post. I have edited the code in the post.

        I will use the error log to see what is happening and try to figure it out.

        I maybe have to look at getCollectionGraph as well, but I thought that i should learn the basics of xpdo first smiley