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.

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.

http://docs.phalconphp.com/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?



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