We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35471
    • 9 Posts
    echo "<pre>";
    $c = $modx->newQuery('modx_site_content');
    $c->select('DISTINCT '.$modx->getSelectColumns('modx_site_content','','',array('id','pagetitle')));
    $c->where(array(
          'id' => 1,
    ));
    $result = $modx->getCollection('modx_site_content', $c);
    var_dump($result);

    This is the code I am attempting to use, to extract page title and id from modx’s own site content. But I only receive NULL as a response, I have tried several different things and nothing seems to work, so I am hoping you guys can help me? smiley
      • 4172
      • 5,888 Posts
      I’m not sure what you exactly want todo.

      If you want to get the fields of Resource with id 1 then try this:

      $resource = $modx->getObject('modResource',1);
      $result = $resource->toArray();
      
      echo "<pre>";
      var_dump($result);
      echo "</pre>";
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 35471
        • 9 Posts
        I am trying to obtain id and pagetitle from the table modx_site_content in the database where the id = 1, but I only get NULL in return. As far as I have been able to read from the sparse documentation there is, that should be the way it is done, it just doesn’t work?
        • To work with objects you need to select all the columns that define the object like class_key, context_key, id (primary key), etc. If you just want raw column values back from the table, you do not need to use objects however.

          To use objects though, here is how it would be done.
          <?php
          $c = $modx->newQuery('modResource', array('id' => 1));
          $c->select($modx->getSelectColumns('modResource','','',array('id', 'pagetitle', 'class_key', 'context_key')));
          $collection = $modx->getCollection('modResource', $c);
          foreach ($collection as $object) {
              var_dump($object->get(array('id', 'pagetitle')));
          }
          

          Note that this would only ever return one object though, since you are selecting it where the primary key id = 1.

          Also note, you never want to var_dump a collection of xPDOObjects (there are references that will recurse) or even an xPDOObject; use the toArray() method or get() method to output specific column values.

          Without instantiating an object for each row, you can do this:
          <?php
          $c = $modx->newQuery('modResource', array('deleted' => false));
          $c->select($modx->getSelectColumns('modResource','','',array('id', 'pagetitle')));
          if ($c->prepare() && $c->stmt->execute()) {
              while ($row = $c->stmt->fetch(PDO::FETCH_ASSOC)) {
                  var_dump($row);
              }
          }
          
            • 35471
            • 9 Posts
            Thanks a lot, that really helped, the documentation I could find doesn’t exactly point out that those values are needed or why. smiley