Create custom methods to check before find
DavidePastore opened this issue · comments
There is some way to create custom methods to check the values before call find_
methods?
An easy example:
<?php
class User extends Model{
public static $_table = "User";
public static $_id_column = "UserID";
/**
* Check if the user can login.
* @return true if the user can login, false otherwise.
*/
public function canLogin($username, $password){
return $this
->where('Username', $username)
->where('Password', $password)
->count() > 0
}
}
and then call it using:
<?php
$username = 'foo';
$password = 'foosecret';
if(Model::factory('User')->canLogin($username, $password)){
echo('You can login.');
}
else{
echo('You can\'t login.');
}
I am not sure I understand what you're asking for, but I would suggest the pattern that I use in these situations:
class User extends Model {
public static $_table = "User";
public static $_id_column = "UserID";
/**
* Check if the user can login.
* @return true if the user can login, false otherwise.
*/
public static function canLogin($username, $password) {
return Model::factory(__CLASS__)
->where('Username', $username)
->where('Password', $password)
->count() > 0;
}
}
$username = 'foo';
$password = 'foosecret';
if(User::canLogin($username, $password)) {
echo('You can login.');
} else{
echo('You can\'t login.');
}
If you're talking about validating the actual values passed into a model then you should see https://github.com/tag/sudzy
Your example it's exactly what i was looking for. Thanks @treffynnon. I think that can be useful for other users. Why don't add it in the documentation?