Neuron-PHP

Post extends Model
in package

Post entity representing a blog post.

Attributes
#[Table]
'posts'

Table of Contents

Constants

STATUS_DRAFT  = 'draft'
Post status constants
STATUS_PUBLISHED  = 'published'
STATUS_SCHEDULED  = 'scheduled'

Properties

$_loadedRelations  : array<string|int, mixed>
Loaded relations cache
$_pdo  : PDO|null
Database connection
$_relationCache  : array<string|int, mixed>
Relation metadata cache
$_author  : User|null
$_authorId  : int
$_body  : string
$_categories  : array<string|int, mixed>
$_contentRaw  : string
$_createdAt  : DateTimeImmutable|null
$_excerpt  : string|null
$_featuredImage  : string|null
$_id  : int|null
$_publishedAt  : DateTimeImmutable|null
$_slug  : string
$_status  : string
$_tags  : array<string|int, mixed>
$_title  : string
$_updatedAt  : DateTimeImmutable|null
$_viewCount  : int

Methods

__construct()  : mixed
__get()  : mixed
Magic method to access relations.
addCategory()  : self
Add category
addTag()  : self
Add tag
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 Post from array data
getAttribute()  : mixed
Get an attribute value from the model.
getAuthor()  : User|null
Get author
getAuthorId()  : int
Get author ID
getBody()  : string
Get body content
getCategories()  : array<string|int, Category>
Get categories
getContent()  : array<string|int, mixed>
Get content as array (decoded Editor.js JSON)
getContentRaw()  : string
Get raw content JSON string
getCreatedAt()  : DateTimeImmutable|null
Get created timestamp
getExcerpt()  : string|null
Get excerpt
getFeaturedImage()  : string|null
Get featured image
getId()  : int|null
Get post ID
getPrimaryKey()  : string
Get the primary key column name.
getPublishedAt()  : DateTimeImmutable|null
Get published date
getSlug()  : string
Get slug
getStatus()  : string
Get status
getTableName()  : string
Get the table name for this model from the Table attribute.
getTags()  : array<string|int, Tag>
Get tags
getTitle()  : string
Get title
getUpdatedAt()  : DateTimeImmutable|null
Get updated timestamp
getViewCount()  : int
Get view count
hasCategory()  : bool
Check if post has category
hasTag()  : bool
Check if post has tag
incrementViewCount()  : self
Increment view count
inTransaction()  : bool
Check if currently inside a transaction.
isDraft()  : bool
Check if post is draft
isPublished()  : bool
Check if post is published
isScheduled()  : bool
Check if post is scheduled
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).
removeCategory()  : self
Remove category
removeTag()  : self
Remove tag
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.
setAuthor()  : self
Set author
setAuthorId()  : self
Set author ID
setBody()  : self
Set body content
setCategories()  : self
Set categories
setContent()  : self
Set content from Editor.js JSON string Also extracts plain text to _body for backward compatibility
setContentArray()  : self
Set content from array (will be JSON encoded) Also extracts plain text to _body for backward compatibility
setCreatedAt()  : self
Set created timestamp
setExcerpt()  : self
Set excerpt
setFeaturedImage()  : self
Set featured image
setId()  : self
Set post ID
setLoadedRelation()  : void
Set a loaded relation (used by eager loading).
setPdo()  : void
Set the PDO connection for all models.
setPublishedAt()  : self
Set published date
setSlug()  : self
Set slug
setStatus()  : self
Set status
setTags()  : self
Set tags
setTitle()  : self
Set title
setUpdatedAt()  : self
Set updated timestamp
setViewCount()  : self
Set view count
toArray()  : array<string|int, mixed>
Convert post 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.
extractListText()  : string
Extract text from list items (handles nested structures)
extractPlainText()  : string
Extract plain text from Editor.js JSON content

Constants

STATUS_DRAFT

Post status constants

Use ContentStatus enum instead

public mixed STATUS_DRAFT = 'draft'

STATUS_PUBLISHED

public mixed STATUS_PUBLISHED = 'published'

STATUS_SCHEDULED

public mixed STATUS_SCHEDULED = 'scheduled'

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 = []

$_author

private User|null $_author = null
Attributes
#[BelongsTo]
\Neuron\Cms\Models\User::class
$foreignKey: 'author_id'

$_authorId

private int $_authorId

$_body

private string $_body = ''

$_categories

private array<string|int, mixed> $_categories = []
Attributes
#[BelongsToMany]
\Neuron\Cms\Models\Category::class
$pivotTable: 'post_categories'
$dependent: \Neuron\Orm\DependentStrategy::DeleteAll

$_contentRaw

private string $_contentRaw = '{"blocks":[]}'

$_createdAt

private DateTimeImmutable|null $_createdAt = null

$_excerpt

private string|null $_excerpt = null

$_featuredImage

private string|null $_featuredImage = null

$_id

private int|null $_id = null

$_publishedAt

private DateTimeImmutable|null $_publishedAt = null

$_slug

private string $_slug

$_status

private string $_status = 'draft'

$_tags

private array<string|int, mixed> $_tags = []
Attributes
#[BelongsToMany]
\Neuron\Cms\Models\Tag::class
$pivotTable: 'post_tags'
$dependent: \Neuron\Orm\DependentStrategy::DeleteAll

$_title

private string $_title

