) that is sufficiently clear and simple for my very basic understanding of 1) PHP, 2) MySQL, 3) MODx.<?php
$fields=array();
$fields['content']='my content';
$fields['pagetitle']='my pagetitle';
$fields['myTV']='tvcontent';
$doc = $modx->newObject('modResource');
$doc->set('createdby', $modx->user->get('id'));
//set docfields from fields-array
foreach ($fields as $field=>$value){
$doc->set($field, $value);
}
$doc->set('template', '1');//set your template
$doc->save();
//set TVs from fields-array
foreach ($fields as $field=>$value)
{
if ($tv = $modx->getObject('modTemplateVar', array ('name'=>$field)))
{
/* handles checkboxes & multiple selects elements */
if (is_array($value)) {
$featureInsert = array();
while (list($featureValue, $featureItem) = each($value)) {
$featureInsert[count($featureInsert)] = $featureItem;
}
$value = implode('||',$featureInsert);
}
$tv->setValue($doc->get('id'), $value);
$tv->save();
}
}
?>$fields['createdon'] = time();
$resource = $modx->newObject('modResource',$fields);$modx->setContent('Some Content');
//or
$modx->setContent($contentVariableName);http://bobsguides.com/custom-db-tables.html
$response = $modx->runProcessor('resource/create',array(
'pagetitle' => 'Fun Page',
'published' => true,
'content' => 'stuff here',
/* etc */
));
if ($response->isError()) {
if ($response->hasFieldErrors()) {
$fieldErrors = $response->getAllErrors();
$errorMessage = implode("\n",$fieldErrors);
} else {
$errorMessage = 'An error occurred: '.$response->getMessage();
}
return $errorMessage;
}
return 'Success!';
$response = $modx->runProcessor('resource/create',$fields);
Shaun, am I correct that calling the processor is preferred over newObject(), because it checks permissions and handles default settings and sanity checks for the fields?
(argghh! -- more rewriting -- it’s never going to end is it?).
OTOH, newObject() should be a lot faster and more efficient in a utility snippet if you have all the necessary fields set correctly.
Yes, although you should check the top of the processor’s file to see what parameters it needs. Example, resource/create:
I assume that the following would also work if you have the $fields set when importing resources as the OP intends:
$response = $modx->runProcessor('resource/create',$fields);
* @param string $pagetitle The page title. * @param string $content The HTML content. Used in conjunction with $ta. * @param integer $template (optional) The modTemplate to use with this * resource. Defaults to 0, or a blank template. * @param integer $parent (optional) The parent resource ID. Defaults to 0. * @param string $class_key (optional) The class key. Defaults to modDocument. * @param integer $menuindex (optional) The menu order. Defaults to 0. * @param string $variablesmodified (optional) A collection of modified TVs. * Along with $tv1, $tv2, etc. * @param string $context_key (optional) The context in which this resource is * located. Defaults to web. * @param string $alias (optional) The alias for FURLs that this resource is * designated to. * @param integer $content_type (optional) The content type. Defaults to * text/html. * @param boolean $published (optional) The published status. * @param string $pub_date (optional) The date on which this resource should * become published. * @param string $unpub_date (optional) The date on which this resource should * become unpublished. * @param string $publishedon (optional) The date this resource was published. * Defaults to time() * @param integer $publishedby (optional) The modUser who published this * resource. Defaults to the current user. * @param json $resource_groups (optional) A JSON array of resource groups to * assign this resource to. * @param boolean $hidemenu (optional) If true, The resource will not show up in * menu builders. * @param boolean $isfolder (optional) Whether or not the resource is a * container of resources. * @param boolean $richtext (optional) If true, MODx will render the available * RTE for editing this resource. * @param boolean $donthit (optional) (deprecated) If true, MODx will not log * visits on this resource. * @param boolean $cacheable (optional) If false, the resource will not be * cached. * @param boolean $searchable (optional) If false, the resource will not appear * in searches. * @param boolean $syncsite (optional) If false, will not empty the cache on * save.
It does, the docs are wrong.