UserModel::getByUsername PHP 메소드

getByUsername() 공개 메소드

Get a user by their username.
public getByUsername ( string $Username ) : boolean | object
$Username string The username of the user.
리턴 boolean | object Returns the user or **false** if they don't exist.
    public function getByUsername($Username)
    {
        if ($Username == '') {
            return false;
        }
        // Check page cache, then memcached
        $User = $this->getUserFromCache($Username, 'name');
        if ($User === Gdn_Cache::CACHEOP_FAILURE) {
            $this->userQuery();
            $User = $this->SQL->where('u.Name', $Username)->get()->firstRow(DATASET_TYPE_ARRAY);
            if ($User) {
                // If success, cache user
                $this->userCache($User);
            }
        }
        // Apply calculated fields
        $this->setCalculatedFields($User);
        // By default, FirstRow() gives stdClass
        if ($User !== false) {
            $User = (object) $User;
        }
        return $User;
    }

Usage Example

 /**
  * Start a new conversation.
  *
  * @since 2.0.0
  * @access public
  *
  * @param string $Recipient Username of the recipient.
  */
 public function add($Recipient = '')
 {
     $this->permission('Conversations.Conversations.Add');
     $this->Form->setModel($this->ConversationModel);
     // Set recipient limit
     if (!checkPermission('Garden.Moderation.Manage') && c('Conversations.MaxRecipients')) {
         $this->addDefinition('MaxRecipients', c('Conversations.MaxRecipients'));
         $this->setData('MaxRecipients', c('Conversations.MaxRecipients'));
     }
     if ($this->Form->authenticatedPostBack()) {
         $RecipientUserIDs = array();
         $To = explode(',', $this->Form->getFormValue('To', ''));
         $UserModel = new UserModel();
         foreach ($To as $Name) {
             if (trim($Name) != '') {
                 $User = $UserModel->getByUsername(trim($Name));
                 if (is_object($User)) {
                     $RecipientUserIDs[] = $User->UserID;
                 }
             }
         }
         // Enforce MaxRecipients
         if (!$this->ConversationModel->addUserAllowed(0, count($RecipientUserIDs))) {
             // Reuse the Info message now as an error.
             $this->Form->addError(sprintf(plural($this->data('MaxRecipients'), "You are limited to %s recipient.", "You are limited to %s recipients."), c('Conversations.MaxRecipients')));
         }
         $this->EventArguments['Recipients'] = $RecipientUserIDs;
         $this->fireEvent('BeforeAddConversation');
         $this->Form->setFormValue('RecipientUserID', $RecipientUserIDs);
         $ConversationID = $this->Form->save($this->ConversationMessageModel);
         if ($ConversationID !== false) {
             $Target = $this->Form->getFormValue('Target', 'messages/' . $ConversationID);
             $this->RedirectUrl = url($Target);
             $Conversation = $this->ConversationModel->getID($ConversationID, Gdn::session()->UserID);
             $NewMessageID = val('FirstMessageID', $Conversation);
             $this->EventArguments['MessageID'] = $NewMessageID;
             $this->fireEvent('AfterConversationSave');
         }
     } else {
         if ($Recipient != '') {
             $this->Form->setValue('To', $Recipient);
         }
     }
     if ($Target = Gdn::request()->get('Target')) {
         $this->Form->addHidden('Target', $Target);
     }
     Gdn_Theme::section('PostConversation');
     $this->title(t('New Conversation'));
     $this->setData('Breadcrumbs', array(array('Name' => t('Inbox'), 'Url' => '/messages/inbox'), array('Name' => $this->data('Title'), 'Url' => 'messages/add')));
     $this->CssClass = 'NoPanel';
     $this->render();
 }
All Usage Examples Of UserModel::getByUsername
UserModel