[NFR]: Mvc\Model\Resultset: Allow traversal by primary key
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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