What would need to be done is get all the URI Segments into an Array, Pass this Array to the getBySlug function, and then have a recursive loop to check the parent id's of the previous segments.
Using your method, how would it work, Would you get the URI as a string such as /parent/child/ and then compare the URI String to the slug stored in the database which assumingly would also be /parent/child/ ?