MODX Community

getImageList does not work conditional selection, how to configure the WHERE parameter?

The task is simple, there is a resource with id 198, it has MIGX TV field_news with a event field, you need to filter records by the WHERE clause, where event (type text) = new or old. All this is needed for the AJAX filter.

Here is the logic of the snippet. Simplified as much as possible for clarity.
In $ _GET [‘event’], the parameter is passed from the form. But in the example I directly prescribed it rigidly.
The problem is that the syntax of the where clause for getImageList is complicated, and I can’t understand how to write it.
If you bypass the $ filter array and write the condition $ where = ‘{“event: =”: “new”}’ directly in WHERE; then everything works. But you need to do this through an array, since there can be a lot of filtering parameters, and for AJAX you need the JSON format, in snippets this place is $ where = $ modx-> toJSON (array ($ filter));

$_GET['event'] = 'new';
$filter = array();
if($_GET['event']) {
  $filter[] = 'event='.$_GET['event'];
  //$filter[] = "{'event:=':'new'}";
}
if($filter) {
 $where = $modx->toJSON(array($filter));
 //$where = '{"event:=":"new"}';
}
$params = array(
  'docid' => 198,
  'tvname' => 'field-news',
    'where' => $where
    //'where' => '{"event:=":"new"}',
  );	
return $modx->runSnippet('getImageList', $params);

How to make this filter work? :frowning: Please help me figure it out!

1 Like

This is my best guess at what you want:

/* see if $_GET['new'] is set */
$new = (bool)  ($modx->getOption('new', $_GET, false, true)); 

if ($new) {
    $where = $modx->toJSON(array('event' => 'new'};
    $params = array(
        'docid' => 198,
        'tvname' => 'field-news',
        'where' => $where
    );	
    $output = $modx->runSnippet('getImageList', $params); 
} else {
    $output = "";
}

return $output;

I’m assuming that you don’t want to call getImageList if $_GET[‘new’] is not set, but maybe I’m misunderstanding what you want.

1 Like

Greetings! Thank you for responding! Happy New Year! :slight_smile: It’s not really about getImageList, it’s about the WHERE clause, the syntax, I want to configure the filter with the conditions simply. I already have a working code, AJAX filter, but it works with pdoResources with resources. Now I want to redo this code to work with MIGX fields instead of resources. And for this I use getImageList instead of pdoResources.

Here is the working code for working with resources and snippets pdoResources.

//Filter Fields Settings
$filter = array();

//Radio, Select & Text Fields Type
if($_GET['date']) {
    $filter[] = 'date='.$_GET['date'];
}

//Checkbox Type
if($_GET['garage']) {
	$filter[] = 'garage=1';
}

//End Settings


if($filter) {
	$where = $modx->toJSON(array($filter));
} else {
	$where = '';
}

$params = array(
	'parents' => $parents,
	'limit' => $limit,
	'offset' => $offset,
	'tpl' => $tpl,
	'select' => 'id,pagetitle,introtext,content',
	'includeTVs' => $fields,
	'showHidden' => '1',
	'sortby' => $sortby,
	'sortdir' => $sortdir,
	'where' => $where
	);

return $modx->runSnippet('pdoResources',$params);

Now I want this code to make work for MIGX TV, a simplified code example in my question above.
And I can’t get him to work with MIGX TV, I think the point is in the syntax WHERE here:
'where' => $ where
// ‘where’ => ‘{“date”: $ where}’
and in the format of the JSON here $ where = $ modx-> toJSON (array ($ filter));

I can’t understand how to make the code work :frowning:

1 Like

Can you explain what you want the where function to do?

I want to make a selection in getImageList with several conditions via the API, this is necessary for the AJAX filter by MIGX fields on the site. I think the problem is the syntax of the WHERE parameter

1 Like

In most cases, and I do think this is true for getImageList, the syntax of a &where property is a JSON-encoded array of key => value syntax, with optional conditions defined in the keys.

More specifically, any syntax that can be used with xPDOQuery->where.

The examples there show the array example; you’d need to encode them to JSON to pass them along in a snippet property.

For example if it gives the following PHP array:

$query->where(array(
   'width' => 15,
));

Then you’d encode the array to JSON and pass it like this:

[[getImageList?
  &where=`{"width":"15"}`
]]

(Or as you’re generating it with PHP before using runSnippet: 'where' => json_encode($filter) )

If that doesn’t work with getImageList, that makes it a MIGX-specific question which I’m afraid I don’t know too much about anymore. Its documentation unfortunately doesn’t mention limits to the where syntax but I can imagine it is a bit more restrictive than the full xPDOQuery syntax.

2 Likes

Thank you, I figured out the WHERE syntax, the selection on several WHERE parameters does not work:

WORK

$params = array(
	'docid' => 1,
	'tvname' => 'field-news',
	 'where' => '{"event:=":"old"}'
	);	
return $modx->runSnippet('getImageList', $params);

RESULT

Array
(
[MIGX_id] => 13
[event] => old
[mytv] => 2019

)

NOT WORK
$params = array(
‘docid’ => 1,
‘tvname’ => ‘field-news’,
‘where’ => ‘{“event:=”:“old”,“mytv:=”:“2017”}’
);
return $modx->runSnippet(‘getImageList’, $params);

NOT RESULT

I am afraid to have to refuse in the PHP of the getImageList :frowning:

1 Like

DECISION.

$filter = array();

if($_GET['date']) {
 $filter[] = 'date='.$_GET['date'];
}
if($_GET['event']) {
 $filter[] = 'event='.$_GET['event'];
}
$newArray = [];
if($filter) {
foreach($filter as $key => $value)
  {
 $tmp = explode('=', $value);
	$newArray[$tmp[0] . ':='] = $tmp[1];
    $res = explode("=", $value);
  }
} else {
	$where = '';
}
$where = json_encode($newArray);

$params = array(
	'docid' => $parents,
	'tvname' => $tvname,
	'tpl' => $tpl,
	'where' => $where
	);	
	
return $modx->runSnippet('getImageList', $params);
1 Like

Great you did it! I always felt your $where function was somewhat undeveloped previously, but you worked it out!

And thanks for your solution, it will help people in the future!

1 Like