3

Is there any replacement for this query in PHP Yii2?

SELECT * FROM my_table WHERE my_column ?& array['2', '1', '3', '4'];

my_column = jsonb

Because i get this error

Error Info: Array
(
  [0] => 42601
  [1] => 7
  [2] => ERROR:  syntax error at or near "$1"
  LINE 1: SELECT * FROM my_table WHERE my_column $1& array['2', '1', '3',       '...
                                                 ^
)

I'm using PHP Yii2 and PostgreSQL 9.4:

$sql = "SELECT * FROM my_table WHERE my_column ?& array['2', '1', '3', '4'];";

$model = TestModel::findBySql($sql)->asArray()->all();

The purpose of this query is compare if the value exists in the database.

The database have:

  1. ["1", "2", "3", "4"]
  2. ["1", "2", "3"]

And it works in the pgAdmin3 SQL Editor.

PostgreSQL 9.41 jsonb Operators

2
  • PostgreSQL likes to use numbered placeholders (i.e. $1, $2, ...) so something in PHP is converting the ? in ?& to $1 as though the ? was a placeholder. That's where the odd $1 is coming from. Don't know enough about the PHP interface to tell you how to fix it though, sorry. Commented Apr 14, 2015 at 3:46
  • First thought was to wrap $sql in yii\db\Expression but seems like it didn't help. Commented Apr 14, 2015 at 5:46

3 Answers 3

2

In PostgreSQL, ? is alias for jsonb_exists() function. ?& is alias for jsonb_exists_all(). So, you can write you query like so:

SELECT * FROM my_table WHERE jsonb_exists_all('my_column', array['2', '1', '3', '4']);
Sign up to request clarification or add additional context in comments.

2 Comments

PHP Warning – yii\base\ErrorException pg_query(): Query failed: ERROR: syntax error at or near jsonb_exists_all
I tried this: TestModel::find() ->where("jsonb_exists_all(my_column, array['2', '1', '3', '4'])") ->asArray() ->all(); As i needed for Yii2 and it worked! It solved my problem, you are amazing! Thank you :)
1

Escaping the SQL String is possibly what's missing. So something like this should ideally work:

SELECT * FROM service WHERE test2 \?& array['2', '1', '3', '4'];

3 Comments

ERROR: syntax error at or near "\" and it generated \$1&
The "?" is required for PostgreSQL to compare in the JSON
0

As a quick temporary solution, I used the pg_query

$host = "localhost";
$user = "test";
$pass = "test";
$db = "test";

$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
          or die("Could not connect to server\n");

//my_column type is jsonb
$query = "SELECT * FROM my_table WHERE my_column ?& array['2', '1', '3']";

$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");

$data = array();
while ($row = pg_fetch_assoc($rs)) {
  $data[] = $row;
}

echo "<pre>";
print_r($data);

pg_close($con);

But I couldn't find a way to make it work with Yii2. hope this help others.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.