Developing with xPDO - MODX Community Forums <![CDATA[Add rows inside Object itself]]>
I have a master Class "Event" that has a function that will add dates to a date table. Very simplified it looks like that:
class Event extends xPDOSimpleObject {
    public function addDate {
        $date = $this->modx->newObject('eventDate');
        $date->set('title', $title);

This works perfect and the addDate function adds the date to the table. Now i would like to move this function inside the eventDate Class. But this does not work anymore. Simplified it looks like that:
class eventDate extends xPDOSimpleObject {
    public function addDate {
        $this->set('title', $title);

I thought i could just add new dates like this. But this does not work. Is there a good example how to communicate via xPDO with the DB inside the Class/Object itself?
]]> Thu, 14 Mar 2019 04:15:39 +0000
<![CDATA[innerJoin with foreign database]]>
I'm working on moving some custom database tables into a foreign db instead of them being in the same database as my modx instance. So currently I have a copy of these tables in both places (locally and in the foreign db).

The problem i'm running into is that when I am trying to query data from the foreign db, innerJoins no longer seem to work as expected. Take the below code for example:

if($modx->getOption('use_foreign_database')) {
// $database_dsn, $foreign_database_user, $foreign_database_password variables are set here
    $db = new xPDO($database_dsn, $foreign_database_user, $foreign_database_password); 
    // Test connection
    $connected = ($xpdo->connect()) ? 'Connected' : 'Not Connected';
    $modx->log(xPDO::LOG_LEVEL_DEBUG, $connected);   

else {
    $db = $modx;
$package_path = MODX_CORE_PATH . 'components/dancers/model/';

$query = $db->newQuery('Payroll');
    $query->innerJoin('PayrollPayPeriod','PayrollPayPeriod',array("Payroll.pay_period_id ="));
    $query->select(array(', employee_id, sum(gross_income) as gross_income,, PayrollPayPeriod.pay_date'));
        'PayrollPayPeriod.pay_date:>=' => date($year . '-' . $month . '-01'),
        'PayrollPayPeriod.pay_date:<=' => date($year . '-' . $month . '-t')
    $payrolls = $db->getCollection('Payroll', $query);

 foreach ($payrolls as $payroll) {

       $modx->log(xPDO::LOG_LEVEL_DEBUG, $payroll->get('employee_id'));
       $modx->log(xPDO::LOG_LEVEL_DEBUG, $payroll->get('PayrollPayPeriod.pay_date'));  
       $modx->log(xPDO::LOG_LEVEL_DEBUG, $payroll->get('gross_income'));  

If $modx->getOption('use_foreign_database') is false, than the query will return the results as I expect and the 3 objects within the foreach will log correctly to the error log, however if $modx->getOption('use_foreign_database') is true (meaning it's pointing to the foreign db), than it will still query the Payroll db correctly and the employee_id will print to the error log, but the other two objects (the sum'd gross_income) and the PayrollPayPeriod.pay_date print out empty statements.

I'm not sure what i'm missing here as the package schema and table relationships in that schema have not changed, the only changing is if I'm connecting to the local db, vs the foreign...]]> Tue, 26 Feb 2019 11:14:29 +0000
<![CDATA[pdoFetch examples anyone?]]> Mon, 19 Nov 2018 08:37:26 +0000 <![CDATA[create new tv and customize view for article]]>
I did a couple research online, but not much luck, so I wonder some expert can help me out here.
Modx version: 2.65
I tried to create a new TV to show up articles grid system. so they don't need to click every single article and template variable to do the edit.
I saw collections plugin can do customize the view like here:
So I wonder can I do the same for articles? because the site already has a decent amount of articles already, not planning to move to collections.

Like this:

Thank you so much for the help!
]]> Wed, 24 Oct 2018 03:57:26 +0000
<![CDATA[Query/Join Multiple Tables with xPDO]]>
I have 3 separate tables (yes, the need to be separate tables) that all have different types of data for the same list of products.

I need to be able to create a page for a product and display all of these cells of data using simple placeholders.

My Package is all set up and ready to go and I know how to do this with a single table using getCollection. But I can't seem to figure out how I would join these so that ALL of the attributes will be available to me for use in TPLs.

Has anyone done something like this.]]> Sat, 11 Aug 2018 06:52:37 +0000
<![CDATA[Mysterious problem with $c->where() result in processor.]]> I've got an interesting problem with where() method smiley
There are one processor exdends the modObjectGetListProcessor.
On it's input is an array.
In prepareQueryBeforeCount() function I do where() like this:
foreach ($query as $k => $v){
    $this->where[$k.':LIKE'] = "%$v%";

Then I use $c->toSql() for checking query result.
Selects and joins are not so interested for us - there is ok, but results of where() method work - that is interesting and slightly mysterious for me smiley

Lets start.
Input array is like this (print from log):
[$query] => Array
            [id] => 100
            [city] => 200
            [name] => 300
            [phone] => 400
            [mail] => 500
            [performer] => 600

In this array can be digits or strings - whatever.

Variable $this->where looks like:
[$this->where] => Array
                  [id:LIKE] => %100%
                  [city:LIKE] => %200%
                  [name:LIKE] => %300%
                  [phone:LIKE] => %400%
                  [mail:LIKE] => %500%
                  [performer:LIKE] => %600%

And after where() method works we've got (part of print from log):
WHERE  ( `a`.`id` LIKE 0 AND `a`.`city` LIKE 0 AND `a`.`name` LIKE '%300%' AND `a`.`phone` LIKE 0 AND `a`.`mail` LIKE 0 AND `a`.`performer` LIKE 0 )

Only one of values is not 0. Always. Even if input array contains only one of this elements.
By experimental way I clear that zeroing happens when where() get concatenated values like "%$v%" OR "%".$v."%". But why for 'name' is not the same bechaviour?

Why Lord?
Oh... Sorry... smiley
Why can it be, guys?
Any thoughts?

You can ask any additional questions.

MODX 2.6.5-pl
PHP 7.2.1
]]> Wed, 25 Jul 2018 07:10:16 +0000
<![CDATA[Get PHP Variable from ID]]>
it's a simple question, but i cant find the right solution:

