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

    In a getListProcessor I have to restrict the returned list based on an SQL condition that I'm not able to 'transform' in a correct xpdo queries. In SQL the syntax should be
    // first query
    SELECT * FROM table1 t1
    WHERE NOT EXISTS (
            SELECT 1 FROM table2 t2
            WHERE t1.id = t2.id
    )
    LIMIT 0 , 30
    
    

    any help would be appreciated
    Cheers [ed. note: anso last edited this post 14 years, 4 months ago.]
      • 22303 MODX Staff
      • 10,725 Posts
      Are these tables represented by model objects or are you just trying to get a raw result set? Some context might help us guide you more accurately.

      This would be the most direct way, if these are just custom tables without a model...
      $stmt = $modx->query("SELECT * FROM table1 t1
      WHERE NOT EXISTS (
              SELECT 1 FROM table2 t2
              WHERE t1.id = t2.id
      )
      LIMIT 0 , 30");
      $rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
        • 1778
        • 659 Posts
        They are represented by model Objects, let say myObj1 for table1 and myObj2 for table2
        as example
        <object class="myObj1" table="table1" extends="xPDOSimpleObject">
        <field key="name" dbtype="varchar" precision="100" phptype="string" null="false" default=""/>
        <composite alias="objects2" class="myObj2" local="id" foreign="object" cardinality="many" owner="local"/>
        </object>
        <object class="myObj2" table="table2" extends="xPDOSimpleObject">
        <field key="object" dbtype="int" precision="10" phptype="integer" null="false" default=""/>
        <field key="description" dbtype="varchar" precision="100" phptype="string" null="false" default=""/>
        <aggregate alias="objects1" class="myObj1" local="object" foreign="id" cardinality="one" owner="foreign"/>
        </object>
        

        Thanks for your help
        Cheers
          • 22303 MODX Staff
          • 10,725 Posts
          Then something like this maybe?

          $query = $modx->newQuery('myObj1', array(
              "NOT EXISTS (SELECT 1 FROM {$modx->getTableName('myObj2')} myObj2 WHERE myObj1.id = myObj2.id)"
          );
          $query->limit(30, 0);
          $myObj1 = $modx->getCollection('myObj1', $query);
          
            • 1778
            • 659 Posts
            Works like a charm, thanks a lot Opengeek, as usual your advices save a lot of my time ^_^
            Cheers