Neuron-PHP

User extends Model
in package

User entity representing a CMS user.

Attributes
#[Table]
'users'

Table of Contents

Constants

ROLE_ADMIN  = 'admin'
User roles
ROLE_AUTHOR  = 'author'
ROLE_EDITOR  = 'editor'
ROLE_SUBSCRIBER  = 'subscriber'
STATUS_ACTIVE  = 'active'
User statuses
STATUS_INACTIVE  = 'inactive'
STATUS_SUSPENDED  = 'suspended'

Properties

$_loadedRelations  : array<string|int, mixed>
Loaded relations cache
$_pdo  : PDO|null
Database connection
$_relationCache  : array<string|int, mixed>
Relation metadata cache
$_createdAt  : DateTimeImmutable|null
$_email  : string
$_emailVerified  : bool
$_events  : array<string|int, mixed>
$_failedLoginAttempts  : int
$_id  : int|null
$_lastLoginAt  : DateTimeImmutable|null
$_lockedUntil  : DateTimeImmutable|null
$_pages  : array<string|int, mixed>
$_passwordHash  : string
$_posts  : array<string|int, mixed>
$_rememberToken  : string|null
$_role  : string
$_status  : string
$_timezone  : string
$_twoFactorRecoveryCodes  : array<string|int, mixed>|null
$_twoFactorSecret  : string|null
$_updatedAt  : DateTimeImmutable|null
$_username  : string

Methods

__construct()  : mixed
__get()  : mixed
Magic method to access relations.
all()  : array<string|int, mixed>
Get all models.
beginTransaction()  : bool
Begin a database transaction.
commit()  : bool
Commit the current transaction.
create()  : static
Create a new model and save it to the database.
delete()  : bool
Delete the model without handling dependents (simple delete).
destroy()  : bool
Delete the model from the database with dependent cascade.
destroyMany()  : int
Delete one or more models by ID.
exists()  : bool
Check if the model exists in the database.
fill()  : self
Fill the model with an array of attributes.
find()  : static|null
Find a model by primary key.
fromArray()  : static
Create user from array
getAttribute()  : mixed
Get an attribute value from the model.
getCreatedAt()  : DateTimeImmutable|null
Get created at timestamp
getEmail()  : string
Get email
getFailedLoginAttempts()  : int
Get failed login attempts count
getId()  : int|null
Get user ID
getLastLoginAt()  : DateTimeImmutable|null
Get last login at timestamp
getLockedUntil()  : DateTimeImmutable|null
Get locked until timestamp
getPasswordHash()  : string
Get password hash
getPrimaryKey()  : string
Get the primary key column name.
getRememberToken()  : string|null
Get remember token
getRole()  : string
Get user role
getStatus()  : string
Get user status
getTableName()  : string
Get the table name for this model from the Table attribute.
getTimezone()  : string
Get user timezone
getTwoFactorRecoveryCodes()  : array<string|int, mixed>|null
Get two-factor recovery codes
getTwoFactorSecret()  : string|null
Get two-factor secret
getUpdatedAt()  : DateTimeImmutable|null
Get updated at timestamp
getUsername()  : string
Get username
hasTwoFactorEnabled()  : bool
Check if two-factor authentication is enabled
incrementFailedLoginAttempts()  : self
Increment failed login attempts
inTransaction()  : bool
Check if currently inside a transaction.
isActive()  : bool
Check if user is active
isAdmin()  : bool
Check if user is admin
isAuthor()  : bool
Check if user is author
isEditor()  : bool
Check if user is editor
isEmailVerified()  : bool
Check if email is verified
isLockedOut()  : bool
Check if user is locked out
isSuspended()  : bool
Check if user is suspended
limit()  : QueryBuilder
Set result limit.
loadRelations()  : void
Eager load relations.
offset()  : QueryBuilder
Set result offset.
orderBy()  : QueryBuilder
Add an order by clause.
query()  : QueryBuilder
Create a new query builder for this model.
relation()  : BelongsToRelation|HasManyRelation|HasOneRelation|BelongsToManyRelation
Get a relation instance (for calling sync/attach/detach methods).
resetFailedLoginAttempts()  : self
Reset failed login attempts
rollBack()  : bool
Roll back the current transaction.
save()  : bool
Save the model to the database (insert or update).
setAttribute()  : void
Set an attribute value on the model.
setCreatedAt()  : self
Set created at timestamp
setEmail()  : self
Set email
setEmailVerified()  : self
Set email verified status
setFailedLoginAttempts()  : self
Set failed login attempts count
setId()  : self
Set user ID
setLastLoginAt()  : self
Set last login at timestamp
setLoadedRelation()  : void
Set a loaded relation (used by eager loading).
setLockedUntil()  : self
Set locked until timestamp
setPasswordHash()  : self
Set password hash
setPdo()  : void
Set the PDO connection for all models.
setRememberToken()  : self
Set remember token
setRole()  : self
Set user role
setStatus()  : self
Set user status
setTimezone()  : self
Set user timezone
setTwoFactorRecoveryCodes()  : self
Set two-factor recovery codes
setTwoFactorSecret()  : self
Set two-factor secret
setUpdatedAt()  : self
Set updated at timestamp
setUsername()  : self
Set username
toArray()  : array<string|int, mixed>
Convert user to array
transaction()  : mixed
Execute a closure within a database transaction.
update()  : bool
Update the model's attributes and save to database.
where()  : QueryBuilder
Start a query with a where clause.
whereIn()  : QueryBuilder
Start a query with a where in clause.
with()  : QueryBuilder
Eager load relations.
createRelation()  : BelongsToRelation|HasManyRelation|HasOneRelation|BelongsToManyRelation
Create a relation instance.
getForeignKeyName()  : string
Get the foreign key name for this model.
getPdo()  : PDO
Get the PDO connection.
getPivotTableName()  : string
Get the pivot table name for a many-to-many relationship.
getRelatedForeignKeyName()  : string
Get the foreign key name for a related model.
getRelationMetadata()  : array<string|int, mixed>|null
Get relation metadata for a property.
handleDependentRelations()  : void
Handle dependent relations before destroying the model.
performInsert()  : bool
Perform an insert operation.
performUpdate()  : bool
Perform an update operation.
propertyToColumn()  : string
Convert property name to database column name.