how i get the PHP-Variable from one resource in the other?

I try to get the value "X-Type" form resource "12" to resource "25"

MODx Resource ID "12":
[[!mySnippet? &type=`X-Type`]]

MODx Resource ID "25":

return $type;

]]> Thu, 19 Jul 2018 11:05:57 +0000
<![CDATA[Modparser error "You should not call uncached elements inside cached!"]]>
Any ideas?

[2018-01-04 18:11:05] (ERROR @ /var/www/vhosts/ : 453) You should not call uncached elements inside cached!
Outer tag: [[pdoResources? &parents=`0` &tpl=`ufsRSSItemE` &class=`ufsEvents` &loadModels=`ufsevents` &where=`{"published:=":"1","eenddate:<=":"[[!now]]"}` &fastMode=`0` &showLog=`0` &sortby=`eenddate` &sortdir=`ASC`]]
Inner tag pdoResources? &parents=`0` &tpl=`ufsRSSItemE` &class=`ufsEvents` &loadModels=`ufsevents` &where=`{"published:=":"1","eenddate:<=":"[[!now]]"}` &fastMode=`0` &showLog=`0` &sortby=`eenddate` &sortdir=`ASC`

Server and install details:

OS: CentOS Linux 7.4.1708 (Core)
Apache web server
PHP 7.0.24
Database server:
Server: Localhost via UNIX socket
Server version: 5.5.56-MariaDB - MariaDB Server

Modx 2.6.1-pl
packages installed:
StageCoach]]> Sun, 24 Jun 2018 10:47:05 +0000
<![CDATA[xPDO database connection script]]>
This works fine as code inline:

$dsn = 'mysql:host=localhost; dbname=database; port=3306; charset=utf8';
$xpdo = new xPDO($dsn,'username','password');

echo $o=($xpdo->connect()) ? 'Connected<br><br>' : 'Not Connected<br><br>';

$results = $xpdo->query("SELECT * FROM `table` LIMIT 20");
while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
  $output .= $modx->getChunk('test', $row);

return $output;

But when I put the connection script into a snippet 'pdoConnect'

define('MODX_CORE_PATH', '/core/');
require_once MODX_CORE_PATH . 'model/modx/modx.class.php';

$dsn = 'mysql:host=localhost; dbname=database; port=3306; charset=utf8';
$xpdo = new xPDO($dsn,'username','password');

and call it using


it throws an error

Fatal error: Call to a member function connect() on a non-object in /home2/pghmin/public_html/core/cache/includes/elements/modsnippet/76.include.cache.php on line 22

Modx 2.2.14 and 2.5.0 same problem.

Seems trivial but I can't find a solution]]> Wed, 20 Jun 2018 03:47:08 +0000
<![CDATA[Get parent page titles in foreach loop of all resources when constructing a resource list for a TV]]>
I have a snippet that gets a list of all resources on the site (with some filtering) for use in a single-select TV. Everything works fine until I also try to get the parent page titles of all of those resources to help make selecting the correct page a little easier as some of my pages have identical pagetitles.

