Jump to main content Jump to doc navigation

In this article, we'll talk about creating and removing objects (and their respective rows in the database), as well as more complex queries.

Creating Objects

Object creation is handled by the newObject method. It assumes that the object you are trying to create has been properly defined inside your XML schema and that schema has generated the proper class files. For a simple example, we can look at the built-in MODX objects.

// let's create a Template
$template = $modx->newObject('modTemplate');

// now, lets save some data into the fields
$template->set('description','A test template.');

// we could have also done it like this:
$data = array(
        'templatename' => 'TestTemplate',
        'description' => 'A test template.',

// okay, now we're ready. let's save.
if ($template->save() === false) {
        die('An error occurred while saving!');

A row is never actually added to the database until the object's save() command is run.

Removing an Object

To remove an object from the database, we use the remove command:


This will also remove any composite relationships defined in the object's schema. In the previous example with modTemplates, these are the modTemplateVarTemplate objects, which map Templates to TVs. Those will cascade and be removed.

More Complex Queries

Okay, so pretty soon you are going to need to do some more complex queries than we've dealt with. That's where the xPDOQuery object comes in. This allows you to build abstract query objects that emulate more advanced SQL commands. So, lets try to grab the third 10 resources (so 21-30), ordered by menuindex, that are either 1) published and searchable, or 2) created by the user with username 'george123'.

$c = $modx->newQuery('modResource');
    'modResource.published' => 1,
    'modResource.searchable' => 1,
    'PublishedBy.username' => 'george123',

$resources = $modx->getCollection('modResource',$c);

A couple of things to note. One, note that innerJoin first passes the class name, then the alias. And in orCondition, the 3rd parameter is the group number, which effectively groups the conditions into proper parenthesis (the first 2 in the first parenthetical group, the 3rd in another).

xPDOQuery supports the the methods: join, rightJoin, leftJoin, innerJoin, andCondition, orCondition, sortby, groupby, limit, bindGraph, bindGraphNode, and select.

Obviously, you can go pretty wild here with complex queries. The nice thing about xPDO in MODX is that there's really a ton of different ways to do most things - you could also have used $modx->getCollectionGraph for this as well.

In the next article, we'll talk about how this is used in the context of MODX processors with JSON.

See Also

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.



$0 per month—let's make that $500!

Learn more