Constants

ROLE_ADMIN

User roles

Use UserRole enum instead

public mixed ROLE_ADMIN = 'admin'

ROLE_AUTHOR

public mixed ROLE_AUTHOR = 'author'

ROLE_EDITOR

public mixed ROLE_EDITOR = 'editor'

ROLE_SUBSCRIBER

public mixed ROLE_SUBSCRIBER = 'subscriber'

STATUS_ACTIVE

User statuses

Use UserStatus enum instead

public mixed STATUS_ACTIVE = 'active'

STATUS_INACTIVE

public mixed STATUS_INACTIVE = 'inactive'

STATUS_SUSPENDED

public mixed STATUS_SUSPENDED = 'suspended'

Properties

$_loadedRelations

Loaded relations cache

protected array<string|int, mixed> $_loadedRelations = []

$_pdo

Database connection

protected static PDO|null $_pdo = null

$_relationCache

Relation metadata cache

protected static array<string|int, mixed> $_relationCache = []

$_createdAt

private DateTimeImmutable|null $_createdAt = null

$_email

private string $_email

$_emailVerified

private bool $_emailVerified = false

$_events

private array<string|int, mixed> $_events = []
Attributes
#[HasMany]
\Neuron\Cms\Models\Event::class
$foreignKey: 'created_by'
$dependent: \Neuron\Orm\DependentStrategy::Nullify

$_failedLoginAttempts

private int $_failedLoginAttempts = 0

$_id

private int|null $_id = null

$_lastLoginAt

private DateTimeImmutable|null $_lastLoginAt = null

$_lockedUntil

private DateTimeImmutable|null $_lockedUntil = null

$_pages

