Skip to content

Instantly share code, notes, and snippets.

@johnsnook
Last active September 13, 2018 18:23
Show Gist options
  • Select an option

  • Save johnsnook/d7bb9fca61dfd990c31a18a1ef4298c4 to your computer and use it in GitHub Desktop.

Select an option

Save johnsnook/d7bb9fca61dfd990c31a18a1ef4298c4 to your computer and use it in GitHub Desktop.
Postresql child record ids as an array
-- I don't understand how totally this works but it does so
CREATE AGGREGATE children_ids (anyelement)
(
sfunc = array_append,
stype = anyarray,
initcond = '{}'
);
-- then you can do
SELECT
DISTINCT visitor.id,
name,
children_ids(visitor_id) as kids
FROM
visitor
LEFT JOIN visits ON visitor.id = visits.visitor_id
GROUP BY
visitor.id
-- and, in yii the $kids fields with be a string like so
-- "{1421,4334,333,123214}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment