$modx->addPackage('extendeduser', MODX_CORE_PATH . 'components/extendeduser/model/', 'modx_'); // this works $res = $modx->getCollection('UserResources'); // as does this $data = $modx->getCollection('UserData'); // and this $user = $modx->getObject('extUser', 2); $data = $user->getOne('UserData'); print_r($data->toArray(), true); this gives a non-object error.
<?php /** * @package extendeduser * @subpackage user.mysql */ class extUser extends modUser { function __construct(xPDO & $xpdo) { parent::__construct($xpdo); $this->set('class_key','extUser'); } } ?>
<?xml version="1.0" encoding="UTF-8"?> <model package="extendeduser" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1" tablePrefix="modx_"> <object class="extUser" extends="modUser"> <composite alias="UserData" local="id" foreign="internalKey" cardinality="one" owner="local" /> <composite alias="UserResources" local="id" foreign="internalKey" cardinality="many" owner="local" /> </object> <object class="UserData" table="user_data" extends="xPDOSimpleObject"> <field key="internalKey" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" default="0" index="unique" /> <field key="firstname" dbtype="varchar" precision="100" phptype="string" null="false" /> <field key="lastname" dbtype="varchar" precision="100" phptype="string" null="false" /> <field key="institution" dbtype="varchar" precision="100" phptype="string" null="false" /> <field key="specialty" dbtype="varchar" precision="100" phptype="string" null="false" /> <field key="eula" dbtype="enum" precision="'TRUE','FALSE'" phptype="string" null="false" /> <field key="oncologistdatabase" dbtype="enum" precision="'TRUE','FALSE'" phptype="string" null="false" /> <field key="newsletters" dbtype="text" phptype="string" null="false" /> <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" > <column key="id" length="" collation="A" null="false" /> </index> <index alias="internalKey" name="internalKey" primary="false" unique="true" type="BTREE" > <column key="internalKey" length="" collation="A" null="false" /> </index> <composite alias="extUser" local="internalKey" foreign="id" cardinality="one" owner="foreign" /> <!--aggregate alias="extUser" local="internalKey" foreign="id" cardinality="one" owner="foreign" /--> <!--aggregate alias="User" class= "modUser" local="internalKey" foreign="id" cardinality="one" owner="foreign"/--> <!--aggregate alias="Profile" class="modUserProfile" local="internalKey" foreign="internalKey" cardinality="one" owner="foreign"/--> </object> <object class="UserResources" table="user_resources" extends="xPDOSimpleObject"> <field key="internalKey" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="index" /> <field key="resource" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="index" /> <field key="favorite" dbtype="enum" precision="'TRUE','FALSE'" phptype="string" null="false" /> <field key="saved" dbtype="enum" precision="'TRUE','FALSE'" phptype="string" null="false" /> <field key="frequency" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="true" /> <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" > <column key="id" length="" collation="A" null="false" /> </index> <index alias="internalKey" name="internalKey" primary="false" unique="false" type="BTREE" > <column key="internalKey" length="" collation="A" null="false" /> </index> <index alias="resource" name="resource" primary="false" unique="false" type="BTREE" > <column key="resource" length="" collation="A" null="false" /> </index> <composite alias="extUser" local="internalKey" foreign="id" cardinality="one" owner="foreign" /> <!--aggregate alias="extUser" local="internalKey" foreign="id" cardinality="one" owner="foreign" /--> <!--aggregate alias="User" class= "modUser" local="internalKey" foreign="id" cardinality="one" owner="foreign"/--> <!--aggregate alias="Profile" class="modUserProfile" local="internalKey" foreign="internalKey" cardinality="one" owner="foreign"/--> </object> </model>
$c = $modx->newQuery('userData'); $c->where(array('User.username' => 'BobRay')); // I you have the user ID, do this instead: // $c->where(array('User.userdata_id = $userId)); $user = $modx->getObjectGraph('userData', '{"Profile":{},"User":{}}', $c); // To get multiple users: // $users = $modx->getCollectionGraph('userData', '{"Profile":{},"User":{}}', $c); $userResources = $user.User->getMany('UserResources');
<?php if (!class_exists('memberTools')) { class memberTools { function __construct(modX & $modx, array $config= array ()) { $this->modx= & $modx; $this->extPath= $modx->getOption('core_path',null, MODX_CORE_PATH).'components/extuser/'; $this->modx->addPackage('extuser', $this->extPath .'model/', 'modx_'); $this->_config= array_merge(array ( 'userID' => $this->modx->user->get('id'), ), $config); $this->userObject = $this->getUserObj($this->_config['userID']); $this->userProfileObject = $this->userObject->getOne('Profile'); $this->userDataObject = $this->userObject->getOne('UserData'); $this->userResourceCollection = $this->userObject->getMany('UserResources'); } /** * add user resource * @param */ public function addUserResource($scriptProperties){ $newResource = $this->modx->newObject('UserResources'); $newResource->fromArray($scriptProperties); $this->userObject->addMany($newResource); $this->userObject->save(); return; } } }
<?php $output = ''; $core = $modx->getOption('core_path').'components/extuser/model/'; $membertools = $modx->getService('membertools', 'memberTools', $core); if (!$membertools instanceof memberTools){ $modx->log(modX::LOG_LEVEL_ERROR, 'memberTools Insantiation failed'); return; }else{ $scriptProperties = array( 'resource' => 23, 'favorite' => 1, 'saved' => 1, 'frequency' => 999 ); $membertools->addUserResource($scriptProperties); } return;
The only thing I can think of is that addService() is adding a new service every time you call it and all instances are executing. It seems unlikely (especially since you're seeing multiples of two), but nothing else comes to mind, unless there's a tag for the snippet someplace where it will execute multiple times (like in a template or aggregated by something like getResources). When it adds multiple records, are they duplicates of each other?
You might try using loadClass() or addPackage() instead of getService(), and deleting all files in the core/cache directory before testing. It doesn't appear that you need getService() here, though maybe I'm missing something. getService() is meant to be called once and then used in multiple locations by referencing it with the $modx object.
https://bobsguides.com/blog.html/2013/05/25/understanding-addpackage(),-loadclass(),-and-getservice()/
// snippet $core = MODX_CORE_PATH . 'components/extuser/model/'; $membertools = $modx->getService('membertools', 'memberTools', $core); if (!$membertools instanceof memberTools){ $modx->log(modX::LOG_LEVEL_ERROR, 'memberTools Insantiation failed'); return; } $membertools->addUserResource('tools'); return; <?php if (!class_exists('memberTools')) { class memberTools { function __construct(modX & $modx, array $config= array ()) { $this->modx= & $modx; $this->_config= array_merge(array ( 'userID' => $this->modx->user->get('id'), ), $config); $this->userObject = $this->modx->getObject('extUser', $this->_config['userID']); $this->userProfileObject = $this->userObject->getOne('Profile'); $this->userDataObject = $this->userObject->getOne('UserData'); $this->userResourceCollection = $this->userObject->getMany('UserResources'); } function __destruct() { unset ($this->extPath, $this->userObject, $this->userID, $this->_config); } /** * add user resource * @param */ public function addUserResource($scriptProperties){ echo '<pre>'; print_r($scriptProperties); echo '</pre>'; $this->modx->log(modX::LOG_LEVEL_ERROR, '[memberTools] : addUserResources function is executing.'); $newResource = $this->modx->newObject('UserResources'); $newResource->set('resource', 23); $newResource->set('favorite', 'true'); $newResource->set('saved', 'false'); $newResource->set('frequency', 777); echo '<pre>'; print_r($newResource->toArray()); echo '</pre>'; $this->userObject->addMany($newResource); $this->userObject->save(); return; } /** * testing user functions * @param */ public function addUserData(){ $data = $this->userObject->getOne('UserData'); $data->set('firstname', 'GROGAN'); $data->set('lastname', 'PILLAR OF GOD'); $data->set('institution', 'yes'); $data->set('specialty', 'stuff'); $data->set('eula', 0); $data->set('oncologistdatabase', 0); $data->set('newsletters', 0); $data->set('academicinterestother', 'Education'); $data->set('academicinterestother', 'stuff'); $data->set('academicinterestspecifics', 'stuff'); $data->set('clinicalinterest', 'Breast'); $data->set('clinicalinterestother', 'stuff'); $data->set('clinicalinterestspecifics', 'stuff'); $this->userObject->save(); return; } } }
<?xml version="1.0" encoding="UTF-8"?> <model package="extuser" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1" tablePrefix="modx_"> <object class="extUser" extends="modUser"> <composite alias="UserData" class="UserData" local="id" foreign="internalKey" cardinality="one" owner="local" /> <composite alias="UserResources" class="UserResources" local="id" foreign="internalKey" cardinality="many" owner="local" /> </object> <object class="UserData" table="user_data" extends="xPDOSimpleObject"> <field key="internalKey" dbtype="varchar" precision="10" phptype="string" null="false" index="unique" /> <field key="firstname" dbtype="varchar" precision="100" phptype="string" null="false" /> <field key="lastname" dbtype="varchar" precision="100" phptype="string" null="false" /> <field key="institution" dbtype="varchar" precision="100" phptype="string" null="false" /> <field key="specialty" dbtype="varchar" precision="100" phptype="string" null="false" /> <field key="academicinterest" dbtype="enum" precision="'Education','Educational Research','Basic Science Research','Phase I/II','Clinical Trials','Phase III Clinical Trials','Health Services Research','Transitional Research','Not Applicable'" phptype="string" null="false" /> <field key="academicinterestother" dbtype="varchar" precision="100" phptype="string" null="false" /> <field key="academicinterestspecifics" dbtype="text" phptype="string" null="false" /> <field key="clinicalinterest" dbtype="enum" precision="'Breast','Lung','GI','GU','Head and Neck','Gynecology','Sarcoma','Melanoma','Supportive Care','Lymphoma','Myeloma','Palliative Care'" phptype="string" null="false" /> <field key="clinicalinterestother" dbtype="varchar" precision="100" phptype="string" null="false" /> <field key="clinicalinterestspecifics" dbtype="text" phptype="string" null="false" /> <field key="eula" dbtype="enum" precision="'TRUE','FALSE'" phptype="string" null="false" /> <field key="oncologistdatabase" dbtype="enum" precision="'TRUE','FALSE'" phptype="string" null="false" /> <field key="newsletters" dbtype="text" phptype="string" null="false" /> <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" > <column key="id" length="" collation="A" null="false" /> </index> <index alias="internalKey" name="internalKey" primary="false" unique="true" type="BTREE" > <column key="internalKey" length="" collation="A" null="false" /> </index> <aggregate alias="extUser" local="internalKey" foreign="id" cardinality="one" owner="foreign" /> </object> <object class="UserResources" table="user_resources" extends="xPDOSimpleObject"> <field key="internalKey" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="index" /> <field key="resource" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" index="index" /> <field key="favorite" dbtype="enum" precision="'TRUE','FALSE'" phptype="string" null="false" /> <field key="saved" dbtype="enum" precision="'TRUE','FALSE'" phptype="string" null="false" /> <field key="frequency" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="true" /> <index alias="PRIMARY" name="PRIMARY" primary="true" unique="true" type="BTREE" > <column key="id" length="" collation="A" null="false" /> </index> <index alias="internalKey" name="internalKey" primary="false" unique="false" type="BTREE" > <column key="internalKey" length="" collation="A" null="false" /> </index> <index alias="resource" name="resource" primary="false" unique="false" type="BTREE" > <column key="resource" length="" collation="A" null="false" /> </index> <aggregate alias="extUser" local="internalKey" foreign="id" cardinality="one" owner="foreign" /> </object> </model>
addMany(array($newResource));