We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

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



98.9k

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



10.7k

I'm sorry. just fixed my post



98.9k

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



10.7k
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.



98.9k

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



10.7k
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



10.7k
Accepted
answer

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



6.9k

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



10.7k
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.9k

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.9k

did you maybe forget the actual sample? :)

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