Skip to content

Instantly share code, notes, and snippets.

@Electrica
Created December 20, 2021 16:57
Show Gist options
  • Save Electrica/c30d79ef1214ee3b45c312983ccf45a6 to your computer and use it in GitHub Desktop.
Save Electrica/c30d79ef1214ee3b45c312983ccf45a6 to your computer and use it in GitHub Desktop.
views
<?php
/**
* @var MODX $modx
*/
$obResource = $modx->resource;
//Вытаскиваем просмотры
$q = $modx->newQuery('modTemplateVarResource');
$q->where(['tmplvarid' => 11, 'contentid' => $obResource->id]);
$obView = $modx->getObject('modTemplateVarResource', $q);
if(is_object($obView)){
$arView = $obView->toArray();
$arView['value'] = $arView['value']+1;
$obView->fromArray($arView);
$obView->save();
}else{
//Нету просмотров, создаем
$obView = $modx->newObject('modTemplateVarResource');
$arr = [
'tmplvarid' => 11,
'contentid' => $obResource->id,
'value' => 1
];
$obView->fromArray($arr);
$obView->save();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment