[[!migxLoopCollection?
&packageName=`rating`
&classname=`Userr`
&where=`{"storyr_id":"[[+id]]"}`
&tpl=`avrating_snip`]]<?xml version="1.0" encoding="UTF-8"?>
<model package="rating" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
<object class="Userr" table="rating" extends="xPDOSimpleObject">
<field key="userr_id" dbtype="varchar" precision="250" phptype="string" null="false" default="" />
<field key="storyr_id" dbtype="varchar" precision="250" phptype="string" null="false" default="" />
<field key="rating" dbtype="varchar" precision="350" phptype="string" null="false" default="" />
</object>
</model>This question has been answered by Bruno17. See the first response.
$packageName = 'rating';
$classname = 'Userr';
$where = $modx->getOption('where',$scriptProperties,'');
$where = $modx->fromJson($where);
$packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/';
$modelpath = $packagepath . 'model/';
$modx->addPackage($packageName, $modelpath);
$c = $modx->newQuery($classname);
$c->select('AVG(rating) as avg');
if (is_array($where)){
$c->where($where);
}
$avg = 0;
if ($object = $modx->getObject($classname,$c)){
$avg = $object->get('avg');
}
return $avg;<?php
$packageName = 'rating';
$classname = 'Userr';
$packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/';
$modelpath = $packagepath . 'model/';
$modx->addPackage($packageName, $modelpath);
$c = $modx->newQuery($classname);
$c->where(array('storyr_id' => $story_id));
$c->select('AVG(rating) as avg');
if ($object = $modx->getObject($classname,$c)){
$avg = $object->get('avg');
}
$output = $modx->getChunk('avrating_template',array(
'storyid' => $story_id,
'rating' => round($avg),
));
return $output;try a snippet like that:
$packageName = 'rating'; $classname = 'Userr'; $where = $modx->getOption('where',$scriptProperties,''); $where = $modx->fromJson($where); $packagepath = $modx->getOption('core_path') . 'components/' . $packageName . '/'; $modelpath = $packagepath . 'model/'; $modx->addPackage($packageName, $modelpath); $c = $modx->newQuery($classname); $c->select('AVG(rating) as avg'); if (is_array($where)){ $c->where($where); } $avg = 0; if ($object = $modx->getObject($classname,$c)){ $avg = $object->get('avg'); } return $avg;