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

    I have a website that I paid a developer to help me with a small part of.
    Unfortunately what he’s done is not supported in the upgrade to 2.1 & I didn’t know enough to spot it. And he suggests rolling back to the previous version as a solution, whereas I would like to get the code updated so I can keep the site updated.

    Can anyone help me with the xPDO replacement for the following code?

    $imgTVres = $modx->db->select(’value’, $tableVars, "contentid = $docId AND tmplvarid = $tvImageId");
    $imgTotal_rows = $modx->db->getRecordCount($imgTVres);

    thanks,
    Rachel
      • 28215
      • 4,149 Posts
      $c = array(
          'contentid' => $docid,
          'tmplvarid' => $tvImageId,
      );
      $imgTVres = $modx->getCollection('modTemplateVarResource',$c);
      $imgTotal_rows = $modx->getCount('modTemplateVarResource',$c);
      


      Then you can loop over the $imgTVres array...remember though they are xPDOObjects, not arrays, so use ->get(’value’) instead of [’value’] when looping.
        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 36575
        • 104 Posts
        Thanks so much for the help splitting red. Unfortunately I don’t know enough about php to know what to do with this.

        Here’s the full snippet, in case someone has some time to tell me what is wrong!

        Cheers, Rachel

        <?php
        $phpThumbOptions = 'w=210&h=220&f=jpg';
        
        // end of settings
        $output = '';
        
        if ($modx->user->hasSessionContext($modx->context->get('key')) )
        {	
        	$user_id       = $modx->user->get('id');
        	$tableVars = $modx->getFullTableName('site_tmplvar_contentvalues'); 
        	$tvImageId = 21;
        	$tvProdDescrId = 20;
        	$wishListDocId = $modx->documentIdentifier;	
        	
        	if (!empty($_GET['deleteId']) and intval($_GET['deleteId']))
        	{
        		$deleteId = intval($_GET['deleteId']);
        		$sql= "DELETE FROM `modx_wishlist` WHERE user_id = {$user_id} AND content_id = {$deleteId} LIMIT 1";
        		
        		if ($modx->exec($sql))
        			$message = 'Item removed from your wishlist';
        		else
        			$message = 'Can\'t remove this item from your wishlist. Please try later';
        			
        		$output .= '<div class="message">'.$message.'</div>';
        	}
        	
        	$sql= "SELECT content_id FROM `modx_wishlist` WHERE user_id = {$user_id}";
        	if ($stmt = $modx->prepare($sql) 
        	and $stmt->execute() 
        	and $result = $stmt->fetchAll(PDO::FETCH_ASSOC))
        	{
        		$output .= '<div>';
        		foreach ($result as $n => $val){
        			$output .= '<div class="column grid_3 listing">';
        			$docId = $val['content_id'];
        			$title = ''; $parameters=array(); $imgSrc = ''; $descrSrc = '';
        
        			$parameters = array(
        				'id' => $docId,
        				'field' => 'pagetitle',
        			  );
        
        			$title = $modx->runSnippet('getResourceField',$parameters);
        			$title = ($title != '') ? $title : $docId;
        
        			$imgTVres = $modx->db->select('value', $tableVars, "contentid = $docId AND tmplvarid = $tvImageId");
        			$imgTotal_rows = $modx->db->getRecordCount($imgTVres);
        			if($imgTotal_rows >= 1 ) {
        				$row = $modx->db->getRow($imgTVres);
        				$imgSrc = $row['value'];
        				if ($imgSrc != '') $imgSrc = '<img src='.$modx->runSnippet("phpthumbof",array('input' => $imgSrc, 'options' => $phpThumbOptions)).' title="'.$title.'" class="imgborder"/>';
        			}   
          
        			$descrTVres = $modx->db->select('value', $tableVars, "contentid = $docId AND tmplvarid = $tvProdDescrId");
        			$descrTotal_rows = $modx->db->getRecordCount($descrTVres);
        			if($descrTotal_rows >= 1 ) {
        				$row = $modx->db->getRow($descrTVres);
        				$descrSrc = $row['value']; 
        			} 
        			$output .= '<h5 class="h5wish"><a href="'.$modx->makeUrl($docId).'">'.$title.'</a></h5>
        						  <a href="'.$modx->makeUrl($docId).'"><div class="p-image">'.$imgSrc.'</div></a><br />
        						  <div class="p-description"><p>'.$descrSrc.'</p></div>
        
        <a href="'.$modx->makeUrl($docId).'" class="button3"><span id="buttonText">View item</span></a></h5>
        
        <a href="'.$modx->makeUrl($wishListDocId,'','&deleteId='.$docId).'" class="button3"><span id="buttonText">Delete from wishlist</span></a>
        
        				    ';
        			$output .= '</div>';
        		}
        		$output .= '</div>';		
        	}
        	else
        		$output .= 'Your wishlist is empty';
        }
        else
        	$output .= 'Login to view your wishlist';
        
        return $output;
          • 32316
          • 387 Posts
          modx->db are gone

          check out this page http://rtfm.modx.com/display/revolution20/Summary+of+Legacy+Code+Removed+in+2.1

          Sorry I do not have more time right now
            • 3749
            • 24,544 Posts
            In order to use xPDO with your modx_wishlist table, you would have to create a schema and class and map files for it. This would be one way to do that: http://bobsguides.com/custom-db-tables.html.

            Then, you could use the xPDO methods described here: http://bobsguides.com/revolution-objects.html.

            As an alternative, you could just use PDO, which still works in the new version.
              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