Jump to content

Finding json string with help of PW selectors.


valan
 Share

Recommended Posts

There is template "mytemplate".
There is field "myjson" in this template that stores json string.
 
What is the most performance-effective way to check if a given json string already exists in some page using PW selectors?
// this code doesn't work due to selector quote issues
$query = 'template=mytemplate, myjson=' . $json;
if($pages->count($query)!==0) ...
// this code doesn't work as sanitized json string is not equal to original json string
$query = 'template=mytemplate, myjson=' . $sanitizer->selectorValue($json); 
if($pages->count($query)!==0) ...
 
Link to comment
Share on other sites

If the only 2 valid possibilities for the content of the "myjson" field are either empty or containing json output, a simple boolean will work-

$query = "template=mytemplate, myjson!=''"

if($pages->count($query)){...
Link to comment
Share on other sites

But he needs to find the page with that JSON string I think, which is much more difficult given single and double quotes.

The only way I can think of to do this is have another JSON field called something like "sanitizedmyjson" and on page save, strip out all quotes and store that copy in that field, then do the sanitized selector based on that field. Sanitizer probably strips out other stuff too, but you get the idea.

  • Like 1
Link to comment
Share on other sites

Aaah, I understand. A variation on Pete's way might be to create a hash and save that for future comparison. Something like this to save (pseudopwcode)

$page->hash = md5($myjsonUnsanitised);
$page->json = $sanitizer->text($myjsonUnsanitised);
$page->save();

And then use the hash as selector later

$hash = md5($jsonToFind);
$query = "template=whatever, hash=$hash";
  • Like 2
Link to comment
Share on other sites

Pete, Dave - thanks for suggestions! Looks like there is no other way than to create additional field.

dragan - I think %= won't work due to issues with quotes in json string.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...