Below is the snippet that works:
$excludeParents = array(42,2,41,31,25,22);
$c = $modx->newQuery('modResource');
    'modResource.parent:NOT IN' => $excludeParents,
    ' IN' => $excludeParents
$resources = $modx->getCollection('modResource',$c);

$pageList = [];
foreach ($resources as $resource) {
    $pageList[] = $resource->get('pagetitle') . '==' . $resource->get('id');

$output = implode("||",$pageList);

return $output;

Below is one of my attempts at getting the parent page titles, this results in a 500 error with no errors being logged in Modx or in server error_logs that I can find.
$excludeParents = array(42,2,41,31,25,22);
$c = $modx->newQuery('modResource');
    'modResource.parent:NOT IN' => $excludeParents,
    ' IN' => $excludeParents
$resources = $modx->getCollection('modResource',$c);

$pageList = [];
foreach ($resources as $resource) {
    $parentID = $resource->get('parent');
    $parent = $modx->getObject('modResource', $parentID);
    $pageList[] = $resource->get('pagetitle') . ' ('. $parent->get('pagetitle') .')==' . $resource->get('id');

$output = implode("||",$pageList);

return $output;

If I just use $parentID in place of $parent->get('pagetitle'), it shows the parent ID's.

I thought maybe Modx didn't like trying to get page titles for ID's of 0, so I also tried adding an if statement to only try to get parent titles if the ID wasn't 0. However, this still results in a 500 error.
$excludeParents = array(42,2,41,31,25,22);
$c = $modx->newQuery('modResource');
    'modResource.parent:NOT IN' => $excludeParents,
    ' IN' => $excludeParents
$resources = $modx->getCollection('modResource',$c);

$pageList = [];
foreach ($resources as $resource) {
    $parentID = $resource->get('parent');
    $parentTitle = '';
    if($parentID !== '0'){
      $parent = $modx->getObject('modResource', $parentID);
      $parentTitle = $parent->get('pagetitle');
    $pageList[] = $resource->get('pagetitle') . ' ('. $parentTitle .')==' . $resource->get('id');

$output = implode("||",$pageList);

return $output;

I appreciate any help!]]> Sat, 05 May 2018 03:46:51 +0000
<![CDATA[xPDO 3 w/o MODx]]>
I'm working on a project where the front will be JS based (Angular or Vue). The back will be a PHP/JWT API but i'm completely addict to xPDO (not a big fan of Laravel since i have a lot of own classes). Usually i work with MODx but this time i won't need the CMS part so i'd like xPDO only.

Is there a getting started or something to start with for xPDO 3.0?

Thanks!]]> Sat, 05 May 2018 03:40:26 +0000
<![CDATA[Create ressource and get id]]>
i try to create new ressource und then get the id:

this is my snippet:
// Part 1  
    $pagetitle = "my ressource";
    $parent = "0";
    $alias = "myalias";

$doc = $modx->newObject('modDocument');
// Part
    $newdoc = $modx->getObject('modDocument', array('pagetitle'=>$pagetitle));
    $id = $newdoc->get('id');

problem: the second part get the id from ressource with pagetitle "my ressource". If i have many ressource with the same pagetitle, then it doesn't work. Know anybody better solution?]]> Sat, 05 May 2018 03:35:23 +0000
<![CDATA[xPDO schema visualizer demo]]>

Suggestions/comments welcome!]]> Sat, 05 May 2018 03:32:16 +0000
<![CDATA[Does xPDO automatically manage closing db connections?]]>
I am simply trying to avoid sleeping mySQL connections where the php is holding the connection open for no reason while it is processing data.


]]> Sat, 05 May 2018 03:28:08 +0000
<![CDATA[runSnippet getImageList inside getResources (for Collections imageRenderer)]]> SOLVED - SEE FIRST ANSWER

I am trying to set up a custom imageRenderer Snippet for my Collections view. I want to display the first image of a MIGX-TV which hosts some images.

So I made this Snippet:
$row = $modx->getOption('row', $scriptProperties, '');
$rowId = $row['id'];

$modx->setPlaceholder('getId', $rowId);