private array<string|int, mixed> $_pages = []
Attributes
#[HasMany]
\Neuron\Cms\Models\Page::class
$foreignKey: 'author_id'
$dependent: \Neuron\Orm\DependentStrategy::Nullify

$_passwordHash

private string $_passwordHash

$_posts

private array<string|int, mixed> $_posts = []
Attributes
#[HasMany]
\Neuron\Cms\Models\Post::class
$foreignKey: 'author_id'
$dependent: \Neuron\Orm\DependentStrategy::Nullify

$_rememberToken

private string|null $_rememberToken = null

$_role

private string $_role = 'subscriber'

$_status

private string $_status = 'active'

$_timezone

private string $_timezone = 'UTC'

$_twoFactorRecoveryCodes

private array<string|int, mixed>|null $_twoFactorRecoveryCodes = null

$_twoFactorSecret

private string|null $_twoFactorSecret = null

$_updatedAt

private DateTimeImmutable|null $_updatedAt = null

$_username

private string $_username

Methods

__construct()

public __construct() : mixed

__get()

Magic method to access relations.

public __get(string $name) : mixed
Parameters
$name : string
Tags
throws
RelationException

all()

Get all models.

public static all() : array<string|int, mixed>
Return values
array<string|int, mixed>

beginTransaction()

Begin a database transaction.

public static beginTransaction() : bool
Tags
throws
ModelException

If PDO is not set or transaction fails

Return values
bool

True on success

commit()

Commit the current transaction.

public static commit() : bool
Tags
throws
ModelException

If PDO is not set or commit fails

Return values
bool

True on success

create()

Create a new model and save it to the database.

public static create(array<string|int, mixed> $attributes) : static
Parameters
$attributes : array<string|int, mixed>
Tags
throws
ModelException
Return values
static

delete()

Delete the model without handling dependents (simple delete).

public delete() : bool
Tags
throws
ModelException
Return values
bool

destroyMany()

Delete one or more models by ID.

public static destroyMany(int|array<string|int, mixed> $ids) : int
Parameters
$ids : int|array<string|int, mixed>
Tags
throws
ModelException
Return values
int

Number of records deleted

exists()

Check if the model exists in the database.

public exists() : bool
Return values
bool

fill()

Fill the model with an array of attributes.

public fill(array<string|int, mixed> $attributes) : self
Parameters
$attributes : array<string|int, mixed>
Return values
self

find()

Find a model by primary key.

public static find(int $id) : static|null
Parameters
$id : int
Return values
static|null

fromArray()

Create user from array

public static fromArray(array<string|int, mixed> $data) : static
Parameters
$data : array<string|int, mixed>
Return values
static

getAttribute()

Get an attribute value from the model.

public getAttribute(string $key) : mixed
Parameters
$key : string

getCreatedAt()

Get created at timestamp

public getCreatedAt() : DateTimeImmutable|null
Return values
DateTimeImmutable|null

getEmail()

Get email

public getEmail() : string
Return values
string

getFailedLoginAttempts()

Get failed login attempts count

public getFailedLoginAttempts() : int
Return values
int

getId()

Get user ID

public getId() : int|null
Return values
int|null

getLastLoginAt()

Get last login at timestamp

public getLastLoginAt() : DateTimeImmutable|null
Return values
DateTimeImmutable|null

getLockedUntil()

Get locked until timestamp

public getLockedUntil() : DateTimeImmutable|null
Return values
DateTimeImmutable|null

getPasswordHash()

Get password hash

public getPasswordHash() : string
Return values
string

getPrimaryKey()

Get the primary key column name.

public static getPrimaryKey() : string
Return values
string

getRememberToken()

Get remember token

public getRememberToken() : string|null
Return values
string|null

getRole()

Get user role

public getRole() : string
Return values
string

getStatus()

Get user status

public getStatus() : string
Return values
string

getTableName()

Get the table name for this model from the Table attribute.

public static getTableName() : string
Tags
throws
ModelException
Return values
string

getTimezone()

Get user timezone

