S
S
SibVector2017-10-27 12:53:50
MODX
SibVector, 2017-10-27 12:53:50

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();

and this is what I see:
update 1 2 2 1
What am I doing wrong!? :/
Maybe I don't understand something? ^^

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question