Saved Resources don't Show in Tree

A case where MODX resources save with no errors but don't show up in the Resources Tree

In the previous article, we looked at how to forward users with links based on the year. In this one we'll look at an odd case where saved resources don't show up in the Resource tree.

MODX logo

The Problem

There are a number of reasons why you can't save a resource, but this is a case where resources save fine but never show up in the tree. This happened to MODX Forum user Moorkaempfer. The resources saved with no trouble and no error messages, but they never showed up in the tree.

After some excellent troubleshooting by Moorkaempfer, the problem was traced to the fact that all new resources were being saved with a resource ID of 0. MODX won't accept that as a legitimate Resource ID and refused to show them in the tree.

Further Diagnosis

The root of the problem turned out to be a faulty setting in the modx_site_content table. The standard setting for the id field for MODX resources include None for the default value and AUTO_INCREMENT in the "Extra" column. You can see these values in PhpMyAdmin if you look at the modx_site_content table and click on the "Structure" tab.

The Solution

Fixing this involved clicking on "Change" on the Structure. After doing that, you can see the Default dropdown menu, which should be set to None. To the right of that, you should see three checkboxes: Null, Adjust privileges, and A_I.

The first one, Null, determines whether the field can be set to Null. For resources, this checkbox should be blank because an resource should never have a null id field.

The second checkbox, Adjust privileges, should be checked. This makes sure that if the database, table, column, or procedure is renamed, copied, or moved, the privileges it has won't be changed but will come along in the move.

The third and final checkbox (and the source of the problem), A_I is the AUTO_INCREMENT setting. It should always be checked for MODX resources so they will be assigned the next available ID in the table. Having it unchecked would mean that either the value of the id field would be provided in the fields when it is created, or the new ID would always be 0. Since MODX doesn't provide a value for the id field, having AUTO_INCREMENT off will always lead to a resource ID of 0.

You probably know this already, but you should never mess with the resource ID field in the database. Changing it can cause TV values and Templates to be connected to the wrong resources. Worse yet, it will leave the parent field of child resources pointing to a parent that no longer exists.

Some new MODX users are annoyed by the fact that resource IDs are not necessarily sequential and can get very large over time, but it's best to just get used to that and leave them alone.

Coming Up

In the next article, we'll look at a method for finding bugs in your code using a binary search.

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 high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)

Comments (0)

Please login to comment.