public getTimezone() : string
Return values
string

getTwoFactorRecoveryCodes()

Get two-factor recovery codes

public getTwoFactorRecoveryCodes() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|null

getTwoFactorSecret()

Get two-factor secret

public getTwoFactorSecret() : string|null
Return values
string|null

getUpdatedAt()

Get updated at timestamp

public getUpdatedAt() : DateTimeImmutable|null
Return values
DateTimeImmutable|null

getUsername()

Get username

public getUsername() : string
Return values
string

hasTwoFactorEnabled()

Check if two-factor authentication is enabled

public hasTwoFactorEnabled() : bool
Return values
bool

incrementFailedLoginAttempts()

Increment failed login attempts

public incrementFailedLoginAttempts() : self
Return values
self

inTransaction()

Check if currently inside a transaction.

public static inTransaction() : bool
Tags
throws
ModelException

If PDO is not set

Return values
bool

True if inside a transaction

isActive()

Check if user is active

public isActive() : bool
Return values
bool

isAdmin()

Check if user is admin

public isAdmin() : bool
Return values
bool

isAuthor()

Check if user is author

public isAuthor() : bool
Return values
bool

isEditor()

Check if user is editor

public isEditor() : bool
Return values
bool

isEmailVerified()

Check if email is verified

public isEmailVerified() : bool
Return values
bool

isLockedOut()

Check if user is locked out

public isLockedOut() : bool
Return values
bool

isSuspended()

Check if user is suspended

public isSuspended() : bool
Return values
bool

loadRelations()

Eager load relations.

public static loadRelations(array<string|int, mixed> $relations, array<string|int, mixed> $models) : void
Parameters
$relations : array<string|int, mixed>

Relation names to load

$models : array<string|int, mixed>

Array of models to load relations for

orderBy()

Add an order by clause.

public static orderBy(string $column[, string $direction = 'ASC' ]) : QueryBuilder
Parameters
$column : string
$direction : string = 'ASC'
Return values
QueryBuilder

resetFailedLoginAttempts()

Reset failed login attempts

public resetFailedLoginAttempts() : self
Return values
self

rollBack()

Roll back the current transaction.

public static rollBack() : bool
Tags
throws
ModelException

If PDO is not set or rollback fails

Return values
bool

True on success

save()

Save the model to the database (insert or update).

public save() : bool
Tags
throws
ModelException
Return values
bool

setAttribute()

Set an attribute value on the model.

public setAttribute(string $key, mixed $value) : void
Parameters
$key : string
$value : mixed

setCreatedAt()

Set created at timestamp

public setCreatedAt(DateTimeImmutable $createdAt) : self
Parameters
$createdAt : DateTimeImmutable
Return values
self

setEmail()

Set email

public setEmail(string $email) : self
Parameters
$email : string
Return values
self

setEmailVerified()

Set email verified status

public setEmailVerified(bool $emailVerified) : self
Parameters
$emailVerified : bool
Return values
self

setFailedLoginAttempts()

Set failed login attempts count

public setFailedLoginAttempts(int $failedLoginAttempts) : self
Parameters
$failedLoginAttempts : int
Return values
self

setId()

Set user ID

public setId(int $id) : self
Parameters
$id : int
Return values
self

setLastLoginAt()

Set last login at timestamp

public setLastLoginAt(DateTimeImmutable|null $lastLoginAt) : self
Parameters
$lastLoginAt : DateTimeImmutable|null
Return values
self

setLoadedRelation()

Set a loaded relation (used by eager loading).

public setLoadedRelation(string $name, mixed $value) : void
Parameters
$name : string
$value : mixed

setLockedUntil()

Set locked until timestamp

public setLockedUntil(DateTimeImmutable|null $lockedUntil) : self
Parameters
$lockedUntil : DateTimeImmutable|null
Return values
self

setPasswordHash()

Set password hash

public setPasswordHash(string $passwordHash) : self
Parameters
$passwordHash : string
Return values
self

setPdo()

Set the PDO connection for all models.

