Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sorting a field within relation #56

Open
thipages opened this issue Jan 12, 2018 · 4 comments
Open

Sorting a field within relation #56

thipages opened this issue Jan 12, 2018 · 4 comments

Comments

@thipages
Copy link

Hi,

lets have a table A which has a fk a_bId refrencing table B, B containg a field b_field
I want to retrieve all A records sorted by b_field.
$sortingString="b_field"; is not working (see $sortingString below).
How may I manage it?
Thank you

$mapperA= new \Maphper\Maphper(
    new \Maphper\DataSource\Database($pdo, 'A', 'id')
);
$mapperB= new \Maphper\Maphper(
    new \Maphper\DataSource\Database($pdo, 'B', 'id')
);
$mapperA->addRelation("b", new\Maphper\Relation\One($mapperB,"a_bId","id"));

$sortingString="what may I put here?";
foreach ($mapperA->sort($sortingString) as $A) {
  echo ($A->b->b_field.":".$A->name);
}
@ckrudelux
Copy link

This is not possible cuase Maphper isn't doing SQL Joins. What you can do is use usort to sort the output with your own sorting function.

usort($mapperA, function($a, $b){
    if ($a->b->b_field == $b->b->b_field) {
        return 0;
    }
    return ($a->b->b_field < $b->b->b_field) ? -1 : 1;
});
foreach ($mapperA as $A) {
  echo ($A->b->b_field.":".$A->name);
}

Not tested, but I think you will get the Idea.

@garrettw
Copy link

This is not possible cuase Maphper isn't doing SQL Joins.

I wonder if it should be - or is there some advantage to not doing that?

@TRPB
Copy link
Member

TRPB commented Mar 22, 2023

The reason for not doing SQL joins is that Maphper is designed to map relationships between arbitrary sources, we don't want to assume that both ends of the relationship are the same SQL database. One end could be a databse and another a CSV or JSON file.

Only PDO is supported currently but you could still define a relationship between two different databases

@garrettw
Copy link

garrettw commented Apr 19, 2023

It would be great if it could detect when two of the same source are being used and thus make use of specific features supported by that source to accomplish relational data retrieval, which would enable things like this. I suppose the question is, what should be returned when a source-supported sort is not available? Maybe throw an error saying something like, "Sorting method not defined for the sources in use."?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants