Code Monkey home page Code Monkey logo

Comments (8)

willemwollebrants avatar willemwollebrants commented on May 26, 2024 1

I think it's worth looking at how the built-in types handle this. Something like the DateTime is a good example, because it also needs to get a value that is either a DateTime or can be converted to one.

Looking at the code there I see the same logic: if $value cannot be converted to what we need, throw an exception.

(I should probably note that DBAL does not throw an exception if you use the built-int Guid field. It doesn't check anything there and treats the field as a regular text field. )

Another reason why I like having this exception is that you know there will never be a row that matches an invalid uuid, so you can avoid bothering the database in this case.

Lastly, I feel that there is a difference between "there are no rows that match your query" and "your query is invalid", but that depends on the application.

from uuid-doctrine.

ramsey avatar ramsey commented on May 26, 2024 1

@phoenixgao, looks like the ConversionException is the correct approach here. Either catch that exception or use Uuid::isValid() before making your query to determine whether the UUID is valid.

Thanks, everyone, for your help!

from uuid-doctrine.

ramsey avatar ramsey commented on May 26, 2024

My guess is that you're hitting that ConversionException either here (if using type uuid_binary) or here (if using type uuid).

In either case, when preparing the query for the database, the type attempts to convert the value to a UUID for the database, and it fails because the value is invalid.

The ConversionException is from Doctrine\DBAL\Types, and it makes sense when attempting to store a value to the database, but it doesn't necessarily make sense when doing queries. Since I'm not a power Doctrine user, I'll ask around to see how others handle similar cases.

Thanks!

from uuid-doctrine.

lcobucci avatar lcobucci commented on May 26, 2024

@phoenixgao @ramsey it's exactly that... before even running the query, DBAL tries to convert the data into dabase type.

You would never have a null return in that case.

from uuid-doctrine.

ramsey avatar ramsey commented on May 26, 2024

@lcobucci Is there a better way to handle this for queries? I assume the OP's desire is to actually make the query and get back 0 results.

from uuid-doctrine.

lcobucci avatar lcobucci commented on May 26, 2024

Is there a better way to handle this for queries?

That's a good question... I see that conversion error as a precondition for your query and I'd say that we should filter/sanitize the value before reaching that area. But as @willemwollebrants said "that depends on the application".

from uuid-doctrine.

carnage avatar carnage commented on May 26, 2024

I think the conversion exception is correct behaviour here.

Either catch the exception and handle or pre-screen your input for validity.

from uuid-doctrine.

phoenixgao avatar phoenixgao commented on May 26, 2024

Yes at first I was considering the uuid field as just a regular string field, so I expected 0 results from database, but as @willemwollebrants pointed out, even in the database it's a string field, but in php it should be considered as a special uuid field and should let the query hit db only if it is a valid uuid value, since the application should already know that field is a uuid field, then all make sense now.

So for my case I think what I should do is create a validation constraint which calls Uuid::isValid() use the symfony build-in uuid constraints to validate the uuid in the request parameters and return a 400 if it's not valid.

Thanks everyone!

from uuid-doctrine.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.