$c = $modx->newQuery('mrResources');
if (!empty($query)){
$qstring = '%'.$query.'%';
$c->innerJoin('mrRooms','Rooms','room = Rooms.id');
$c->where(array(
'name:LIKE' => $qstring
,'OR:Rooms.name:LIKE' => $qstring
));
}
$tests['count'] = $count = $modx->getCount('mrResources', $c);
$resources = $modx->getIterator('mrResources',$c);
foreach ($resources as $resource) {
$resourceArray = $resource->toArray();
$resourceArray['room_name'] = $modx->getObject('mrRooms',$resourceArray['room'])->get('name');
$tests[] = $resourceArray;
}
Array
(
[mrRoomsGetListPath] => c:/inetpub/wwwroot/test_site/MeetingRooms/core/components/MeetingRooms/processors/mgr/mrRooms/getlist
[rooms getlist] => 1
[mrResourceGetListPath] => c:/inetpub/wwwroot/test_site/MeetingRooms/core/components/MeetingRooms/processors/mgr/mrResources/getlist.php
[resources getlist] => 1
[query] => one
[qstring] => %one%
[count] => 1
[0] => Array
(
[id] => 1
[name] => resource one
[max_amount] => 1
[room] => 1
[room_name] => test1
)
)
<?php
//config data
$isLimit = !empty($scriptProperties['limit']);
$start = $modx->getOption('start',$scriptProperties,0);
$limit = $modx->getOption('limit',$scriptProperties,10);
$sort = $modx->getOption('sort',$scriptProperties,'name');
$dir = $modx->getOption('dir',$scriptProperties,'ASC');
$query = $modx->getOption('query',$scriptProperties,'');
$c = $modx->newQuery('mrResources');
$c->setClassAlias('mrResources');
$c->leftJoin('mrRooms','Rooms');
if (!empty($query)){
$qstring = '%'.$query.'%';
$c->where(array(
'mrResources.name:LIKE' => $qstring
));
}
$count = $modx->getCount('mrResources', $c);
$c->sortby($sort,$dir);
$resources = $modx->getIterator('mrResources',$c);
//iterate
$list = array();
foreach ($resources as $resource) {
$resourceArray = $resource->toArray();
$resourceArray['room_name'] = $modx->getObject('mrRooms',$resourceArray['room'])->get('name');
$list[] = $resourceArray;
}
return $this->outputArray($list,$count);
({"total":"0","results":[]})This question has been answered by cdm014. See the first response.
<?php
$isLimit = !empty($scriptProperties['limit']);
$start = $modx->getOption('start',$scriptProperties,0);
$limit = $modx->getOption('limit',$scriptProperties,10);
$sort = $modx->getOption('sort',$scriptProperties,'name');
$dir = $modx->getOption('dir',$scriptProperties,'ASC');
$query = $modx->getOption('query',$scriptProperties,'');
$debug = array();
//query the db
$resTable = $modx->getTableName('mrResources');
$roomsTable = $modx->getTableName('mrRooms');
$qstring = '%'.$query.'%';
$debug['query'] = $qstring;
//build the query note I'm prefacing a lot of the fields with the table name
//I had to do this to get what I needed
$sql = "Select ".$resTable.".id, ".$resTable.".name, max_amount, room, Rooms.name as `room_name` from $resTable join $roomsTable as Rooms on room = Rooms.id where $resTable.name like '$qstring' or Rooms.name like '$qstring'";
$debug['sql'] = $sql;
//actually prepare the statement
$stmt = $modx->prepare($sql);
$stmtArray = array();
//execute the statement. No I'm not using variables here as I did that directly in the
//sql query
if ($stmt && $stmt->execute()){
$stmtArray[] = $stmt->fetchAll(PDO::FETCH_ASSOC);
//$tests['stmtArray'] = $stmtArray;
}
$list = array();
//had to use element[0] to actually get my objects
foreach ($stmtArray[0] as $stmtRes)
{
//some cleaning up and processing of the data
$room = $modx->getObject('mrRooms',$stmtRes['room']);
$stmtRes['room_name'] = $room->get('name');
//actually add it to the list for output
$list[] = $stmtRes;
}
//yes count only shows ones that match the search query
$count = count($list);
return $this->outputArray($list,$count);