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.

fromArray()

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

[code language=”php”]
$fields = $resource->toArray();
$fields = $chunk->toArray();
$fields = $user->toArray();
[/code]

 

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

[code language=”php”]
$fields = array(
‘pagetitle’ => ‘My Page’,
‘alias’ => ‘my-page’,
‘published’ => ‘1’,
‘hidemenu’ => ‘0’,
‘introtext’ => ‘This is my Page’,
)

$resource = $modx->newObject(‘modResource’);
$resource->fromArray($fields);
$resource->save();
[/code]

 

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:

[code language=”php”]
public function fromArray($fields, $keyPrefix= ”,
$setPrimaryKeys= false, $rawValues= false,
$adhocValues= false) {}

[/code]

 

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:

[code language=”php”]
$resource->fromArray($fields, ”, false, true);
[/code]

 

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():

[code language=”php”]
$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);
$newUser->save(false);
[/code]

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 | RSS 2.0

Author Spotlight

Bob Ray

Bob Ray is the author of MODX: The Official Guide and over 30 MODX add-on components. He hosts Bob's Guides, a source of valuable information for MODX users, and has been very active in the MODX Forums with over 19,000 posts.

Leave a Reply

Your email address will not be published. Required fields are marked *