Welcome Guest, Not a member yet? Register   Sign In
A faster way to get an id-indexed result from database?
#3

(This post was last modified: 04-22-2020, 10:08 AM by jreklund.)

I would use the function getUnbufferedRow('array') instead. As of right know you are getting an extra foreach. I'm not sure how to extend the Database class.

You can however create your own function that you can create in a library or BaseController / Base Model.

PHP Code:
// In a controller or model class
function getResultWithKey(\CodeIgniter\Database\ResultInterface $resultstring $key 'id')
{
    
$results = [];

    while (
$row $result->getUnbufferedRow('array'))
    {
        if (isset(
$row[$key]))
        {
            
$results[$row[$key]] = $row;
        }
    }

    return 
$results;
}

// Someplace else
$res $db->table('records')->get();
$result $this->getResultWithKey($res); 
Reply


Messages In This Thread
RE: A faster way to get an id-indexed result from database? - by jreklund - 04-22-2020, 10:01 AM



Theme © iAndrew 2016 - Forum software by © MyBB