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

Binding parameters: String placeholders not working?

I've been trying to figure out what i've been doing wrong, but I guess that string placeholders are just broken.

https://docs.phalcon.io/pt/latest/reference/models.html#binding-parameters

This doesn't seem to work for me:

$user = \user\User::findFirst(['username = :username', ['username' => 'testuser']]);

I get the following error:

Syntax error, unexpected token COLON, near to 'username', when parsing: SELECT [user\User].* FROM [user\User] WHERE username = :username (64)

Numberic place holders do seem to work for me.

$user = \user\User::findFirst(['username = ?1', [1 => 'testuser']]);

Can anyone confirm?



33.8k
Accepted
answer

$user = \user\User::findFirst(["username = :username", ["username" => "testuser"]]);

isn't the same as

$user = \user\User::findFirst(["username = :username:", ["username" => "testuser"]]);

(at least that's what I see). Also, maybe it's more clear with:

$conditions = "username = :username:";
$binds = array(
    "username" => "testuser"
    );
$something = Something::findFirst(
    $conditions,
    $binds
    );

And if you don't get it, you forgot to close the bind parameter with :.

Ah, my bad. I'm coming from a different framework and did not expect the trailing colon. Somehow I looked over it. Thanks RompePC.