Dashboard > People > Shaun McCormick > 2008 > June > 25 > PHP Coding in MODx Revolution, Pt. II

View Attachments (0) Info

PHP Coding in MODx Revolution, Pt. II

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

Creating objects is pretty simple. It's important to note, however, that a row is never actually added to the database until the object's save() command is run. So, on to the code:

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

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

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

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

It's that simple.

Removing an Object

Okay, so assuming we have the same previous object and now want to remove it (you could also grab another object, of course), the code is simply:

$template->remove();

Yes. That's it. Done. It will also remove any composite relationships - with modTemplates, these are the modTemplateVarTemplate objects, which map Templates to TVs. Those will cascade and be removed.

More Complex Queries

Okay, so obviously 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');
$c->leftJoin('modUser','PublishedBy');
$c->where(array(
	'modResource.published' => 1,
	'modResource.searchable' => 1,
));
$c->orCondition(array(
	'PublishedBy.username' => 'george123',
),null,1);
$c->sortby('menuindex','ASC');
$c->limit(10,20);

$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.

<< June 2008 >>
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          

PHP Coding in MODx Revolution, Pt. III >>
<< PHP Coding in MODx Revolution, Pt. I


Browse Space
- Pages
- Labels
- Attachments
- Bookmarks
- News
- Activity
- Advanced

Explore Confluence
- Popular Labels
- Notation Guide

Your Account
Log In

Other Features

View a printable version of the current page.

Add Content


Powered by Atlassian Confluence 2.7.1, the Enterprise Wiki.
Bug/feature request - Contact administrators