• PHP reference
  • Models

User

class User extends AttributedModel

User model.

Properties

integer $id

ModelRelations $relations

string $uuid

DB $db

boolean $anonymous

Whether the user is an anonymous (i.e. not signed in) user.

string $email

string $username

string $name

int $community

string $source

int $time_created

int $suspended

int $superadmin

array $array

string $firstname

string $lastname

Methods

boolean get_from_session(boolean $required = false, integer $community = null)

Get user data from the session. Optionally redirects to login page if not signed in, by setting first parameter to true. Automatically select a community to sign in to by specifying a community ID using the second parameter.

Parameters

  • boolean $required
  • integer $community

Return Value

  • boolean

login(integer $community = null, string $additional_output = '', boolean $http_redir = true)

Redirect to login page, optionally pre-select a certain community.

Parameters

  • integer $community
  • string $additional_output
  • boolean $http_redir

boolean is_connected_to_community(Community|integer $community)

Find out whether this user can sign in to a specific community or community ID.

Parameters

Return Value

  • boolean

Community|boolean get_first_community()

Get the first community the user is part of.

Return Value

array get_communities()

Get a list of all the Communities the user is part of.

This returns an array of arrays, the sub-arrays contain id and role.

Return Value

  • array

boolean is_manager_of_community(Community|integer $community)

Check if the user is a manager of the specified community or community ID.

Parameters

Return Value

  • boolean

string get_username()

Get the user's user name in a format that can be displayed. When no username is set for the current user, a 8-character version of the user ID (with leading zeroes if necessary) is returned.

Return Value

  • string

boolean|mixed security_event(string $type, array $data = [], Community|integer $community = null)

Record a user event.

Parameters

  • string $type
  • array $data
  • Community|integer $community

Return Value

  • boolean|mixed

array get_array()

Get a simple array representation of the user. This associative array contains the user ID (id), their full name (name) and the URL of a 120px wide profile image (picture).

Return Value

  • array

static null|User get_by_username(string $username)

Get a user by their username, otherwise return null.

Parameters

  • string $username

Return Value

static null|User getByEmail(string $email, integer $community = null)

Get a user by their email address, optionally filtering by community they belong to. Returns null when there's no match.

Parameters

  • string $email the user's email address
  • integer $community optionally filter by checking if the user belongs to a certain community ID (community or manager relationship)

Return Value

Inherited from Fillable

static boolean|Fillable create($fields)

Insert a new entry into the model's database table.

Parameters

  • $fields

Return Value

Inherited from Model

__construct(mixed $source = null, boolean $disable_global = false)

Model constructor. Pass in an ID (numeric) or object to start the model.

Parameters

  • mixed $source
  • boolean $disable_global

static Model[] getAll(integer $limit = null)

Get array of all table rows as model instances.

Parameters

  • integer $limit Maximum number of entries to retrieve

Return Value

static integer getCount(array|string $where = null)

Get the number of items in the model that match the query.

Parameters

  • array|string $where Array or SQL query component to build a WHERE ... query.

Return Value

  • integer

static Model|null find(integer $id)

Get the item of this model with the given ID if it exists. Returns null otherwise.

Note that any value passed to this function is converted into an integer!

Parameters

  • integer $id

Return Value

static Model|null findWith(string|array $field, mixed $value = null)

Check if an item with a given field value (exact match) exists.

Parameters

  • string|array $field
  • mixed $value

Return Value

static Model|null getBy(string|array $field, mixed $value = null)

Get item by a specific field (exact match). Returns null on failure.

Parameters

  • string|array $field
  • mixed $value

Return Value

static Model[] getManyBy(string|array $field, mixed $value = null)

Get array of items that have a specific field value (exact match).

Parameters

  • string|array $field
  • mixed $value

Return Value

static boolean exists(integer $id)

Check if an item with a given ID exists.

Parameters

  • integer $id

Return Value

  • boolean

static boolean existsWith(string $field, mixed $value = null)

Check if an item with a given field value (exact match) exists.

Parameters

  • string $field
  • mixed $value

Return Value

  • boolean

boolean update(string|array $field, mixed $value = null)

Update a field of this entry.

Parameters

  • string|array $field
  • mixed $value

Return Value

  • boolean

boolean del()

Delete the resource from the database.

Return Value

  • boolean

object barebones()

Get a bare bones version of the Model (without db, relations, etc.).

Return Value

  • object

Inherited from AttributedModel

mixed attr(string $key, null $value = null)

Get or set a value on the model object.

Parameters

  • string $key
  • null $value

Return Value

  • mixed

mixed val(string $key, null $value = null)

Get or set a value on the model object. Alias for attr().

Parameters

  • string $key
  • null $value

Return Value

  • mixed

array attributes()

Get all metadata values for a model object.

Return Value

  • array

array vals()

Get all metadata values for a model object. Alias for attributes().

Return Value

  • array

static mixed|null getAttribute(string $type, integer $modelId, string $attributeKey, null $scope = null, boolean $cache = true)

Static function to get a parameter without having to retrieve the base model.

Parameters

  • string $type
  • integer $modelId
  • string $attributeKey
  • null $scope
  • boolean $cache

Return Value

  • mixed|null

boolean is(string $key)

Convenience method for testing if a model value is truthy (bool true, string 'true', int 1, string '1').

Parameters

  • string $key

Return Value

  • boolean

Inherited from HasUuid

string getUuid()

Get a user's UUID identifier.

Return Value

  • string

Inherited from HasPicture

string getPicture(integer $size = 300)

Returns the user's profile picture URL. The first parameter allows you to set the desired size at which you want to display it, which helps Scholica return the correct resized version.

Note that the returned image might be larger than the size you request!

Parameters

  • integer $size Minimal size of the resulting image, in pixels (defaults to 300 pixels)

Return Value

  • string

Inherited from HasProfileLink

string getProfileUrl(boolean $managerMode = false)

Get the user's profile URL.

Parameters

  • boolean $managerMode

Return Value

  • string

Inherited from HasExternalAccounts

boolean add_external_account(string $type, mixed $external_id, null $external_token = null)

Add a link with an external account to the current user account.

Parameters

  • string $type
  • mixed $external_id
  • null $external_token

Return Value

  • boolean

array get_external_accounts()

Returns the user's linked accounts as an array of database objects, ordered by descending time created (newest is first item in the array).

Return Value

  • array

static null|User get_by_external_account(string $type, mixed $external_id)

Get a user by their linked account, otherwise returns null.

Parameters

  • string $type
  • mixed $external_id

Return Value

Exceptions