### Install wp-orm with Composer Source: https://github.com/rafsan096/wp-orm/blob/master/Home.md This snippet demonstrates how to install the wp-orm library using Composer, a dependency manager for PHP. Ensure Composer is installed on your system before running this command. ```bash composer require dbout/wp-orm ``` -------------------------------- ### Install illuminate/events Composer Package Source: https://github.com/rafsan096/wp-orm/blob/master/Events.md This command installs the `illuminate/events` package, which is required to enable Eloquent event dispatching functionality in the `wp-orm` library. This package provides the necessary event dispatcher component. ```bash composer require illuminate/events ^11.0 ``` -------------------------------- ### Update wp-orm to v4.x using Composer Source: https://github.com/rafsan096/wp-orm/blob/master/Upgrading-from-v3-to-v4.md This command updates the wp-orm library to the latest minor version of v4.x using Composer, ensuring you have the most recent features and fixes for your project. ```bash composer require dbout/wp-orm ``` -------------------------------- ### Initialize Laravel Container for Facades in PHP Source: https://github.com/rafsan096/wp-orm/blob/master/DB-facade.md This PHP snippet demonstrates how to set up the Illuminate Container and bind the `Dbout\WpOrm\Orm\Database` instance to the 'db' key, then associate it with the `Facade` class. This setup is crucial for enabling Laravel-style facades, such as `DB`, in a non-Laravel environment like a WordPress mu-plugin. ```php use Dbout\WpOrm\Orm\Database; use Illuminate\Container\Container; use Illuminate\Support\Facades\Facade; $container = new Container(); $container->instance('db', Database::getInstance()); Facade::setFacadeApplication($container); ``` -------------------------------- ### Include Composer Autoloader in PHP Source: https://github.com/rafsan096/wp-orm/blob/master/Home.md After installing wp-orm with Composer, this PHP snippet shows how to include the Composer autoloader. This step is crucial to make the library's classes available in your PHP scripts. ```php require __DIR__ . '/vendor/autoload.php'; ``` -------------------------------- ### wp-orm v3 to v4 API Migration: Removed Functions and Replacements Source: https://github.com/rafsan096/wp-orm/blob/master/Upgrading-from-v3-to-v4.md This section provides a detailed mapping of functions removed in wp-orm v4 and their equivalent replacements in the new version. This information is crucial for migrating existing codebases from v3 to v4, highlighting necessary API adjustments. ```APIDOC \Dbout\WpOrm\Builders\OptionBuilder::findOneByName() -> \Dbout\WpOrm\Models\Option::findOneByName() \Dbout\WpOrm\Builders\PostBuilder::findOneByName() -> \Dbout\WpOrm\Models\Post::findOneByName() \Dbout\WpOrm\Builders\UserBuilder::findOneByEmail() -> \Dbout\WpOrm\Models\User::findOneByEmail() \Dbout\WpOrm\Builders\UserBuilder::findOneByLogin() -> \Dbout\WpOrm\Models\User::findOneByLogin() \Dbout\WpOrm\Builders\Comment::setAuthor() -> \Dbout\WpOrm\Models\Comment::setCommentAuthor() \Dbout\WpOrm\Builders\Comment::getAuthor() -> \Dbout\WpOrm\Models\Comment::getCommentAuthor() \Dbout\WpOrm\Builders\Comment::setAuthorEmail() -> \Dbout\WpOrm\Models\Comment::setCommentAuthorEmail() \Dbout\WpOrm\Builders\Comment::getAuthorEmail() -> \Dbout\WpOrm\Models\Comment::getCommentAuthorEmail() \Dbout\WpOrm\Builders\Comment::setAuthorUrl() -> \Dbout\WpOrm\Models\Comment::setCommentAuthorUrl() \Dbout\WpOrm\Builders\Comment::getAuthorUrl() -> \Dbout\WpOrm\Models\Comment::getCommentAuthorUrl() \Dbout\WpOrm\Builders\Comment::setAuthorIp() -> \Dbout\WpOrm\Models\Comment::setCommentAuthorIP() \Dbout\WpOrm\Builders\Comment::getAuthorIp() -> \Dbout\WpOrm\Models\Comment::getCommentAuthorIP() \Dbout\WpOrm\Builders\Comment::setContent() -> \Dbout\WpOrm\Models\Comment::setCommentContent() \Dbout\WpOrm\Builders\Comment::getContent() -> \Dbout\WpOrm\Models\Comment::getCommentContent() \Dbout\WpOrm\Builders\Comment::setKarma() -> \Dbout\WpOrm\Models\Comment::setCommentKarma() \Dbout\WpOrm\Builders\Comment::getKarma() -> \Dbout\WpOrm\Models\Comment::getCommentKarma() \Dbout\WpOrm\Builders\Comment::setAgent() -> \Dbout\WpOrm\Models\Comment::setCommentAgent() \Dbout\WpOrm\Builders\Comment::getAgent() -> \Dbout\WpOrm\Models\Comment::getCommentAgent() \Dbout\WpOrm\Builders\Comment::setType() -> \Dbout\WpOrm\Models\Comment::setCommentType() \Dbout\WpOrm\Orm\AbstractModel::getTable() -> No equivalent ``` -------------------------------- ### Specify a Custom Table for WordPress ORM Model (PHP) Source: https://github.com/rafsan096/wp-orm/blob/master/Create-custom-model.md This example shows how to explicitly define a custom table name for a generic WordPress ORM model. By setting the `$table` protected property, you override the default naming convention inferred from the class name. ```PHP use Dbout\WpOrm\Orm\AbstractModel; class MyModel extends AbstractModel { protected $table = 'my_table'; } ``` -------------------------------- ### Using findOneBy Methods for Specific Data Retrieval Source: https://github.com/rafsan096/wp-orm/blob/master/Filter-data.md WP-ORM extends certain models like `User`, `Option`, and `Post` with convenient `findOneBy` magic methods. These methods allow direct retrieval of a single record based on a specific attribute, such as email, login, name, or GUID. ```PHP User::findOneByEmail() User::findOneByLogin() Option::findOneByName() Post::findOneByName() Post::findOneByGuid() ``` -------------------------------- ### Register Eloquent Event Listener with Closure Source: https://github.com/rafsan096/wp-orm/blob/master/Events.md This PHP example shows how to register a closure-based event listener for the `saved` event within an Eloquent model's `boot` method. After setting the event dispatcher, a callback function is defined to execute custom logic whenever a model instance is saved. ```php use Illuminate\Events\Dispatcher; use Dbout\WpOrm\Orm\AbstractModel; class User extends AbstractModel { protected static function boot() { static::setEventDispatcher(new Dispatcher()); parent::boot(); static::saved(function (User $user) { // Add your logic here }); } } ``` -------------------------------- ### Filtering Data with Eloquent Query Builder (where clause) Source: https://github.com/rafsan096/wp-orm/blob/master/Filter-data.md WP-ORM models extend Eloquent's query builder capabilities, allowing you to use standard Eloquent methods like `where` to add constraints to your queries. This example demonstrates filtering posts based on their `ping_status` column. ```PHP use Dbout\WpOrm\Models\Post; $posts = Post::query() ->where('ping_status', 'closed') ->get(); ``` -------------------------------- ### Filtering Models with Meta Relations using addMetaToFilter Source: https://github.com/rafsan096/wp-orm/blob/master/Filter-data.md For WP-ORM models that support meta relations (e.g., `Post`, `User`), you can use the `addMetaToFilter` method to filter records based on their associated meta data. This is particularly useful for custom fields, as shown in the example filtering products by `product_type` meta key. ```PHP $products = Post::query() ->addMetaToFilter('product_type', 'simple') ->get(); ``` -------------------------------- ### Use DB Facade for Raw Database Operations in PHP Source: https://github.com/rafsan096/wp-orm/blob/master/DB-facade.md This PHP snippet shows how to utilize the `Illuminate\Support\Facades\DB` facade after it has been properly initialized. It demonstrates a simple database operation using the `raw()` method, which allows executing raw SQL expressions, in this case, incrementing a count. ```php use \Illuminate\Support\Facades\DB; $count = DB::raw('count + 1'); ``` -------------------------------- ### Configure Eloquent Event Dispatcher in Model Source: https://github.com/rafsan096/wp-orm/blob/master/Events.md This PHP code demonstrates how to set up the event dispatcher within an Eloquent model's `boot` method. It initializes a new `Dispatcher` instance from `illuminate/events` and assigns it to the model using `setEventDispatcher`, enabling event functionality for that model. ```php use Illuminate\Events\Dispatcher; use Dbout\WpOrm\Orm\AbstractModel; class User extends AbstractModel { protected static function boot() { static::setEventDispatcher(new Dispatcher()); parent::boot(); } } ``` -------------------------------- ### Define a Basic Generic WordPress ORM Model (PHP) Source: https://github.com/rafsan096/wp-orm/blob/master/Create-custom-model.md This snippet demonstrates the simplest way to define a generic model in WordPress ORM by extending `Dbout\WpOrm\Orm\AbstractModel`. By default, the table name is inferred from the class name (snake case, plural). ```PHP use Dbout\WpOrm\Orm\AbstractModel; class MyModel extends AbstractModel { } ``` -------------------------------- ### Define a Custom Post Type Model in WordPress ORM (PHP) Source: https://github.com/rafsan096/wp-orm/blob/master/Create-custom-model.md This snippet illustrates how to create a model specifically for a WordPress Custom Post Type. It extends `Dbout\WpOrm\Models\CustomPost` and requires defining the `$_type` protected string property, which automatically filters queries and sets the `post_type` on creation. ```PHP use Dbout\WpOrm\Models\CustomPost; class MyCustomType extends CustomPost { /** * @inheritDoc */ protected string $_type = 'my_customm_type'; } ``` -------------------------------- ### Available Tap Filter Classes in WP-ORM Source: https://github.com/rafsan096/wp-orm/blob/master/Filter-data.md This section lists the various `Tap` classes available in WP-ORM, categorized by the model type they apply to. These classes can be instantiated and passed to the `tap` method to apply specific filtering logic for posts, attachments, comments, and options. ```APIDOC Post Filters: - Dbout\WpOrm\Taps\Post\IsAuthorTap - Dbout\WpOrm\Taps\Post\IsPingStatusTap - Dbout\WpOrm\Taps\Post\IsPostTypeTap - Dbout\WpOrm\Taps\Post\IsStatusTap Attachment Filters: - Dbout\WpOrm\Taps\Attachment\IsMimeTypeTap Comment Filters: - Dbout\WpOrm\Taps\Comment\IsApprovedTap - Dbout\WpOrm\Taps\Comment\IsCommentTypeTap - Dbout\WpOrm\Taps\Comment\IsUserTap Option Filters: - Dbout\WpOrm\Taps\Option\IsAutoloadTap ``` -------------------------------- ### Filtering Data with tap Method (Single Filter) Source: https://github.com/rafsan096/wp-orm/blob/master/Filter-data.md The `tap` method provides a flexible way to inject custom filtering logic into the Eloquent query builder. By instantiating a specific `Tap` class (e.g., `IsAuthorTap`) and passing it to `tap`, you can apply a single, predefined filter to your query. ```PHP use Dbout\WpOrm\Taps\Post\IsAuthorTap; use Dbout\WpOrm\Taps\Post\IsStatusTap; use Dbout\WpOrm\Enums\PostStatus; use Dbout\WpOrm\Models\Post; $posts = Post::query() ->tap(new IsAuthorTap(1)) ->get(); ``` -------------------------------- ### Applying Multiple Filters with tap Method Source: https://github.com/rafsan096/wp-orm/blob/master/Filter-data.md To apply multiple filters using the `tap` method, simply chain multiple `tap` calls. Each call can use a different `Tap` class, allowing you to combine various filtering conditions (e.g., filtering by author ID and post status) on the same query. ```PHP use Dbout\WpOrm\Taps\Post\IsAuthorTap; use Dbout\WpOrm\Taps\Post\IsStatusTap; use Dbout\WpOrm\Enums\PostStatus; use Dbout\WpOrm\Models\Post; $posts = Post::query() ->tap(new IsAuthorTap(1)) ->tap(new IsStatusTap(PostStatus::Publish)) ->get(); ``` -------------------------------- ### Define Attribute Casts in Eloquent Models (PHP) Source: https://github.com/rafsan096/wp-orm/blob/master/Attribute-&-meta-casting.md This PHP snippet demonstrates how to define attribute casting in an Eloquent model using the `protected $casts` property. It shows how to automatically convert `is_admin` to boolean, `settings` to array, and `created_at` to datetime when interacting with the model. ```php class User extends Model { protected $casts = [ 'is_admin' => 'boolean', 'settings' => 'array', 'created_at' => 'datetime', ]; } ``` -------------------------------- ### Define Meta Casts using metaCasts() Method (PHP) Source: https://github.com/rafsan096/wp-orm/blob/master/Attribute-&-meta-casting.md This PHP snippet illustrates how to define meta casting in a WP-ORM model by overriding the `protected function metaCasts(): array` method. It demonstrates casting the `is_admin` meta value to a boolean type, ensuring automatic conversion upon access. ```php namespace App\Models; use Dbout\WpOrm\Models\Post; class User extends Post { protected function metaCasts(): array { return [ 'is_admin' => 'boolean', ]; } } ``` -------------------------------- ### Accessing a Casted Meta Value (PHP) Source: https://github.com/rafsan096/wp-orm/blob/master/Attribute-&-meta-casting.md This PHP snippet shows how to access a meta value that has been configured for casting. After defining the `is_admin` meta to be cast to a boolean, this code retrieves a user and then accesses the `is_admin` meta, which will automatically be returned as a boolean. ```php $user = App\Models\User::find(1); $isAdmin = $user->getMetaValue('is_admin'); ``` -------------------------------- ### Define Meta Casts using $casts Property (PHP) Source: https://github.com/rafsan096/wp-orm/blob/master/Attribute-&-meta-casting.md This PHP snippet demonstrates an alternative way to define meta casting in a WP-ORM model, similar to attribute casting, by using the `protected $casts` property. It shows how to cast the `is_admin` meta value to a boolean type. ```php namespace App\Models; use Dbout\WpOrm\Models\Post; class User extends Post { protected $casts = [ 'is_admin' => 'boolean', ]; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.