Basics

Share this on:

Get the id of the current resource

$modx->resource->id


Get User Info

$user = $modx->getUser();
echo $user->get('username');
Available fields:
id,username,password,cachepwd,class_key,active,remote_key,remote_data,hash_class,
salt,primary_group, session_stale, sudo

To find more useful information

$profile = $modx->user->getOne('Profile');
$fullname = $profile->get('fullname');
Available fields: fullname, email, phone, blocked, blockeduntil, blockedafter, logincount, lastlogin, thislogin
failedlogincount, sesionid, dob, gender, address, country, city, state, zip, fax, photo, comment, website, extended

Basic database query:

$query = 'select name from myTable where id = '.$id;
$result = $modx->query($query);
if (!is_object($result)) {
return 'No result!';
}
else {
$row = $result->fetch(PDO::FETCH_ASSOC);
return 'Result:' .print_r($row,true);
}
$stmt = $modx->query('SELECT * FROM `mytable` ORDER BY `name` ASC');
if ($stmt && $stmt instanceof PDOStatement) {
    while ($row= $stmt->fetch(PDO::FETCH_ASSOC)) {
        echo $row['name'];
    }
}

...

Accessing a custom db that you have generated the schema and map files for (possibly using CMP Generator

      // Install package<br>      
$path = MODX_CORE_PATH . 'components/myModel/';
$result = $modx->addPackage('myModel',$path .'model/','');
$rows = $modx->getCollection('MyModel');      
$output .= '<p>Total: '. count($rows ) . '</p>';
foreach($rows as $row) {
  $count = 0; 
  $output .= 'id: ' . $row->get('id');;           
  $output .= '<br/>name: ' . $row->get('name');          
  $output .= '<br/></p>';           
  $children = $row->getMany('My Other Model Class That Has A Many To One Relationship With The Above Class');           
  $output .= '<p>Children: '. count($children) . '</p>';
  foreach($children as $child) {                
         // do stuff <br>           
  }       
}      
return $output;

...

Write something to the Error Log

$modx->log(modX::LOG_LEVEL_ERROR,'Something... ');

Gotchas!

Formit

When putting in your public and private keys in the system settings make sure you put it in formit.recaptcha_private_key and formit.recaptcha_public_key and NOT in the recaptcha.private_key and recaptcha.public_key. That can keep you confused for a while!

Share this on:
Mike Nuttall

Author: Mike Nuttall

Mike has been web designing, programming and building web applications in Leeds for many years. He founded Onsitenow in 2009 and has been helping clients turn business ideas into on-line reality ever since. Mike can be followed on Twitter and has a profile on Google+.