public static setPdo(PDO $pdo) : void
Parameters
$pdo : PDO

setRememberToken()

Set remember token

public setRememberToken(string|null $rememberToken) : self
Parameters
$rememberToken : string|null
Return values
self

setRole()

Set user role

public setRole(string $role) : self
Parameters
$role : string
Return values
self

setStatus()

Set user status

public setStatus(string $status) : self
Parameters
$status : string
Return values
self

setTimezone()

Set user timezone

public setTimezone(string $timezone) : self
Parameters
$timezone : string
Return values
self

setTwoFactorRecoveryCodes()

Set two-factor recovery codes

public setTwoFactorRecoveryCodes(array<string|int, mixed>|null $twoFactorRecoveryCodes) : self
Parameters
$twoFactorRecoveryCodes : array<string|int, mixed>|null
Return values
self

setTwoFactorSecret()

Set two-factor secret

public setTwoFactorSecret(string|null $twoFactorSecret) : self
Parameters
$twoFactorSecret : string|null
Return values
self

setUpdatedAt()

Set updated at timestamp

public setUpdatedAt(DateTimeImmutable|null $updatedAt) : self
Parameters
$updatedAt : DateTimeImmutable|null
Return values
self

setUsername()

Set username

public setUsername(string $username) : self
Parameters
$username : string
Return values
self

toArray()

Convert user to array

public toArray() : array<string|int, mixed>
Return values
array<string|int, mixed>

transaction()

Execute a closure within a database transaction.

public static transaction(callable $callback) : mixed

If the closure executes successfully, the transaction is committed. If an exception is thrown, the transaction is rolled back and the exception is re-thrown.

Parameters
$callback : callable

Function to execute within transaction

Tags
throws
Throwable

If callback throws an exception

throws
ModelException

If PDO is not set

Return values
mixed

The return value of the callback

update()

Update the model's attributes and save to database.

public update(array<string|int, mixed> $attributes) : bool
Parameters
$attributes : array<string|int, mixed>
Tags
throws
ModelException
Return values
bool

where()

Start a query with a where clause.

public static where(string $column, mixed $operator[, mixed|null $value = null ]) : QueryBuilder
Parameters
$column : string
$operator : mixed
$value : mixed|null = null
Return values
QueryBuilder

whereIn()

Start a query with a where in clause.

public static whereIn(string $column, array<string|int, mixed> $values) : QueryBuilder
Parameters
$column : string
$values : array<string|int, mixed>
Return values
QueryBuilder

with()

Eager load relations.

public static with(array<string|int, mixed>|string $relations) : QueryBuilder
Parameters
$relations : array<string|int, mixed>|string
Return values
QueryBuilder

getForeignKeyName()

Get the foreign key name for this model.

protected getForeignKeyName() : string
Return values
string

getPdo()

Get the PDO connection.

protected static getPdo() : PDO
Tags
throws
ModelException
Return values
PDO

getPivotTableName()

Get the pivot table name for a many-to-many relationship.

protected getPivotTableName(string $relatedModel) : string
Parameters
$relatedModel : string
Return values
string

getRelatedForeignKeyName()

Get the foreign key name for a related model.

protected getRelatedForeignKeyName(string $relatedModel) : string
Parameters
$relatedModel : string
Return values
string

getRelationMetadata()

Get relation metadata for a property.

protected getRelationMetadata(string $propertyName) : array<string|int, mixed>|null
Parameters
$propertyName : string
Return values
array<string|int, mixed>|null

handleDependentRelations()

Handle dependent relations before destroying the model.

protected handleDependentRelations() : void
Tags
throws
RelationException

performInsert()

Perform an insert operation.

protected performInsert() : bool
Tags
throws
ModelException
Return values
bool

performUpdate()

Perform an update operation.

protected performUpdate() : bool
Tags
throws
ModelException
Return values
bool

propertyToColumn()

Convert property name to database column name.

protected propertyToColumn(string $propertyName) : string
Parameters
$propertyName : string
Return values
string

        
On this page

Search results