$_updatedAt

private DateTimeImmutable|null $_updatedAt = null

$_viewCount

private int $_viewCount = 0

Methods

__construct()

public __construct() : mixed

__get()

Magic method to access relations.

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

addCategory()

Add category

public addCategory(Category $category) : self
Parameters
$category : Category
Return values
self

addTag()

Add tag

public addTag(Tag $tag) : self
Parameters
$tag : Tag
Return values
self

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 Post from array data

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

Associative array of post data

Tags
throws
Exception
Return values
static

getAttribute()

Get an attribute value from the model.

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

getAuthor()

Get author

public getAuthor() : User|null
Return values
User|null

getAuthorId()

Get author ID

public getAuthorId() : int
Return values
int

getBody()

Get body content

public getBody() : string
Return values
string

getCategories()

Get categories

public getCategories() : array<string|int, Category>
Return values
array<string|int, Category>

getContent()

Get content as array (decoded Editor.js JSON)

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

getContentRaw()

Get raw content JSON string

public getContentRaw() : string
Return values
string

getCreatedAt()

Get created timestamp

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

getExcerpt()

Get excerpt

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

getFeaturedImage()

Get featured image

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

getId()

Get post ID

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

getPrimaryKey()

Get the primary key column name.

public static getPrimaryKey() : string
Return values
string

getPublishedAt()

Get published date

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

getSlug()

Get slug

public getSlug() : string
Return values
string

getStatus()

Get 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

getTags()

Get tags

public getTags() : array<string|int, Tag>
Return values
array<string|int, Tag>

getTitle()

Get title

public getTitle() : string
Return values
string

getUpdatedAt()

Get updated timestamp

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

getViewCount()

Get view count

public getViewCount() : int
Return values
int

hasCategory()

Check if post has category

public hasCategory(Category $category) : bool
Parameters
$category : Category
Return values
bool

hasTag()

Check if post has tag

public hasTag(Tag $tag) : bool
Parameters
$tag : Tag
Return values
bool

incrementViewCount()

Increment view count

public incrementViewCount() : 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

isDraft()

Check if post is draft

public isDraft() : bool
Return values
bool

isPublished()

Check if post is published

public isPublished() : bool
Return values
bool

isScheduled()

Check if post is scheduled

public isScheduled() : 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

removeCategory()

Remove category

public removeCategory(Category $category) : self
Parameters
$category : Category
Return values
self

removeTag()

Remove tag

public removeTag(Tag $tag) : self
Parameters
$tag : Tag
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

setAuthor()

Set author

public setAuthor(User|null $author) : self
Parameters
$author : User|null
Return values
self

setAuthorId()

Set author ID

public setAuthorId(int $authorId) : self
Parameters
$authorId : int
Return values
self

setBody()

Set body content

public setBody(string $body) : self
Parameters
$body : string
Return values
self

setCategories()

Set categories

public setCategories(array<string|int, Category$categories) : self
Parameters
$categories : array<string|int, Category>
Return values
self

setContent()

Set content from Editor.js JSON string Also extracts plain text to _body for backward compatibility

public setContent(string $jsonContent) : self
Parameters
$jsonContent : string
Return values
self

setContentArray()

Set content from array (will be JSON encoded) Also extracts plain text to _body for backward compatibility

public setContentArray(array<string|int, mixed> $content) : self
Parameters
$content : array<string|int, mixed>

Content array to encode

Tags
throws
JsonException

If JSON encoding fails

Return values
self

setCreatedAt()

Set created timestamp

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

setExcerpt()

Set excerpt

public setExcerpt(string|null $excerpt) : self
Parameters
$excerpt : string|null
Return values
self

setFeaturedImage()

Set featured image

public setFeaturedImage(string|null $featuredImage) : self
Parameters
$featuredImage : string|null
Return values
self

setId()

Set post ID

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

setLoadedRelation()

Set a loaded relation (used by eager loading).

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

setPdo()

Set the PDO connection for all models.

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

setPublishedAt()

Set published date

public setPublishedAt(DateTimeImmutable|null $publishedAt) : self
Parameters
$publishedAt : DateTimeImmutable|null
Return values
self

setSlug()

Set slug

public setSlug(string $slug) : self
Parameters
$slug : string
Return values
self

setStatus()

Set status

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

setTags()

Set tags

public setTags(array<string|int, Tag$tags) : self
Parameters
$tags : array<string|int, Tag>
Return values
self

setTitle()

Set title

public setTitle(string $title) : self
Parameters
$title : string
Return values
self

setUpdatedAt()

Set updated timestamp

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

setViewCount()

Set view count

public setViewCount(int $viewCount) : self
Parameters
$viewCount : int
Return values
self

toArray()

Convert post 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

extractListText()

Extract text from list items (handles nested structures)

private extractListText(array<string|int, mixed> $items) : string

Editor.js List v1.9+ supports nested lists where items can be:

  • Simple strings: "Item text"
  • Objects with nested items: { "content": "Item text", "items": [nested items] }
Parameters
$items : array<string|int, mixed>

List items array

Return values
string

Extracted text with newlines between items

extractPlainText()

Extract plain text from Editor.js JSON content

private extractPlainText(string $jsonContent) : string
Parameters
$jsonContent : string

Editor.js JSON string

Return values
string

Plain text extracted from blocks


        
On this page

Search results