• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Query result as an indexed array or object by index of choice

#1
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')
    ->whereIn('id', [1,2,3,4])
    ->orderBy('id''ASC')
    ->findAll();

$indexed = [];
foreach(
$unindexed as $i) {
   $indexed[$i->id] = $i;

process can be simplified with array_column:
PHP Code:
$indexed array_column($unindexednull'id'); 

What about a built in function in a model? like this:
PHP Code:
$indexed $model->select('id, filter_id, value_name')
            ->whereIn('id', [1,2,3,4])
            ->orderBy('id''ASC')
            ->getResultKey('id'); 

So far I have a blunt copy of findAll():
PHP Code:
public function getResultKey(string $keyint $limit 0int $offset 0)
{
      return array_column($this->findAll($limit$offset), null$key);


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')
            ->whereIn('id', [1,2,3,4])
            ->orderBy('id''ASC')
            ->findAll(), null'id'); 
You can see things I made with codeigniter here: itart.pro its not overly impressive as I have very little time to learn.
Reply

#2
I'd love to see such feature.

I think that:
  1. By default $primaryKey should be used
  2. Instead of getResultKey() replacing findAll() we should get a modifier (similar to the asArray())

Consider this:
PHP Code:
$model->indexed()->findAll() 
PHP Code:
$model->indexed('id')->findAll() 
Reply

#3
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?
Reply

#4
(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.

Perhaps you can send in a PR in the Github repo?
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:
  1. findColumn() returning unindexed array (indexed from 0 to n)
  2. findAllColumn() returning indexed array

(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
Reply

#5
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.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.