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() throws an exception on a valid id string

Not sure whether it's even a Phalcon related issue, though I've never run into this before on other frameworks (or plain php). So maybe you guys faced similar problem before.

Basically I've got a function that is trying to read and decrypt cookies (and then log the user in). It breaks at the last line of below code, when trying to create MongoId of $userId:

public function loginWithRememberMe()
{

            $key = \Phalcon\DI::getDefault()->get('crypt')->getKey();
            $crypt = new Phalcon\Crypt();

            $userId = $crypt->decrypt($this->cookies->get('RMU')->getValue(), $key);
            $cookieToken = $crypt->decrypt($this->cookies->get('RMT')->getValue(), $key);

            if($userId !== null && $cookieToken !== null)
            {
                $users = new User();
                $user = $users->findById( $userId );

I am able to see the $userId when I echo it, but when passing it to findById it throws following exception:

MongoException: Invalid object ID

  • string itself is a valid id, as I have tested it running the query manually
  • if I hardcode it inside findById, it works fine
  • function is in a library that extends Phalcon\Component
  • it is called at the very beginning in my ControllerBase (base of all my controllers)
  • cookies are present in browser
  • encryption/decryption seems to work fine (using Phalcon\Crypt)
  • id in question: 4f20835f97c7142a30000c9b (it is also visible in stacktrace)

Looks to me like a scope issue which I am failing to notice.

edited Aug '14

I guess you should use

$user = $users->findFirstById( $userId );

Because Phalcon allows search by table attributes only on FindFirst method, and guess its the result you need, isnt it? Maybe I'm wrong..



765

Why am I thinkig that those methods are static methods...

User::findById(...);

? I am not sure, but in case that find, findFirst, count and so on are static methods, I think that this is static too.

Also, you can use

User::findFirst($userId);


2.0k
edited Aug '14

I've figured out what was causing the error.

echo strlen($userId); // outputs 32 whereas it should be 24

There were some "invisible" characters in $userId string that would not come up even in var_dump() output.

Running:

$userId = preg_replace('/[^A-Za-z0-9]/', '', $userId);

solved the issue.

Not sure if it's a Phalcon Crypt problem or if I've missed something when it comes to handling cookies.



82.7k
Accepted
answer

Block cipher modes for symmetric-key encryption algorithms in ECB/CBC modes require plain text input that is a multiple of the block size, so messages must be padded to bring them to this length when they're decripted. This is why you find extra zero null characters at the end of the input.