Answer the question
In order to leave comments, you need to log in
How to update an object in MODX?
Let me explain.
The package created in CMPGenerator, new objects are created perfectly, but the existing ones do not want to be updated.
Here is the code for the snippet that should update an existing entry in the database:
<?php
$modx->addPackage('profut', $modx->getOption('core_path').'components/profut/model/');
$user = $modx->getUser();
$pfUser = $modx->getObject('ProfutUsers', array('uid' => $user->get('id')));
$pfUserArray = array();
if ($pfUser) { // true
echo "update ";
$pfUserArray = $pfUser->toArray();
}
else {
echo "create ";
$pfUser = $modx->newObject('ProfutUsers');
$pfUserArray['uid'] = $user->get('id');
$pfUserArray['team_id'] = 0;
$pfUserArray['role'] = 0;
}
echo $pfUser->get('way_id') . ' '; // 1
// ((int) $hook->getValue('casetype')) == 2
$pfUserArray['way_id'] = ((int) $hook->getValue('casetype'));
$pfUser->fromArray($pfUserArray);
echo $pfUser->get('way_id') . ' '; // 2
$pfUser->save();
echo $pfUser->get('way_id') . ' '; // 2
$pfUser = $modx->getObject('ProfutUsers', array('uid' => $user->get('id')));
echo $pfUser->get('way_id') . ' '; // 1
die();
update 1 2 2 1
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question