Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

findById Invalid object ID

Hello,

$user = User::findById('545eb081631d16153a293a66');

  1. works fine if id exists.
  2. return Invalid object ID if no object with that id exists

Is that a bug?

Thanks



85.3k

Sorry, I can't see the differences between both codes



7.0k

I'm sorry. just fixed my post



85.3k

When you say invalid object what are you exactly referring to? If there is no record how an object can be returned?



7.0k
edited Nov '14

$user = User::findById('545eb081631d16153a293a66');

die();

those 2 lines of code throw an exception if id does not exist in database

"Invalid object ID" - this is what I see on the web page.



85.3k

I think a MongoID should be passed: $user = User::findById(\MongoID('545eb081631d16153a293a66'));



7.0k
edited Nov '14

Same behavior:

$user = User::findById(new MongoId('-545eb081631d16153a293a66'));

thrown Invalid Object Id

$user = User::findById(new MongoId('545eb081631d16153a293a66'));

works fine. got an object



7.0k
Accepted
answer

I found a reason of my problem. Id should be exactly 24 characters, otherwise New MongoId throws an exeption.



6.0k

Can you mark your answer/resolution as the acceptable answer, IGonza? :)



7.0k
edited Nov '14

btw, why does phalcon not pass that error?it does not generate an error to apach log.

Edit: My fault. There was try catch...



6.0k

I'm not entirely sure, I haven't used or tried it. I'm simply going through the forum and finding unanswered posts and trying to help them, yours was unanswered but already resolved.

How are you set up to use Mongo? I may be able to look into it but I need to reproduce it first



6.0k

did you maybe forget the actual sample? :)

MongoId regexp /^[a-f\d]{24}$/i