Using fromArray() to Write Raw Values to the DB

In the previous article, we saw a way to write raw values to the MODX database with PDO. In this one, we’ll see a slightly slower, but much more convenient method that uses xPDO’s fromArray() method.


If you’ve written code that uses Tpl chunks to display the fields of MODX objects, you’ve probably used xPDO’s toArray() method. Since any MODX object is also an xPDO object, you can convert all its fields to a PHP associative array by calling its toArray() method.

Here are some examples:MODX logo

$fields = $resource->toArray();
$fields = $chunk->toArray();
$fields = $user->toArray();


There is also a reciprocal method called fromArray() that sets the object fields from an associative array. It looks like this:

$fields = array(
    'pagetitle' => 'My Page',
    'alias' => 'my-page',
    'published' => '1',
    'hidemenu' => '0',
    'introtext' => 'This is my Page',

$resource = $modx->newObject('modResource');


When called this way, fromArray() calls the object’s set() method for each field. That means, for example, that any date fields are converted to Unix timestamps, and JSON fields (like the properties field of elements and the extended field of the user object) will be converted to JSON strings.

What if you don’t want that? What if you already have timestamps or JSON strings to store in the fields? Or maybe, as in the previous article, you want to store user passwords that are already hashed because you’re importing them from another install of MODX.

The fromArray() method gives you a way to store the raw values, but it’s a little tricky. The fromArray() method has a number of optional arguments, shown below:

public function fromArray($fields, $keyPrefix= '',
    $setPrimaryKeys= false, $rawValues= false,
    $adhocValues= false) {}


The fromArray() code is in the xPDOObject class if you’d like to take a look at it. The first argument is the array of fields we used above. The second specifies a prefix to strip from the array keys. The third tells MODX to set the primary key of the object (these two are seldom used). The fourth argument is the one we want. When set to true, it stores the raw values from the $fields array rather than calling set(). The fifth argument (seldom used) makes the keys class variables.

If you want fromArray() to use the raw values of the $fields array, you need to set the intermediate arguments to their default values. So the call would look like this:

$resource->fromArray($fields, '', false, true);


The first argument is our $fields array. The second sets the prefix to an empty string. The third is set to false to tell MODX not to set the primary key of the object. Finally, the fourth argument is true, which tells MODX not to call set(), but rather to use the raw values of the $fields array for the field values.

Note: You almost never want to set the primary key of the object. If you do, and an object already exists with that key, you’ll get an error. The only time I’ve ever set the third argument to true involved importing data from another MODX install into completely empty tables. This preserves the relationships between the objects. For example, resource fields like parent, template, createdby, editedby, and publishedby, depend on the parents, templates, and users, having their original IDs.

Rewriting the User Password Code

In the previous article, we used PDO to write raw user passwords to the database. Here’s what that code would look like using fromArray():

$fields = array(
    'username' => 'SomeUserName',
    'active' => '1',
    'cachepwd' => '',
    'password' => 'SomePassword',
    'hash_class' => 'hashing.modMD5',
    'salt' => '',

$newUser = $this->modx->newObject('modUser');
$newUser->fromArray($fields, "", false, true);

/* for debugging */
// echo "\n\n" . print_r($newUser->toArray(), true);

The code above will write the raw values from the $fields array to the user fields in the database. Note that we’ve called save() with the argument, false. This tells MODX not to cache the user object and speeds things up a little.

For more information on how to use MODX to create a web site, see my web site Bob’s Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for quality MODX Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , | Posted under MODX, MODX | Leave a comment

Let’s take a look at the Dolphin U Dashboard

Hello and welcome! In today’s blog we’re going to take a look at the new Dolphin U Dashboard. The dashboard shows you general information about your site and server. To access the dashboard click “Dashboard” in the studio area.

accessing the studio dashboard

accessing the studio dashboard

The dashboard shows you the cache usage, Dolphin version, disk space and basic host info.

dolphin u dashboard

dolphin u dashboard

You can also conduct a server audit by clicking the down arrow below the log out button, then clicking “Server audit”.

dolphin u server audit

dolphin u server audit

The server audit will show you all you need to know regarding your server meeting the Dolphin U specifications.

dolphin u server audit

dolphin u server audit

This is just another tool to keep your site (and server) running smoothly.

Looking for quality BoonEx Dolphin Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under BoonEX Dolphin | Leave a comment

ocPortal Custom Profile Fields (CPF)

What are CPFs and why would you use them?

ocPortal CPF’s are a powerful way to collect member information, you can create extremely complex signup forms but for now we will concentrate on some simple additions by creating form fields that allow the user to enter simple text, select an option from a dropdown list and make a selection from radio buttons.

Learn More

Tags: , , , | Posted under ocPortal | Leave a comment