Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
public function usernameAvailable($username)
{
$db = $this->DB();
$result = $db->execute("
SELECT `id`
FROM " . $this->_table . "
WHERE `username` = " . $db->qstr($username)
);
if (!empty($result->fields))
{
// Check if current username is same as username we're checking
if ($this->id)
{
return ($this->username == $username);
}
}
return true;
}
I use it to check if a username exists so I can then display a "username already exists" error to the user or admin when setting up a new member account. I am using it for both the "add member" and "edit member" forms. So on the add form, it only needs to check that the username doesn't exists, but on the edit screen, it needs to check that the username doesn't exists or that it is the username already given to the user.
Would it make more sense to just not apply the validation rule (This method) unless the username has been changed?
The function name doesn't match the code, or rather one part of the code doesn't match the function name. Specifically the return where usernames match.
public function usernameAvailable($username)
{
return count($this->DB()->execute('SELECT `id` FROM '.$this->table.' WHERE `username` = \''. $db->qstr($username).'\'') > 0);
}
feyd wrote:The function name doesn't match the code, or rather one part of the code doesn't match the function name. Specifically the return where usernames match.
// Check if current username is same as username we're checking
if ($user->username != $form['username']->getValue())
{
$form->addRule('username', 'Username "' . $username->getValue('username') . '" is already in use', 'callback', array($user, 'usernameAvailable'));
}
$user = new User($username);
if ($user->isAvailable()) {
eh, I guess that depends on if you think $user->isAvailable makes more sense than $user->usernameAvailable(). to me, user and username aren't synonymous.
If you wanted to be lazy, and you were dealing with a fully fledged model, you could just blindly create the User object, attempt to add it to the database, and then gracefully back out (using try...catch) when the DB complains about a unique key violation (assuming username has been appropriately defined). Saves you an SQL query, although it's a little voodoo-ish. If you think about it, though, it makes intuitive sense.
matthijs wrote:Christopher, did you mean it shouldn't or shouldn't it? Makes a big difference ...
I was supposed to be shouldn't it. And it wasn't the name so much as the parameter that I meant to point out in my example -- isThisUsersUsernameAvailable(). The user should know about itself, otherwise you could just make a procedural helper function.
I might design it with a UserFactory that returned a valid User object or null/error/exception if the requested username was not available.
Last edited by Christopher on Tue Jan 02, 2007 3:52 pm, edited 1 time in total.
matthijs wrote:Christopher, did you mean it shouldn't or shouldn't it? Makes a big difference ...
I was supposed to be shouldn't it. And it wasn't the name so much as the parameter that I meant to point out in my example -- isThisUsersUsernameAvailable()