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

what could be the reasons that it is impossible to find a record in the database

================================ use Multiple\Frontend\Models\Users; =

Users model:

namespace Multiple\Frontend\Models;

class Users extends \Phalcon\Mvc\Model {

public $id;

public $firstname;

public $lastname;

public $email;

public $birthday;

public $sex;

public $username;

public $password;

public $logins;

public $last_login;

public  $role;

public function getSource()
{
    return 'users';
}

}

Trying to get the data on the condition

$users = Users::find(array( array('username' => 'admin' ) ));

$users->username; // empty

------------ so, too, does not work ----- $users = Users::find(array( "conditions" => array("username" => "admin") ));



98.9k

The reason is that this syntax is not supported:

$users = Users::find(array(
"conditions" => array("username" => "admin")
));


11.9k
edited Oct '14

$users = Users::find(array( array('username' => 'admin' ) )); ??



11.9k

I was doing the documentation as it should be?



98.9k
edited Oct '14

That syntax is not supported, you must use:

$users = Users::find(array(
    "conditions" => "username = 'admin'"
));
$users = Users::find(array(
    "username = 'admin'"
));
$users = Users::find("username = 'admin'");


11.9k

what a dick so it does not work? Why is it not transparent work with the database? I have worked with many ORM but your not be any damn explanation ...

$users = Users::query() ->where("username = :username:") ->andWhere("password = :password:") ->bind(array("username" => "'.$username.'", "password" => "'.$password.'")) ->limit(1) ->execute();



98.9k

Wow man, are you ok? are you expecting get help talking that way to others?

Explanation of what? Can you point me to the docs/post/example where we say that syntax is supported?

What is this? ->bind(array("username" => "'.$username.'", "password" => "'.$password.'"))

Why are you adding quotes to bound parameters? Other ORM you have worked force you to double quote the data?



11.9k

tell me how can substitute a variable?

$robots = Robots::query() ->where("type = :type:") ->andWhere("year < 2000") ->bind(array("type" => "mechanical")) ->order("name") ->execute();



98.9k

This is the condition:

->where("type = :type:") 

It has a placeholder :type:, you pass a value for that using "bind":

->bind(array("type" => "mechanical")) // pass 'mechanical' to replace the :type: placeholder
->bind(array("type" => $type)) // pass a variable with any arbitrary value
->bind(array("type" => $this->request->getPost("type")) // pass a value from the request


11.9k

[Tue, 21 May 13 20:20:19 +0200][INFO] SELECT IF(COUNT(*)>0, 1 , 0) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='users' [Tue, 21 May 13 20:20:19 +0200][INFO] DESCRIBE users [Tue, 21 May 13 20:20:19 +0200][INFO] SELECT users.id, users.role, users.firstname, users.lastname, users.email, users.birthday, users.sex, users.username, users.password, users.logins, users.last_login FROM users WHERE (users.username = :username) AND (users.password = :password) LIMIT 1

$users = Users::query() ->where("username = :username:") ->andWhere("password = :password:") ->bind(array("username" => $username, "password" => $password)) ->limit(1) ->execute();

that is, the data is not replaced by



98.9k

No, Phalcon does not replace the bound parameters, that task is perfomed by the MySQL server which knows the internal details of the active charset avoiding SQL injections.



11.9k

$users = Users::query() ->where("username = :username:") ->andWhere("password = :password:") ->bind(array("username" => $username, "password" => $password)) ->limit(1) ->execute(); //$users -- Phalcon\Mvc\Model\Resultset\Simple $users = $users->toArray();

print_r $users 1 shows...



98.9k

Users::query() will return a resulset of 1 or more records, in your case just use:

//Returns the first record that match the conditions
$user = Users::findFirst(array(
    "username = :username: AND password = :password:",
    "bind" => array("username" => $username, "password" => $password)
));


11.9k

does not work



11.9k

I'd rather use the built-in language



11.9k

$users = Users::find( array( "conditions" => "username = ?1", "bind" => array(1 => $username) ) );

$users = $users->toArray();

Array ( [0] => Array ( [id] => 1 [role] => Administrator [firstname] => not filled [lastname] => not filled [email] => not filled [birthday] => not filled [sex] => not filled [username] => admin [password] => $2a$08$O1BmDY2XkgwjTB1UOEU4DemXMJKtN//TlK4CTvbpBh6 [logins] => 0 [last_login] => 0 ) )

$users = Users::find( array( "conditions" => "username = ?1 AND password = ?2", "bind" => array(1 => $username, 2 => $password) ) );

so I get just 1



11.9k

my brain does not understand how it works for you here.



11.9k

why is everyone so stupid to understand and difficult to