Skip to content

Feature idea: Model results by key #2167

Description

@MGatner

So I end up doing this all the time, especially for ORM-like prefetching:

$results = [];
foreach ((new MyModel())->findAll() as $object)
{
	// Re-index by primary key
	$results[$object->id] = $object;
}

I'd like to find an efficient way to do this as part of the model (or builder?) but it creates some potentially-unintuitive scenarios when using orderBy() that you could have an array of results with numerical keys not in order. That said I think it could be worth it, but BaseResult is already iterating through the whole result set once so ideally it would happen there so it isn't happening twice:


		if ($this->resultObject)
		{
			foreach ($this->resultObject as $row)
			{
				$this->resultArray[] = (array) $row;
			}

			return $this->resultArray;
		}

		is_null($this->rowData) || $this->dataSeek(0);
		while ($row = $this->fetchAssoc())
		{
			$this->resultArray[] = $row;
		}

Is this a bad idea for a core change to the framework?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions