We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38666
    • 98 Posts
    MODX Revolution 2.2.4-pl (traditional)
    Struggling xPDO newbie again. I have created a model and generated the classes and map for this table via xPDO. The A-Z list (https://forums.modx.com/thread/81854/xpdo-help-with-select-distinct#dis-post-451623) working but now am having trouble with the second stage where I am attempting to use the letter generated in the A-Z list to list the members with surnames beginning with a letter

    How do I use a substring like Left(Surname,1) with getCollection? Is the use of getCollection appropriate for this use?

    I have this working:
    $output = '';
    $sql = "SELECT * FROM csaac_members WHERE LEFT(Surname,1)= 'A' ";
    $csaac = $modx->query($sql);
    
    foreach ($csaac as $row) {
        $output .= $row['Surname'] .'<br/>';
    }
    }
    return $output;

    to produce a list like
    Alam
    Alde
    Armstrong
    Asgahari
    Ashton 

    But when I try to do something like this to produce a full listing for each member
    $output = '';
    $csaacP = "A";
    $csaacmember = $modx->getCollection('Members',array('Left(Surname,1)'=>$csaacP));
    foreach ($csaacmember as $row) {
     $fields = $row->toArray();
    $output .= $modx->getChunk('MemberProv', $fields);
    }
    }
    return $output;


    I get a blank screen (the chunk works with a query delivering same data from a different collection). All suggestions welcome.

    This question has been answered by dunnock. See the first response.

      Give a man the answer, and he’ll only have a temporary solution. Teach him the principles that led you to that answer, and he will be able to create his own solutions in the future.
    • discuss.answer
      • 42602
      • 81 Posts
      Am not 100% sure but change the array so that it is not key => value pair to

      array("Left(Surname,1) = '{$csaacP}'")


      Notice that this is unsafe as no escaping if you have user input for it. That works with the query object where() method, so it should work with the getCollection also
        • 38666
        • 98 Posts
        Thanks dunnock. As usual it's all in the syntax!
          Give a man the answer, and he’ll only have a temporary solution. Teach him the principles that led you to that answer, and he will be able to create his own solutions in the future.