MODX Community

$obj->fromArray($obj_data) error?


Trying to insert a new row in modMenu table like:

$menu = $modx->newObject('modMenu');
$menu_data = array(
				'text' => 'myapp.configuration',
				'parent' => 'components',
				'action' => 'configuration',
				'description' => 'myapp.configuration.menu_desc',
				'icon' => '',
				'menuindex' => '5',
				'params' => '',
				'handler' => '',
				'permissions' => 'myapp_configurator',
				'namespace' => 'myapp',


I get a row but with empty “text” field, like if array[0] element don’t fill the value.

I am doing something wrong?

Try this:

$menu->fromArray($menu_data, "", true, true));

The third argument tells fromArray() to set the primary key. It defaults to false and text is the primary key, so it’s not being set.

You could also have left the text member out of the array used this just before the save() call:

$menu->set('text', 'myapp.configuration');

FYI, the second argument (empty string here) holds an optional prefix to remove, the fourth argument tells fromArray() to use the raw values from your array to set the fields rather than calling the object’s set() method for each one.