We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40758
    • 17 Posts
    I'm running into SQL issues using selfLink on IIS/MS SQL.
    I'm trying to debug the parts that's incompatible.

    Is there a way to get the $modx->newQuery object to show the query string it's running?
      • 3749
      • 24,544 Posts
      $criteria->prepare();
      echo $criteria->toSQL();


      To get even more information, I think you can do this:

      In your class constructor:
      $this->setDebug(true);
      


      /* query code and execution here */
      die();
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 40758
        • 17 Posts
        Thank you BobRay. That helped a lot.
        Turns out I couldn't have used selfLink on the blog pages for next/prev button, because the object doesn't not have menu index.
        Since the entries were not set in by chronological order, I can't use the id as well.
        All that's left is the getquery, with dates before/after current document's publishedon date, sort it, and limit by one.
        Though I can't get the date comparison right.
          • 40758
          • 17 Posts
          //get params
          $resource = $modx->getOption('id', $scriptProperties, $modx->resource->get('id'));
          $direction = $modx->getOption('direction', $scriptProperties, false);
          $tpl = $modx->getOption('tpl', $scriptProperties, false);
          $linktxt = $modx->getOption('linktext', $scriptProperties, false);
          
          
          if($res = $modx->getObject('modResource', $resource)) {
          	
          	$menuindex = $res->get('menuindex');
          	$parentid = $res->get('parent');
          	$c = $modx->newQuery('modResource');
          	$c->limit(1);
          	switch($direction) {
          		case 'next':
          		case 'prev':
          			$c->where(array('id:!=' => $resource));
          			$c->where(array('parent' => $parentid));
          			$c->where(array('published' => true));
          			$c->where(array('deleted' => false));
          			break;
          	}
          	switch($direction) {
          		case 'next':
          			//$c->where(array('publishedon:<' => 'date('.$res->get('publishedon').')' ));
                                  $c->where("[modResource].[publishedon] > CAST('" . $res->get('publishedon') . "' AS DATETIME) " , xPDOQuery::SQL_AND);
          			$c->sortby('publishedon','DESC');
          			break;
          		case 'prev':
          			//$c->where(array('publishedon:>' => 'date('.$res->get('publishedon').')' ));
          			$c->sortby('publishedon','ASC');
          			break;
          	}
          //debug
          //$c->prepare();
          //return 'SQL: ' . $c->toSQL();
          	if($linkResource = $modx->getObject('modResource', $c)) {
          		// build placeholders
          		$placeholders = array(
          			'id' => $linkResource->get('id'),
          			'pagetitle' => (!empty($linktxt)) ? $linktxt : $linkResource->get('pagetitle'), 
          			'longtitle' => (!empty($linktxt)) ? $linktxt : $linkResource->get('longtitle'),
          			'publishedon' => (!empty($linktxt)) ? $linktxt : $linkResource->get('publishedon'),
          			'menutitle' => (!empty($linktxt)) ? $linktxt : $linkResource->get('menutitle')
          		);
          		
          		// parse chunk
          		$chunk = $modx->getObject('modChunk', array('name' => $tpl));
          		if(!$chunk) {
          			$useChunk = '<a href="[[~[[+id]]]]">[[+menutitle:isempty=`[[+pagetitle]]`]]</a>';
          			$chunk = $modx->newObject('modChunk');
          			$chunk->setCacheable(false);
          			$chunk->setContent($useChunk);
          		}
          		return $chunk->process($placeholders);
          	}else{
          		return 'no object found';
          	}
          }
          
            • 40758
            • 17 Posts
            Brain is fried today, will see if there's any progress tomorrow to fix the publishedon date comparison part.

            CAST(), DATE() didn't seem to do any good.
              • 3749
              • 24,544 Posts
              You do know that you can drag the resources in the tree to set their menu indexes, right?

              Just drag them into the order you want to show them in and their menuindex fields will be renumbered (starting at 0, IIRC).


              I think you could also use one of these and call your snippet via getPage.

              $c->sortby('publishedon', 'ASC');
              $c->sortby('publishedon', 'DESC');
              



              Or just use getResources together with getPage.




              ------------------------------------------------------------------------------------------
              PLEASE, PLEASE specify the version of MODX you are using.
              MODX info for everyone: http://bobsguides.com/modx.html
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 40758
                • 17 Posts
                Thanks, but those document are "article" objects in a blog, which doesn't have menu index at all.
                If the object has menu index, or even entered/imported in chronological order, it would be so much easier.
                  • 40758
                  • 17 Posts
                  ran out of time, found no solution to compare date via newQuery.
                  Went brute force edit/re-entering all entries in chronological order by id...sigh...