phalcon / phalcon/cphalcon

[NFR]: Mvc\Model\Resultset: Allow traversal by primary key

Open
#17,330 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

new feature request
Dominant language
PHP
Stars
10.8k
Forks
1.9k
Avg merge
7h 2m
Merged PRs (30d)
47

Description

Is your feature request related to a problem? Please describe.
Yes
When a model's find() method returns several records, it can be inconvenient to access specific records even with the primary key. I am new to Phalcon, but unless I'm missing something, here is what I had to do, for example, to retrieve the titles of a set of documents:

$documentsArray = $pages->items->toArray();
$titleRecords = Documents::find([
    'conditions' => 'uid in ({ids:array})',
    'bind' => ['ids' => array_column($documentsArray, 'uid_document')]
    ]);
$titles = [];
foreach ($titleRecords as $document) {
    $titles[$document->uid] = $document->title;
}
foreach ($documentsArray as &$documentArray) {
    $documentArray['document_title'] = $titles[$documentArray['uid_document']];
}

Describe the solution you'd like
It would be much more convenient if Mvc\Model\Resultset could be browsed not just with 0-based indices, but using at least primary keys, so that - for example - the above could be simplified to just something like:

$documentsArray = $pages->items->toArray();
$titleRecords = Documents::find([
    'conditions' => 'uid in ({ids:array})',
    'bind' => ['ids' => array_column($documentsArray, 'uid_document')],
    ], Model::INDEX_PRIMARY);
foreach ($documentsArray as &$documentArray) {
    $documentArray['document_title'] = $titleRecords[$documentArray['uid_document']];
}

For me, it is code simplicity/readability which matters. I am not worried about the performance of traversal for this case.

Describe alternatives you've considered
Introducing a new method to complement find() (something like findByPrimaryKey()) could be just as good as adding an argument to find().

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Mvc\Model\Resultset API and the Model::find entry point described in the issue. Determine how resultset indexing currently works and how primary-key indexing should be exposed. Done means callers can retrieve returned records by primary key while existing zero-based traversal remains supported.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
backend, databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.