Download the PHP package mjkhajeh/wporm without Composer

On this page you can find all versions of the php package mjkhajeh/wporm. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package wporm

WPORM - Lightweight WordPress ORM

WPORM is a lightweight Object-Relational Mapping (ORM) library for WordPress plugins. It provides an Eloquent-like API for defining models, querying data, and managing database schema, all while leveraging WordPress's native $wpdb database layer.

wporm

Documentation

Features

Installation

With Composer (Recommended)

You can install WPORM via Composer. In your plugin or theme directory, run:

Then include Composer's autoloader in your plugin bootstrap file:

Manual Installation

  1. Place the ORM directory in your plugin folder.
  2. Include the ORM in your plugin bootstrap:

Defining a Model

Create a model class extending MJ\WPORM\Model:

Note: When using $table in custom SQL queries, do not manually add the WordPress prefix (e.g., $wpdb->prefix). The ORM automatically handles table prefixing. Use $table = (new User)->getTable(); as shown in the next, which returns the fully-prefixed table name.

Schema Management

Create or update tables using the model's up method and the SchemaBuilder:

Unique Indexes (Eloquent-style)

You can add a unique index to a column using Eloquent-style chaining:

For multi-column unique indexes, use:

This works for all column types and matches Eloquent's API.

Basic Usage

Creating a Record

Querying Records

Querying by a Specific Column

You can easily retrieve records by a specific column using the query builder's where method. For example, to get all parts with a specific product_id:

Or, to get the first user by email:

You can also use other comparison operators:

This approach works for any column in your table.

Creating or Updating Records: updateOrCreate

WPORM provides an updateOrCreate method, similar to Laravel Eloquent, for easily updating an existing record or creating a new one if it doesn't exist.

Usage:

This is useful for upsert operations, such as syncing data or ensuring a record exists with certain values.

Creating or Getting Records: firstOrCreate and firstOrNew

WPORM also provides firstOrCreate and firstOrNew methods, similar to Laravel Eloquent, for convenient record retrieval or creation.

firstOrCreate Usage:

firstOrNew Usage:

These methods are useful for ensuring a record exists, or for preparing a new record with default values if not found.

Updating a Record

Deleting a Record

Pagination

WPORM supports Eloquent-style pagination with the following methods on the query builder:

paginate($perPage = 15, $page = null)

Returns a paginated result array with total count and page info:

simplePaginate($perPage = 15, $page = null)

Returns a paginated result array without total count (more efficient for large tables):

See Methods.md for more details and options.

Attribute Casting

Add a $casts property to your model:

Array Conversion and Casting

Example:

Relationships

WPORM supports Eloquent-style relationships. You can define them in your model using the following methods:

All relationship methods return either a model instance or a Collection of models. You can use them just like in Eloquent.

Relationship Existence Filtering: whereHas, orWhereHas, has

Examples:

Custom Attribute Accessors/Mutators

Appended (Computed) Attributes

You can add computed (virtual) attributes to your model's array/JSON output using the $appends property, just like in Eloquent.

Transactions

Custom Queries

You can execute custom SQL queries using the underlying $wpdb instance or by extending the model/query builder. For example:

You can also add custom static methods to your model for more complex queries:

Raw Table Queries with DB::table()

WPORM now supports Eloquent-style raw table queries using the DB class:

See DB.md for more details.

Complex Where Statements

WPORM now supports complex nested where/orWhere statements using closures, similar to Eloquent:

You can still use multiple where calls for AND logic, and orWhere for OR logic:

Note: For very advanced SQL, you can always use $wpdb directly.

You can also use $wpdb directly for complex SQL logic:

Using newQuery()

The newQuery() method returns a fresh query builder instance for your model. This is useful when you want to start a new query chain, especially in custom scopes or advanced use cases. It is functionally similar to query(), but is a common convention in many ORMs.

Example:

You can use newQuery() anywhere you would use query(). Both methods are available for convenience and compatibility with common ORM patterns.

Timestamp Columns

You can customize how WPORM handles timestamp columns in your models. By default, models will automatically manage created_at and updated_at columns if $timestamps = true (the default).

Example: Customizing Timestamp Column Names

With this setup, WPORM will automatically set created_on and changed_on when you create or update an Article record.

Example: Disabling Timestamps

If you do not want WPORM to manage any timestamp columns, set $timestamps = false in your model:

In this case, WPORM will not attempt to set or update any timestamp columns automatically.

Global Scopes

You can define global scopes on your model to automatically apply query constraints to all queries for that model.

Example:

All queries will now include status = 'published' automatically:

To disable global scopes for a query:

To remove a specific global scope at runtime:

Soft Deletes

WPORM supports Eloquent-style soft deletes, allowing you to "delete" records without actually removing them from the database. To enable soft deletes on a model, set the $softDeletes property to true:

Soft Delete Strategies: Timestamp vs Boolean Flag

WPORM supports two soft delete strategies:

  1. Timestamp column (default, Eloquent-style):

    • Uses a deleted_at (or custom) column to store the deletion datetime.
    • Set $softDeletes = true; and (optionally) $deletedAtColumn = 'deleted_at'; in your model.
    • Example:

    • In your migration/schema:
  2. Boolean flag column:

    • Uses a boolean column (e.g., deleted) to indicate soft deletion (1 = deleted, 0 = not deleted).
    • Set $softDeletes = true;, $deletedAtColumn = 'deleted', and $softDeleteType = 'boolean'; in your model.
    • Example:

    • In your migration/schema:

How it works

Example Usage

Troubleshooting & Tips

Contributing

Contributions, bug reports, and feature requests are welcome! Please open an issue or submit a pull request.

Credits

WPORM is inspired by Laravel's Eloquent ORM and adapted for the WordPress ecosystem.


Version

Security Note

Performance Tips

FAQ

Q: Why is my table not created?

Q: How do I debug a failed query?

Q: Can I use this ORM outside of WordPress?

Resources

License Details

This project is licensed under the MIT License. See the LICENSE file or MIT License for details.



All versions of wporm with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package mjkhajeh/wporm contains the following files

Loading the files please wait ....