<![CDATA[ Add new document to (new) resource group via api - My Forums]]> https://forums.modx.com/thread/?thread=31499 <![CDATA[Re: Add new document to (new) resource group via api]]> https://forums.modx.com/thread/31499/add-new-document-to-new-resource-group-via-api#dis-post-497787 https://github.com/modxcms/revolution/pull/11364 error log spam could be prevented like this (as the join/leaveGroup() methods throw an error each time a resource is already in a group or not in a group...)

if (!$resource->isMember('ResourceGroupName')) {
    $resource->joinGroup('ResourceGroupName');
}
]]>
exside May 12, 2014, 03:29 PM https://forums.modx.com/thread/31499/add-new-document-to-new-resource-group-via-api#dis-post-497787
<![CDATA[Re: Add new document to (new) resource group via api]]> https://forums.modx.com/thread/31499/add-new-document-to-new-resource-group-via-api#dis-post-170423
$resource->joinGroup('ResourceGroupName');
/* or */
$resource->joinGroup(123); /* id of resource group */
]]>
splittingred Mar 16, 2011, 07:39 AM https://forums.modx.com/thread/31499/add-new-document-to-new-resource-group-via-api#dis-post-170423
<![CDATA[Re: Add new document to (new) resource group via api]]> https://forums.modx.com/thread/31499/add-new-document-to-new-resource-group-via-api#dis-post-170422
This seems to work, unless anyone can suggest a reason why it’s bad:

$assign = $modx->newObject('modResourceGroupResource');
$data = array('document_group'=>MYGROUPID,'document'=>MYDOCID);
$assign->fromArray($data);
$assign->save();
]]>
[email protected] Mar 16, 2011, 06:09 AM https://forums.modx.com/thread/31499/add-new-document-to-new-resource-group-via-api#dis-post-170422
<![CDATA[Re: Add new document to (new) resource group via api]]> https://forums.modx.com/thread/31499/add-new-document-to-new-resource-group-via-api#dis-post-170421
$group = $this->modx->newObject('modResourceGroup');
//Not sure about hardcoding the private_memgroup and _webgroup...
$groupdata = array('name'=>'MYNAME,'private_memgroup'=>0,'private_webgroup'=>0);
$group->fromArray($groupdata);
$group->save();
$newgroupid = $group->get('id');

But the question remains about how to assign a resource to it using the api.

Nothing like a discussion with yourself...]]>
[email protected] Mar 16, 2011, 05:58 AM https://forums.modx.com/thread/31499/add-new-document-to-new-resource-group-via-api#dis-post-170421
<![CDATA[Add new document to (new) resource group via api]]> https://forums.modx.com/thread/31499/add-new-document-to-new-resource-group-via-api#dis-post-170420 In a Custom Manager Page I am creating a new resource and want to a) create a new resource group and b) add the new resource to that resource group.
The code I am using for creating the resource itself is similar to the following:
$page = $this->modx->newObject('modDocument');

$pagedata = array( PAGEDATA HERE );

$page->fromArray($pagedata);

$page->save();

$newpageid = $page->get('id');



Can anyone give me any pointers for the methods to use: I guess I’m looking for something like "newObject(’modResourceGroup’)", then setResourceGroup($newpageid,$newgroupid) (Although obviously that would be a bit too easy!).

Thanks for any help in advance.]]>
[email protected] Mar 16, 2011, 05:47 AM https://forums.modx.com/thread/31499/add-new-document-to-new-resource-group-via-api#dis-post-170420