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
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
Return values
bool —True on success
commit()
Commit the current transaction.
public
static commit() : bool
Tags
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
Return values
staticdelete()
Delete the model without handling dependents (simple delete).
public
delete() : bool
Tags
Return values
booldestroy()
Delete the model from the database with dependent cascade.
public
destroy() : bool
Tags
Return values
booldestroyMany()
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
Return values
int —Number of records deleted
exists()
Check if the model exists in the database.
public
exists() : bool
Return values
boolfill()
Fill the model with an array of attributes.
public
fill(array<string|int, mixed> $attributes) : self
Parameters
- $attributes : array<string|int, mixed>
Return values
selffind()
Find a model by primary key.
public
static find(int $id) : static|null
Parameters
- $id : int
Return values
static|nullfromArray()
Create user from array
public
static fromArray(array<string|int, mixed> $data) : static
Parameters
- $data : array<string|int, mixed>
Return values
staticgetAttribute()
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|nullgetEmail()
Get email
public
getEmail() : string
Return values
stringgetFailedLoginAttempts()
Get failed login attempts count
public
getFailedLoginAttempts() : int
Return values
intgetId()
Get user ID
public
getId() : int|null
Return values
int|nullgetLastLoginAt()
Get last login at timestamp
public
getLastLoginAt() : DateTimeImmutable|null
Return values
DateTimeImmutable|nullgetLockedUntil()
Get locked until timestamp
public
getLockedUntil() : DateTimeImmutable|null
Return values
DateTimeImmutable|nullgetPasswordHash()
Get password hash
public
getPasswordHash() : string
Return values
stringgetPrimaryKey()
Get the primary key column name.
public
static getPrimaryKey() : string
Return values
stringgetRememberToken()
Get remember token
public
getRememberToken() : string|null
Return values
string|nullgetRole()
Get user role
public
getRole() : string
Return values
stringgetStatus()
Get user status
public
getStatus() : string
Return values
stringgetTableName()
Get the table name for this model from the Table attribute.
public
static getTableName() : string
Tags
Return values
stringgetTimezone()
Get user timezone
public
getTimezone() : string
Return values
stringgetTwoFactorRecoveryCodes()
Get two-factor recovery codes
public
getTwoFactorRecoveryCodes() : array<string|int, mixed>|null
Return values
array<string|int, mixed>|nullgetTwoFactorSecret()
Get two-factor secret
public
getTwoFactorSecret() : string|null
Return values
string|nullgetUpdatedAt()
Get updated at timestamp
public
getUpdatedAt() : DateTimeImmutable|null
Return values
DateTimeImmutable|nullgetUsername()
Get username
public
getUsername() : string
Return values
stringhasTwoFactorEnabled()
Check if two-factor authentication is enabled
public
hasTwoFactorEnabled() : bool
Return values
boolincrementFailedLoginAttempts()
Increment failed login attempts
public
incrementFailedLoginAttempts() : self
Return values
selfinTransaction()
Check if currently inside a transaction.
public
static inTransaction() : bool
Tags
Return values
bool —True if inside a transaction
isActive()
Check if user is active
public
isActive() : bool
Return values
boolisAdmin()
Check if user is admin
public
isAdmin() : bool
Return values
boolisAuthor()
Check if user is author
public
isAuthor() : bool
Return values
boolisEditor()
Check if user is editor
public
isEditor() : bool
Return values
boolisEmailVerified()
Check if email is verified
public
isEmailVerified() : bool
Return values
boolisLockedOut()
Check if user is locked out
public
isLockedOut() : bool
Return values
boolisSuspended()
Check if user is suspended
public
isSuspended() : bool
Return values
boollimit()
Set result limit.
public
static limit(int $limit) : QueryBuilder
Parameters
- $limit : int
Return values
QueryBuilderloadRelations()
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
offset()
Set result offset.
public
static offset(int $offset) : QueryBuilder
Parameters
- $offset : int
Return values
QueryBuilderorderBy()
Add an order by clause.
public
static orderBy(string $column[, string $direction = 'ASC' ]) : QueryBuilder
Parameters
- $column : string
- $direction : string = 'ASC'
Return values
QueryBuilderquery()
Create a new query builder for this model.
public
static query() : QueryBuilder
Return values
QueryBuilderrelation()
Get a relation instance (for calling sync/attach/detach methods).
public
relation(string $name) : BelongsToRelation|HasManyRelation|HasOneRelation|BelongsToManyRelation
Parameters
- $name : string
-
Relation name
Tags
Return values
BelongsToRelation|HasManyRelation|HasOneRelation|BelongsToManyRelationresetFailedLoginAttempts()
Reset failed login attempts
public
resetFailedLoginAttempts() : self
Return values
selfrollBack()
Roll back the current transaction.
public
static rollBack() : bool
Tags
Return values
bool —True on success
save()
Save the model to the database (insert or update).
public
save() : bool
Tags
Return values
boolsetAttribute()
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
selfsetEmail()
Set email
public
setEmail(string $email) : self
Parameters
- $email : string
Return values
selfsetEmailVerified()
Set email verified status
public
setEmailVerified(bool $emailVerified) : self
Parameters
- $emailVerified : bool
Return values
selfsetFailedLoginAttempts()
Set failed login attempts count
public
setFailedLoginAttempts(int $failedLoginAttempts) : self
Parameters
- $failedLoginAttempts : int
Return values
selfsetId()
Set user ID
public
setId(int $id) : self
Parameters
- $id : int
Return values
selfsetLastLoginAt()
Set last login at timestamp
public
setLastLoginAt(DateTimeImmutable|null $lastLoginAt) : self
Parameters
- $lastLoginAt : DateTimeImmutable|null
Return values
selfsetLoadedRelation()
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
selfsetPasswordHash()
Set password hash
public
setPasswordHash(string $passwordHash) : self
Parameters
- $passwordHash : string
Return values
selfsetPdo()
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
selfsetRole()
Set user role
public
setRole(string $role) : self
Parameters
- $role : string
Return values
selfsetStatus()
Set user status
public
setStatus(string $status) : self
Parameters
- $status : string
Return values
selfsetTimezone()
Set user timezone
public
setTimezone(string $timezone) : self
Parameters
- $timezone : string
Return values
selfsetTwoFactorRecoveryCodes()
Set two-factor recovery codes
public
setTwoFactorRecoveryCodes(array<string|int, mixed>|null $twoFactorRecoveryCodes) : self
Parameters
- $twoFactorRecoveryCodes : array<string|int, mixed>|null
Return values
selfsetTwoFactorSecret()
Set two-factor secret
public
setTwoFactorSecret(string|null $twoFactorSecret) : self
Parameters
- $twoFactorSecret : string|null
Return values
selfsetUpdatedAt()
Set updated at timestamp
public
setUpdatedAt(DateTimeImmutable|null $updatedAt) : self
Parameters
- $updatedAt : DateTimeImmutable|null
Return values
selfsetUsername()
Set username
public
setUsername(string $username) : self
Parameters
- $username : string
Return values
selftoArray()
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
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
Return values
boolwhere()
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
QueryBuilderwhereIn()
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
QueryBuilderwith()
Eager load relations.
public
static with(array<string|int, mixed>|string $relations) : QueryBuilder
Parameters
- $relations : array<string|int, mixed>|string
Return values
QueryBuildercreateRelation()
Create a relation instance.
protected
createRelation(string $propertyName, array<string|int, mixed> $metadata) : BelongsToRelation|HasManyRelation|HasOneRelation|BelongsToManyRelation
Parameters
- $propertyName : string
- $metadata : array<string|int, mixed>
Tags
Return values
BelongsToRelation|HasManyRelation|HasOneRelation|BelongsToManyRelationgetForeignKeyName()
Get the foreign key name for this model.
protected
getForeignKeyName() : string
Return values
stringgetPdo()
Get the PDO connection.
protected
static getPdo() : PDO
Tags
Return values
PDOgetPivotTableName()
Get the pivot table name for a many-to-many relationship.
protected
getPivotTableName(string $relatedModel) : string
Parameters
- $relatedModel : string
Return values
stringgetRelatedForeignKeyName()
Get the foreign key name for a related model.
protected
getRelatedForeignKeyName(string $relatedModel) : string
Parameters
- $relatedModel : string
Return values
stringgetRelationMetadata()
Get relation metadata for a property.
protected
getRelationMetadata(string $propertyName) : array<string|int, mixed>|null
Parameters
- $propertyName : string
Return values
array<string|int, mixed>|nullhandleDependentRelations()
Handle dependent relations before destroying the model.
protected
handleDependentRelations() : void
Tags
performInsert()
Perform an insert operation.
protected
performInsert() : bool
Tags
Return values
boolperformUpdate()
Perform an update operation.
protected
performUpdate() : bool
Tags
Return values
boolpropertyToColumn()
Convert property name to database column name.
protected
propertyToColumn(string $propertyName) : string
Parameters
- $propertyName : string