![]() |
Query result as an indexed array or object by index of choice - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Feature Requests (https://forum.codeigniter.com/forumdisplay.php?fid=29) +--- Thread: Query result as an indexed array or object by index of choice (/showthread.php?tid=76215) |
Query result as an indexed array or object by index of choice - Leo - 04-22-2020 Very often I need to index my array or object by a certain key like this: PHP Code: $unindexed = $model->select('id, filter_id, value_name') PHP Code: $indexed = array_column($unindexed, null, 'id'); What about a built in function in a model? like this: PHP Code: $indexed = $model->select('id, filter_id, value_name') So far I have a blunt copy of findAll(): PHP Code: public function getResultKey(string $key, int $limit = 0, int $offset = 0) It works as expected, it can return an array of arrays or objects depending on your models returnType. Also it can use integer or string key for indexing. But, with it being just a copy of findAll() it seems like bloatware. If anyone has better ideas of how to implement this, or if you think its worth it, or make it chain with other methods that would be nice. Otherwise, I can usually just do this: PHP Code: $indexed = array_column($model->select('id, filter_id, value_name') RE: Query result as an indexed array or object by index of choice - rmilecki - 11-14-2020 I'd love to see such feature. I think that:
Consider this: PHP Code: $model->indexed()->findAll() PHP Code: $model->indexed('id')->findAll() RE: Query result as an indexed array or object by index of choice - paulbalandan - 11-16-2020 There is already a findColumn method which uses array_column for single result. Maybe we can have an analogous findAllColumn() which will do the indexing for set of results. Perhaps you can send in a PR in the Github repo? RE: Query result as an indexed array or object by index of choice - rmilecki - 11-16-2020 (11-16-2020, 11:16 AM)paulbalandan Wrote: There is already a findColumn method which uses array_column for single result. Maybe we can have an analogous findAllColumn() which will do the indexing for set of results.findColumn() uses find() without any argument which means it finds and returns all matching rows. Not just a single row (result). It uses array_column() without 3rd argument so it doesn't handle any array indexing. I think it would be inconsistent to have:
(11-16-2020, 11:16 AM)paulbalandan Wrote: Perhaps you can send in a PR in the Github repo?I did, let's see how it goes: #3895 Model: support returning array indexed by a specified column RE: Query result as an indexed array or object by index of choice - paulbalandan - 11-17-2020 I commented in your PR to update your fork first since it is outdated with the base branch. Currently, if ever it will be approved, it is not mergeable because your fork has merge conflicts. |