We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 17499 ☆ A M B ☆
    • 872 Posts
    @Sottwell
    Where is the problem with dependancies ?

    @TimGS
    You didn’t save the chunk back in the database.
    Now you need more lines to update the chunk using the DBAPI.

      • 30023
      • 172 Posts
      Quote from: lossendae at Apr 30, 2011, 05:26 AM

      @TimGS
      You didn’t save the chunk back in the database.
      Now you need more lines to update the chunk using the DBAPI.

      Ooops - I ’coded’ it the wrong way round i.e. chunk to file.

      The right way round (I hope grin) in Evo, albeit untested, in two lines (though it could be a one-liner):

      <?php
          $chunk = file_get_contents(MODX_BASE_PATH . 'components/spform/banlist.inc.php');
          $modx->db->insert(array('name'=>'spfbanlist', 'snippet'=>$chunk), $modx->getFullTablename('site_htmlsnippets'));
      ?>


      I’m still not seeing how learning xPDO is going to help the coding of my websites.

      -- Tim.

        • 18373 ☆ A M B ☆
        • 3,141 Posts
        Few example that may show why it’s more readable & easy to make complex queries that are easy to maintain.. IMO it’s not about the absolute length of a script, as regular SQL syntax would be shorter most of the time, but Revo also offers that through $modx->query($sql_here).

        Assume $_POST[] is filled with resource field names and the value they should be set to.

        // Make new object from POST
        $doc = $modx->newObject('modResource');
        $doc->fromArray($_POST); //You should NEVER do this - validate it first! But just sayin'
        $doc->save();



        // Script which lists resources based on $_POST values passed
        $c = $modx->newQuery('modResource');
        
        // Check if there's a parent value set to use. 
        if (($_POST['parent'] != 0) && (empty($_POST['parent']) && (is_numeric($_POST['parent'])) { 
          $c->where(array('parent' => $_POST['parent']; 
        }
        // Basicly limitating to some stuff we always want
        $c->where(array(
          'published' => 1,
          'hidemenu' => 0,
          'searchable' => 1,
          'limit' => (is_numeric($_POST['limit'])) ? $_POST['limit'] : 5));
        
        // Check some sort values
        $sortby = ($_POST['sortby']) ? $_POST['sortby'] : 'menuindex';
        $sortorder = ($_POST['sortorder']) ? $_POST['sortorder'] : 'asc';
        $c->sort($sortby,$sortorder);
        
        // Get matching resources
        $res = $modx->getCollection('modResource',$c);
        
        // Loop over them and output
        $o = '';
        foreach ($res as $cur) {
          $fields = $cur->toArray();
          $fields['tv'] = $cur->getMany('TemplateVars')->toArray();
          $o .= $modx->getChunk('loopingtpl',$fields);
        }
        
        return $o;

        Not tested and not even barely coming near what xPDO is capable of. getCollectionGraph would be a good example, which is somewhat a shortcut for joins (which you can also just use as $c->innerJoin() by the way).

        I’m bad at coming up with examples when I want to show some. tongue
          Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

          Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
          • 4172
          • 5,888 Posts
          Quote from: TimGS at May 01, 2011, 06:08 AM

          Quote from: lossendae at Apr 30, 2011, 05:26 AM

          @TimGS
          You didn’t save the chunk back in the database.
          Now you need more lines to update the chunk using the DBAPI.

          Ooops - I ’coded’ it the wrong way round i.e. chunk to file.

          The right way round (I hope grin) in Evo, albeit untested, in two lines (though it could be a one-liner):

          <?php
              $chunk = file_get_contents(MODX_BASE_PATH . 'components/spform/banlist.inc.php');
              $modx->db->insert(array('name'=>'spfbanlist', 'snippet'=>$chunk), $modx->getFullTablename('site_htmlsnippets'));
          ?>


          I’m still not seeing how learning xPDO is going to help the coding of my websites.

          -- Tim.



          this example isn’t complete.

          what would you say is easier and more elegant, more intuitiv to code, easier to read once you know how xpdo works:
          A:
          <?php
            
              $chunk = file_get_contents(MODX_BASE_PATH . 'components/spform/banlist.inc.php');
              $result = $modx->db->select( '*', $modx->getFullTablename('site_htmlsnippets'), 'name="spfBanlist"');
              if( $modx->db->getRecordCount( $result ) >= 1 ) {
                  $modx->db->update(array('snippet'=>$chunk), $modx->getFullTablename('site_htmlsnippets'),'name="spfBanlist"');   
              }    
              else{
                  $modx->db->insert(array('name'=>'spfbanlist', 'snippet'=>$chunk), $modx->getFullTablename('site_htmlsnippets'));       
              }
              
              
          ?>


          B:
          <?php
              $chunkObj = $modx->getObject('modChunk', array('name'=>'spfBanlist'));
              if (empty($chunkObj)){
                 $chunkObj = $modx->newObject('modChunk');
                 $chunkObj->set('name','spfBanlist'); 
              }
              $fName = MODX_CORE_PATH . 'components/spform/banlist.inc.php';
              $chunkObj->setContent(file_get_contents($fName));
              $chunkObj->save();
              
          ?>
          


          my favorite is B.
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 30023
            • 172 Posts
            Quote from: Bruno17 at May 01, 2011, 07:03 AM

            this example isn’t complete.

            what would you say is easier and more elegant, more intuitiv to code, easier to read once you know how xpdo works:
            A:
            <?php
              
                $chunk = file_get_contents(MODX_BASE_PATH . 'components/spform/banlist.inc.php');
                $result = $modx->db->select( '*', $modx->getFullTablename('site_htmlsnippets'), 'name="spfBanlist"');
                if( $modx->db->getRecordCount( $result ) >= 1 ) {
                    $modx->db->update(array('snippet'=>$chunk), $modx->getFullTablename('site_htmlsnippets'),'name="spfBanlist"');   
                }    
                else{
                    $modx->db->insert(array('name'=>'spfbanlist', 'snippet'=>$chunk), $modx->getFullTablename('site_htmlsnippets'));       
                }
                
                
            ?>


            B:
            <?php
                $chunkObj = $modx->getObject('modChunk', array('name'=>'spfBanlist'));
                if (empty($chunkObj)){
                   $chunkObj = $modx->newObject('modChunk');
                   $chunkObj->set('name','spfBanlist'); 
                }
                $fName = MODX_CORE_PATH . 'components/spform/banlist.inc.php';
                $chunkObj->setContent(file_get_contents($fName));
                $chunkObj->save();
                
            ?>
            

            my favorite is B

            Oh hang on a minute - the original example from BobRay was ’minus a few sanity checks’ - which being unfamilar with SPForm may well include things that will be unknown to me. Sure, your code is obviously more complete (*) even to someone with my level of Revo/xPDO/SPForm knowledge, but don’t starting now introducing the checks that (to you (**)) penalise Evo - either give full and complete code or stick with the bare bones - no picking and choosing please!

            (*) I say ’more’ because you omit to consider what happens if a chunk already exists called spfbanlist that is not the data that is being imported - shouldn’t you inform the user before overwriting? Like I said, either a barebones example or a full solution, not a halfway house designed to show the perceived advantages of Revo.

            (**) I say ’to you’ because I am still seeing little if any difference.

            -- Tim.
              • 18373 ☆ A M B ☆
              • 3,141 Posts
              Didn’t see this mentioned yet - but the xPDO layer allows database abstraction which makes it able of working on different database systems. 2.1 also supports sqlsrv next to mysql.

              Also - the underlying database structure can me modified to be more efficient / make more sense, while keeping a clean upgrade path if you use the recommended object approach.


              Probably the biggest reason to learn/use xPDO is because it’s OOP - Object Oriented Programming. You can do it without, but there are some distinct advantages on it that Google can tell you about better than I can.
                Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

                Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
                • 4172
                • 5,888 Posts
                Sorry, I don’t want to penalise the one or the other. Both are great.
                I just want to help you with your Decision.

                You and Bob did different things with your code.
                Bob is updating an existing record.
                You are inserting a new one.

                So I combined both and made examples for both systems to show you the differences and added my favorite.

                Its up to you, what you choose at the end.

                Sorry again.
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 30023
                  • 172 Posts
                  Quote from: Mark at May 01, 2011, 07:56 AM

                  Didn’t see this mentioned yet - but the xPDO layer allows database abstraction which makes it able of working on different database systems. 2.1 also supports sqlsrv next to mysql.

                  IIRC the structure is there in the Evo code core for a non-MySQL DBAPI. Probably the reason that no one has taken advantage of this though is the same reason that the ability to use non-MySQL databases is such a non-issue for the vast majority. Just how many here have had to build a website where they were not able (or even considered not using) MySQL?

                  Quote from: Mark at May 01, 2011, 07:56 AM

                  Also - the underlying database structure can me modified to be more efficient / make more sense, while keeping a clean upgrade path if you use the recommended object approach.

                  How would you want to modify the db structure? I’m not being rhetorical - though I admit to being skeptical as to any realistic practical advantage.

                  Quote from: Mark at May 01, 2011, 07:56 AM

                  Probably the biggest reason to learn/use xPDO is because it’s OOP - Object Oriented Programming. You can do it without, but there are some distinct advantages on it that Google can tell you about better than I can.

                  Whilst OOP has gained wide acceptance and I use it where appropriate - e.g. for using on colaborative projects for encouraging good working practices that are conducive to future robustness and ease of maintainence - its not a cure-all.

                  Again, whilst Google will indeed produce a vast quantity of articles (often involving no critical thought and often just copied/plagiarised) describing OOP as such a cure-all, it will also produce some more balanced original articles that whilst citing its advantages, cite its disadvantages (or simply just say why its not always the big deal its made out to be).

                  The majority of small/medium size websites rarely involve more than a small number of programmers (probably rarely involve more than one programmer) in addition to the designer/HTML/CSS coder. Furthermore future rebuilds are more than likely going to be complete ground-up rebuilds (I do not claim this to be always the best course of action - I am just saying that this happens). In such cases OOP is probably of little advantage.

                  Incidentally I do think the OOP core of Evo (and I presume Revo is similar) is worthwhile - in this case there are many thousands of programmers who use the MODx API (Evo or Revo), and hence the ’clerical’ aspects of the MODx core developers being able to decide just which methods/properties are public have clear advantages. Edit: Having took a quick look I’m not actually sure to what extent this aspect is utilised after all.

                  -- Tim.
                    • 30023
                    • 172 Posts
                    Quote from: Bruno17 at May 01, 2011, 08:39 AM

                    You and Bob did different things with your code.
                    Bob is updating an existing record.
                    You are inserting a new one.

                    Yes, accepted - fair enough... the perils of giving barebones examples of more complex code.

                    Sorry for any misunderstanding.

                    -- Tim.
                      • 6228
                      • 249 Posts
                      Quote from: TimGS at May 01, 2011, 06:08 AM

                      I’m still not seeing how learning xPDO is going to help the coding of my websites.
                      -- Tim.
                      Which may be precisely why you are arguing against it - because you have not worked with it as extensively as, say Bob or Mark for example. I abandoned Evo long ago after forcing myself to understand Revo, and realistically I see no comparison between the two models in terms of usability, functionality and overall cohesiveness.

                      But it’s precisely how Morpheus describes the Matrix: Unfortunately, no one can be told what MODx Revolution is. You have to see it for yourself. cool
                        lo9on.com

                        MODx Evolution/Revolution | Remote Desktop Training | Development