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

db error

when i want to use findfirst i got this error:

Fatal error: Call to undefined method User::findFirst() in C:\wamp\www\soscial\app\controllers\LoginController.php on line 78

$user = User::findFirst("email='$email' AND password='$password' AND show_admin='1'");

this is my code. and i have an model User.php

how can i solve this?

@geass

Check whether the autoloader works.

Also does your user class extend model ?



39.3k

loader is working , and yes class User extends \Phalcon\Mvc\Model

. But there ar smt i think , when i delete the example database password there ar no error about database ?



39.3k

i dont know about what the problem but when i change User class to Users it's solved ?



39.3k

i ask so much question sorry but i want to learn this

i have users table and on it has "user_type" column. and on "user_types" table. on user_type there ar a row like that ; id = 1, name = admin, and on users table there ar row id = 1 user_type = 1 (i hope i can explain it)

on the login i want to findFirst (email = $email, $password = $password and name = admin(its from user_type) ) how can i do that

(i mean exactly : select * from users inner join user_types on user_types.id = users.user_type where email = $email and password = $password and user_types.name = 'admin')

so thanks



98.9k

Something like this must work, remember define the relationships between both models in the class definitions:

$user = $this->modelsManager->createBuilder()   
    ->from('Users')
    ->join('UserTypes')
    ->where('Users.email = :email:')
    -andWhere('Users.password = :password:')
    ->getQuery()
    ->execute(array('email' => $email, 'password' => $password))
    ->getFirst();


39.3k

how can i write the sql query after that to test ? (i mean get last query)