dras
dras

Reputation: 368

How can I get a PSQL array to JSON using IDIORM?

I've looked at PSQL JSON Docs for a while, but I'm still a little confused about how to get it working. I have a text array in a particular row and want to pull it out into PHP and then insert into a JS var.

When I head to terminal and psql I put my query in it works great

SELECT array_to_json(COL) from TABLE where name ='foo';

However, when I create the same in a raw_execute in idiorm, I end up with a true boolean. How do I get actual results from the raw_execute into a php var? Where can I read more if this is an obnoxiously ignorant question?

Upvotes: 0

Views: 291

Answers (1)

dras
dras

Reputation: 368

Turns out raw_execute returns a bool for success or failure...

After combing idiorm docs long enough found raw_query feature here. I ended up putting my select statement from above into php var $sql and doing the following:

$result = \ORM::for_table('TABLE')->raw_query($sql)->find_one();

Because this was all being done inside a php foreach,

foreach(ORM::for_table("TABLE")->find_result_set() as $record) {

I was able to then insert the var into my JS dictionary

<script>
<?=record->name?> = {
    ...
    version: <?=isset($result->array_to_json) ? $result->array_to_json : '[]'?>

and it was then available in browser to display as desired.

Upvotes: 0

Related Questions