We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18174
    • 116 Posts
    I searched the forum for days about a answer to execute a SELECT Statement over more than one table, i.e. return values from more than one table. In fact I have to make a feature made in Evo ready for Revo. There I have ONE query Statement which selects data from 10 tables related to eachother. I tried several examples from the tutorials and from the forum here but no success. It all ends up with a final getCollection call which is related to a certain data object in the schema.
    It seems to me that in xPDO the attempt is to have several small queries and put the logic more into script code than into a complex DB query.
    Could anybody confirm this? Then I can stop searching further.
    It's easy to execute it with $modx->query(). But for how long will the deprecated DBAPI live in MODX Revo?
    regards
    manu
      • 4172
      • 5,888 Posts
      did you allready create your xpdo-schema, if yes, can we see it?
      what was your mysql-query?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 22303 MODX Staff
        • 10,725 Posts
        Quote from: manu37 at Feb 20, 2013, 11:43 AM
        I searched the forum for days about a answer to execute a SELECT Statement over more than one table, i.e. return values from more than one table. In fact I have to make a feature made in Evo ready for Revo. There I have ONE query Statement which selects data from 10 tables related to eachother. I tried several examples from the tutorials and from the forum here but no success. It all ends up with a final getCollection call which is related to a certain data object in the schema.
        It seems to me that in xPDO the attempt is to have several small queries and put the logic more into script code than into a complex DB query.
        Could anybody confirm this? Then I can stop searching further.
        It's easy to execute it with $modx->query(). But for how long will the deprecated DBAPI live in MODX Revo?
        For getting related objects, there are many methods in xPDO, such as getCollectionGraph(). These refer to a relation graph that can be automatically loaded based on the defined relationships. So, no, it is not xPDO's attempt to break anything up into smaller queries with logic more into script code than into a complex DB query. You can generate very complex queries with the API and execute them using the object methods xPDO provides, or simply prepare a PDOStatement from it that you can execute and fetch results from manually. xPDO just wraps the functionality of PDO.

        Also, the DBAPI was removed from MODX in the 2.1 release. modX::query() is not a DBAPI method; it is an xPDO method that wraps the PDO::query() method, and it will not be deprecated anytime soon.
          • 18174
          • 116 Posts
          Hi Bruno, hi Jason

          Thanks for your replies.
          I attached a DB Schema as pdf, the query statement and the schema.xml.
          In the schema I just started to connect the two tables KundenKonfig and KundeLob to play with getCollectionGraph and sort of.
          I stopped trying after memory exhausts, time outs but no error log entries at all. Is there a handy debug option?
          I also tried to attempt to solve it with process logic:
          <?php
          $kundenKonfig = $modx->getObject('KundenKonfig',array('usergroup'=>$usergroup));
          $kundenKonfig->set('vk_jaehrlich',0);  //just to get along with my query stmt.
          
          foreach($kundenKonfig->getMany('fkKundeLob') as $kundeLob) {
            
            foreach($kundeLob->getMany('fkKundeLobTarif') as $kundeLobTarif) {
              $tarif = $kundeLobTarif->getOne('fkTarif'); 
              
          
              foreach($tarif->getMany('fkTarifrolle','Tarifrolle.gueltig_ab<=CURDATE()') as $tarifrolle) {//don't know how to set the filter here? Additionally I have to find the "youngest" record (timestamp). How do I control the order the collection?
              
                  foreach($tarifrolle->getMany('fkTarifrollenSequenz') {
               ....

          I didn't test the code yet. Just a little practise to get comfortable with xPDO..
          regards
          manu
            • 18174
            • 116 Posts
            The advantage of a monster sql statement is the nice interface to test/discuss with db developers/designers.
            The disadvantage is the poor readability without program logic.
            For practising and to bring light into logic I made the xPDO approach:
            <?php
            $modx->addPackage('WPCalc', MODX_CORE_PATH . 'components/WPCalc/model/','modx_');
            
            $kundenKonfig = $modx->getObjectGraph('KundenKonfig','{KundeLobs:{Lob:{LobLeistungsarts:{Leistungsart:{}}}}}',array('id' => 1));
              var_dump('kundenKonfig',$kundenKonfig->toArray());
            
              foreach($kundenKonfig->KundeLobs as $kundeLob){
                var_dump('kundeLob',$kundeLob->toArray());
                
                $kundeLobTarif = $kundeLob->getOne('KundeLobTarif'); 
                $tarif = $kundeLobTarif->getOne('Tarif');
                var_dump('tarif',$tarif->toArray());
            
                $query = $modx->newQuery('Tarifrolle');
                $query->where(array('Tarifrolle.tarif'=>$tarif->get('id')));
                $query->andCondition(array('Tarifrolle.gueltig_ab < CURDATE()'));
                $query->sortby('Tarifrolle.timestamp','DESC');
                $tarifrolle = $modx->getObject('Tarifrolle',$query);   
                var_dump('tarifrolle',$tarifrolle->toArray());
              
                $lob = $kundeLob->getOne('Lob');
                var_dump('lob',$lob->toArray());
                
                
                foreach($lob->LobLeistungsarts as $lobLeistungsart){
                  var_dump('lobLeistungsart',$lobLeistungsart->toArray());
                  var_dump('Leistungsart',$lobLeistungsart->Leistungsart->toArray());
                  
                  $query = $modx->newQuery('TarifrollenSequenz');
                  $query->where(array('tarifrolle_id'=>$tarifrolle->get('id')
                                     ,'leistungsart'=>$lobLeistungsart->Leistungsart->get('leistungsart')));
                  var_dump('is_tarifrollenSequenz',count($modx->getObject('TarifrollenSequenz',$query)));
                }
            }
            return;