API PUT with TVs

I can do with some help if possible.
I can’t for the life of me figure out why when running a PUT command on TV fields they don’t update.

Current code which lists the TVs
Any help would be greatly appreciated.

 <? class MyControllerItems extends modRestController {

    public $classKey = 'modResource';
    public $defaultSortField = 'id';
    public $defaultSortDirection = 'ASC';

    public function read($id) {
        if (empty($id)) {
            return $this->failure($this->modx->lexicon('rest.err_field_ns',array(
                'field' => $this->primaryKeyField,
        /** @var xPDOObject $object */
        $c = $this->getPrimaryKeyCriteria($id);
        $this->object = $this->modx->getObject($this->classKey,$c);
        if (empty($this->object)) {
            return $this->failure($this->modx->lexicon('rest.err_obj_nf',array(
                'class_key' => $this->classKey,
	$tv = $this->modx->getObject('modTemplateVarResource', array('tmplvarid' => $tvID, 'contentid' => $id));
    $objectArray = Array();
    $objectArray ['id']  = $id;
    $objectArray ['pagetitle']  = $this->object->pagetitle;
    $objectArray ['alias']  = $this->object->alias;
     $objectArray['welcome-message'] = $this->object->getTVValue(20);

            $afterRead = $this->afterRead($objectArray);
            if ($afterRead !== true && $afterRead !== null) {
                return $this->failure($afterRead === false ? $this->errorMessage : $afterRead);

            return $this->success('',$objectArray);


You’ll need to implement the TV saving manually in the put() method. Only standard resource fields are automatically handled, and welcome-mesage is not a standard field.

Thanks @markh Markh, Thats what i thought, put cannot find an example that helps me. Do you have a simple example?

Something like this would work:

public function afterPut(array &$objectArray) {}
   $value = $this->getProperty('welcome-message');
   $this->object->setTVValue('welcome-message', $value);
   // include in the response
   $objectArray['welcome-message'] = $value;

Probably worth doing some value sanitisation/validation rather than naively saving like this, though.

Use the source code for modRestController to find out what’s all there. The code isn’t too hard to follow.

Thank you :slight_smile:

That worked perfectly thank you @markh one last question, Is it possible to clear cache or save resource once the PUT has been made as the changes aren’t showing right away.


Yes, call refresh() on the cache manager. See: https://docs.modx.org/current/en/extending-modx/caching#refreshing-the-modx-core-cache

1 Like

FWIW, since it appears that you already have the primary key field, there’s no need to call the getPrimaryKeyCriteria() method.

This should do the same thing a tiny bit faster:

$this->object = $this->modx->getObject($this->classKey, array($this->primaryKeyField => $id ));
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.