$gR = $modx->runSnippet('getResources', [
'parents' => '-1',
'resources' => $rowId,
'tpl' => 'ref-image-id'

return '<a href="[[++site_url]]manager/?a=resource/update&id=' . $row['id'] . '" title="' . $row['pagetitle'] . '"><img src="' . $gR . '" width="60" ></a>';

In my Chunk ref-image-id I have this (only works cached):

The ref-image-item Chunk:

On my Collections view all of the images have the path of the first resource. The image itself ([[+image]]) is correct.
What I get is:


when it should be:


I have tried this without the placeholder and inside my getImageList call using &docid=`[[+id]]` - same problem.]]> Fri, 02 Feb 2018 09:50:30 +0000
<![CDATA[foreach single placeholder]]>
i try to get my pagetitles as single option-values (placeholder) like this:

<form method="POST" class="form" action="[[~[[*id]]]]">



<input type="submit" value="Send" /></form>

but my snippet put all pagetitle in one row like this:

<form method="POST" class="form" action="[[~[[*id]]]]">


<input type="submit" value="Send" /></form>

What ist wrong here? Please help.

My Snippet:

$collection = $modx->getCollection('modResource',array('template' => 4));
foreach ($collection as $collect){
    $list = "<option>". $mytitle. "</option>";


HTML Form:

<form method="POST" class="form" action="[[~[[*id]]]]">
<input type="submit" value="Send" /></form>
]]> Sun, 17 Dec 2017 09:39:41 +0000
<![CDATA[Alias comparison. To long waiting period.]]>
I have written a snippet, to compare all alias in my db with the "check" variable. This snippet ist working, but the waiting period (if alias found) is very long. I have only 10 resource!

Is something wrong with this snippet? Does anyone have other, faster ideas?

$check = "home";
$resourses = $modx->getCollection('modResource',array(
   'alias' => $check
foreach ($resourses as $resourse) {
  $output = $resourse->get('alias');

if (isset($output)) {return "True";} else {return "False";}
]]> Wed, 06 Dec 2017 09:21:19 +0000
<![CDATA[Placeholder as link doesn't work]]>
i cant set my placeholder as link in chunk:


$newsite = "24";
$modx->setPlaceholder('mysite', $newsite);

$mychunk = $modx->getChunk('site-tpl');
$res = $modx->getObject('modResource',$id);

Chunk site-tpl:
Here is my new site: <a href="[[~[[+mysite]]]]">My new Site</a><br>
TestID: [[+mysite]]

Here is my new site: <a href="">My new Site</a><br>
TestID: 24

what is wrong here?]]> Fri, 01 Dec 2017 11:33:12 +0000
<![CDATA[array question]]>
Ive a maybe simple (but for me not) array question:

I try to show the groupmembers by a snippet which gets the groups out of a TV:

[[!grouplist? &group=`[[*Gruppe]]`]] (Inpupt fe: 2,3,4

I query it but it always only shows up the first group (2):
'UserGroupMembers.user_group:IN' => array($group)

So my assumption is that I represent the array wrong to the query, because when I do

'UserGroupMembers.user_group:IN' => array(2,3,4) it works!

Any ideas?]]> Wed, 20 Sep 2017 09:40:17 +0000
<![CDATA[Rest API - Mutiple TV's for a collection of resources]]>
Was hoping one of the wise members of the modx community could help me, I have two TV's for a template (tv-eample1 and tv-example2). I Was wondering if anyone knows how to get these two different TV's from each resource, I am able to get the TV's using innerJoin but as not all resources will use these TV's, I need to be able to achieve this using left join, although left join does return the TV's the values are wrong, it would use exactly the same value for tv-eample1 and tv-example2 when there both would be different.

here is my code, maybe am going about this wrong, this is for my rest service so won't be using any modx extra as it does use custom tables, but I have removed them to simplify the issue:

Version:MODX Revolution 2.5.7-pl
		$myIds = $this->modx->getChildIds(2,2);
		$query = $this->modx->newQuery('modResource'); 
		  '','modResource.pagetitle','tvv.contentid as tvvcontentid','tvv.value as example1','tvv2.value as example2',' as tvname',' as tvname2',' as tvid',' as tvid2'
		$query->leftJoin('modTemplateVarResource', 'tvv', array(
		    'tvv.contentid ='
		$query->leftJoin('modTemplateVarResource', 'tvv2', array(
		    'tvv2.contentid ='
		$query->leftJoin('modTemplateVar', 'tv', array(' = tvv.tmplvarid','' => 'tv-eample1'));
		$query->leftJoin('modTemplateVar', 'tv2', array(' = tvv2.tmplvarid','' => 'tv-example2'));	
		$query->where(array('id:IN' => $myIds));
		$items = $this->modx->getCollection('modResource', $query);

Any help will be appreciated.

Thanks]]> Fri, 08 Sep 2017 10:24:54 +0000