ahren
ahren

Reputation: 16959

$modx->query doesn't seem to execute

As per the documentation here: http://rtfm.modx.com/display/xPDO20/xPDO.query

Which shows the following as an example:

$result = $modx->query("SELECT * FROM modx_users WHERE id=1");
if (!is_object($result)) {
   return 'No result!';
}

I would assume this statement would work:

$checkUnique = $modx->query("SELECT * FROM my_table_name WHERE guid = '$unique' AND used = 0");

//guid already used, or non-existant
if(!is_object($checkUnique)){
  $result = array(
    "result" => false
  );
  return json_encode($result);
}

When I do a var_dump (of $checkUnique), I get this as a result:

object(PDOStatement)#22 (1) { ["queryString"]=> string(70) "SELECT * FROM my_table_name WHERE guid='5114722f24870' AND used=0"}

I know the used column has been set to 1, but it never triggers my if block.

What am I doing wrong?

Upvotes: 2

Views: 2996

Answers (1)

Phil
Phil

Reputation: 164871

It seems the MODx doco could use some help. I just tried this locally and an empty result set still returns a PDOStatement object.

You could attempt to use PDOStatement::rowCount() to check for the number of rows returned

$count = $checkUnique->rowCount();

or alter your query like so

$check = $modx->query("SELECT COUNT(1) FROM my_table_name WHERE guid = '$unique' AND used = 0");
$count = $check->fetchColumn();
if (!$count) {
    // etc

I've raised a bug with the MODx documentation - http://tracker.modx.com/issues/9502

Upvotes: 2

Related Questions