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

Introduce Order enum #389

Merged
merged 1 commit into from
Feb 24, 2024
Merged

Introduce Order enum #389

merged 1 commit into from
Feb 24, 2024

Conversation

greg0ire
Copy link
Member

@greg0ire greg0ire commented Feb 17, 2024

The plan is to ultimately use when dealing with Criteria::$orderings

@ThomasLandauer
Copy link
Contributor

Yeah, looks good to me :-)
If you merge it, I will take care of the rest of the docs, as promised in doctrine/orm#11263 (comment)

src/Criteria.php Outdated Show resolved Hide resolved
@@ -155,16 +155,19 @@ public function getWhereExpression()
*/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the future, we would have @return array<string, Order> here

Copy link
Member Author

@greg0ire greg0ire Feb 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I should use array<string, string|Order> right now so that people start adapting their code, even if in practice we don't break BC and stay with array<string, string> ? 🤔 either that or introducing a new method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should provide a way to opt into the future behavior of this method. We could do so by adding a boolean flag (meh) or by adding a new method and deprecating this one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll try to find a new name.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is no setter, I went with just orderings()

src/Order.php Outdated
Comment on lines 9 to 10
case ASC = 'ASC';
case DESC = 'DESC';
Copy link
Member

@derrabus derrabus Feb 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case ASC = 'ASC';
case DESC = 'DESC';
case Ascending = 'ASC';
case Descending = 'DESC';

WDYT?

*
* @param array<string, string> $orderings
* @param array<string, string|Order> $orderings
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The types of the constructor signature have to be adjusted as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

src/Criteria.php Outdated
return $ordering;
}

return strtoupper($ordering) === Order::ASC->value ? Order::ASC : Order::DESC;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Trigger a deprecation?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we give the ORM a chance to upgrade first?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to wait for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, let me add that deprecation layer then.

Comment on lines -166 to -167
* @see Criteria::ASC
* @see Criteria::DESC
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deprecate the constants?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -331,7 +332,7 @@ public function testMatchingWithSortingPreserveKeys(): void
'object1' => $object1,
],
$collection
->matching(new Criteria(null, ['sortField' => Criteria::ASC]))
->matching(new Criteria(null, ['sortField' => Order::ASC]))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By simply changing the tests, you lose coverage for the old way of using this API. Codecov rightfully complains about that.

@ThomasLandauer
Copy link
Contributor

I guess this will then also be the way to order collections?:

#[ORM\OneToMany(...)
#[ORM\OrderBy(value: ['foo' => Order::Ascending])
private Collection $bar;

@greg0ire
Copy link
Member Author

Yes 👍

The plan is to ultimately use it and only it dealing with
Criteria::$orderings.
@greg0ire
Copy link
Member Author

Ping @derrabus 🙏

@greg0ire greg0ire added this to the 2.2.0 milestone Feb 24, 2024
@greg0ire greg0ire merged commit 957e63b into doctrine:2.2.x Feb 24, 2024
9 checks passed
@greg0ire greg0ire deleted the order-enum branch February 24, 2024 23:08
@ThomasLandauer
Copy link
Contributor

@greg0ire
Copy link
Member Author

greg0ire commented Feb 25, 2024

I think it depends, we have to look at the code, and see if the new names are passed as is or if some kind of translation/method signature changes are required.

BTW, why are you mentioning DBAL? It does not depend on collections, does it?

@ThomasLandauer
Copy link
Contributor

I'm talking about the docs, not the code!
DBAL docs have some occurrences, e.g. https://www.doctrine-project.org/projects/doctrine-dbal/en/4.0/reference/query-builder.html#order-by-clause

So should I replace all 'ASC' with the new Order::Ascending everywhere in ORM 3.1 (assuming that by that time everybody will have upgraded their collections)? Or add a note "New in version xy" like at https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/tutorials/extra-lazy-associations.html#extra-lazy-associations ? - This could become a mess, if there are many occurrences (which I guess there are...)

@greg0ire
Copy link
Member Author

greg0ire commented Feb 25, 2024

I'm talking about the docs, not the code!

I know, and please don't shout!

DBAL docs have some occurrences, e.g. https://www.doctrine-project.org/projects/doctrine-dbal/en/4.0/reference/query-builder.html#order-by-clause

That's an SQL query builder. This has nothing to do with collections whatsoever. doctrine/dbal should work without doctrine/collections installed. If you modify those docs to refer to Order, you will generate support tickets.

So should I replace all 'ASC' with the new Order::Ascending everywhere in ORM 3.1 (assuming that by that time everybody will have upgraded their collections)? Or add a note "New in version xy" like at https://www.doctrine-project.org/projects/doctrine-orm/en/2.11/tutorials/extra-lazy-associations.html#extra-lazy-associations ? - This could become a mess, if there are many occurrences (which I guess there are...)

Maybe, wait a bit, we have to evaluate which ones are going to be OK, and which ones require a breaking change. For instance consider this:

class ClassFromTheORM
{
    public function methodFromTheOrm(string $ordering)
    {
        Criteria::orderBy(['field' => $ordering);
    }
}

Can't recommend Order::Ascending for that one.

@ThomasLandauer
Copy link
Contributor

and please don't shout!

Sorry :-)

That's an SQL query builder. This has nothing to do with collections whatsoever.

I thought this is what this PR addresses?! - Moving from ->orderBy('username', 'ASC') to ->orderBy('username', Order::Ascending)?
So what is deprecated then?

@greg0ire
Copy link
Member Author

So what is deprecated then?

What you just said + referring to Criteria::ASC. We can see that it happens in the ORM, here for instance: https://github.com/doctrine/orm/blob/2a8802af122921ad9c40a266a74ce8c210c18e44/src/PersistentCollection.php#L588 or here: https://github.com/doctrine/orm/blob/2a8802af122921ad9c40a266a74ce8c210c18e44/src/Mapping/Driver/XmlDriver.php#L408

I don't think you will be able to find similar stuff in the DBAL.

@ThomasLandauer
Copy link
Contributor

ThomasLandauer commented Feb 25, 2024

OK, gotcha.

@greg0ire
Copy link
Member Author

You should IMO wait until we at least:

  • release doctrine/collections 2.2.0
  • contribute something that allows using Order::Ascending (right now, QueryBuilder::orderBy() accepts a string, and Order::Ascending is no string, Order::Ascending->value is).

@jifer
Copy link

jifer commented Mar 13, 2024

We just upgraded doctrine packages. Changing Criteria constants to enum was straightforward. We do love enums.

Problem is that we used this everywhere where QueryBuilder is involved (a lot), and it was quite disappointing seeing that this is not going to work.

We go from this:

$this->getEntityManager()
            ->getRepository(SomeEntity::class)
            ->createQueryBuilder('h')
            ->orderBy('h.id', Criteria::DESC)
            ->getQuery()
            ->getResult();

to this:

$this->getEntityManager()
            ->getRepository(SomeEntity::class)
            ->createQueryBuilder('h')
            ->orderBy('h.id', Order::Descending->value)
            ->getQuery()
            ->getResult();

Yes, I do understand that Collections & ORM are separate, but ORM relays on Collections as dependency.
So can we count on:
a) Allowing Order as orderBy second param
b) or dedicated enum for QB that will replace string.

@derrabus
Copy link
Member

Neither a) nor b) is planned at the moment. That being said, this is the wrong repository for this discussion.

@doctrine doctrine locked and limited conversation to collaborators Mar 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants