{"id":19626736,"url":"https://github.com/iron-bound-designs/ironbound-db","last_synced_at":"2025-04-28T05:34:00.151Z","repository":{"id":56993539,"uuid":"42843058","full_name":"iron-bound-designs/IronBound-DB","owner":"iron-bound-designs","description":"WordPress DB package.","archived":false,"fork":false,"pushed_at":"2020-02-23T00:00:31.000Z","size":383,"stargazers_count":33,"open_issues_count":4,"forks_count":6,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-08-18T17:58:08.281Z","etag":null,"topics":["orm","wordpress"],"latest_commit_sha":null,"homepage":"ironbound-db.timothybjacobs.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/iron-bound-designs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-09-21T04:03:10.000Z","updated_at":"2024-01-11T19:05:54.000Z","dependencies_parsed_at":"2022-08-21T13:20:39.761Z","dependency_job_id":null,"html_url":"https://github.com/iron-bound-designs/IronBound-DB","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iron-bound-designs%2FIronBound-DB","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iron-bound-designs%2FIronBound-DB/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iron-bound-designs%2FIronBound-DB/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/iron-bound-designs%2FIronBound-DB/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/iron-bound-designs","download_url":"https://codeload.github.com/iron-bound-designs/IronBound-DB/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224098994,"owners_count":17255546,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["orm","wordpress"],"created_at":"2024-11-11T11:47:27.541Z","updated_at":"2024-11-11T11:47:28.540Z","avatar_url":"https://github.com/iron-bound-designs.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# IronBound DB\n\n[![Build Status](https://travis-ci.org/iron-bound-designs/IronBound-DB.svg?branch=master)](https://travis-ci.org/iron-bound-designs/IronBound-DB) [![codecov](https://codecov.io/gh/iron-bound-designs/IronBound-DB/graph/badge.svg)](https://codecov.io/gh/iron-bound-designs/IronBound-DB)\n\n\nIronBound DB is a straightforward, but powerful, ORM for WordPress. It facilitates the creation of custom database tables,\n and bridges the gap between WordPress functionality and IronBound Models.\n\nInstalls with composer.\n\n````\ncomposer require ironbound/db v2.0.0\n````\n\n## Basic Usage\n\n### Relationships\n\n#### One to Many\n```php\n$author = Author::create( array( 'name' =\u003e 'John Doe' ) );\n$author-\u003ebooks-\u003eadd( new Book( array( 'title' =\u003e \"John's Book\" ) ) );\n$author-\u003esave();\n\n// Eager load One-to-Many Books relationship to prevent N+1 problem\n$authors = Author::with( 'books' )-\u003eresults();\n\nforeach ( $authors as $author ) {\n\tforeach ( $author-\u003ebooks as $book ) {\n\t\t$book-\u003eprice += 5.00;\n\t}\n}\n\n$authors-\u003esave();\n\n```\n\n#### Many to Many\n\n```php\n$book1 = Book::create( array( 'title' =\u003e 'Book 1' ) );\n$book2 = Book::create( array( 'title' =\u003e 'Book 2' ) );\n$book3 = Book::create( array( 'title' =\u003e 'Book 3' ) );\n\n$bronx\t\t= Library::create( array( 'name' =\u003e 'Bronx Library' ) );\n$manhattan  = Library::create( array( 'name' =\u003e 'Manhattan Library' ) );\n\n$manhattan-\u003ebooks-\u003eadd( $book2 );\n$manhattan-\u003ebooks-\u003eadd( $book3 );\n$manhattan-\u003esave();\n\n$bronx-\u003ebooks-\u003eadd( $book1 );\n$bronx-\u003ebooks-\u003eadd( $book2 );\n$bronx-\u003esave();\n\n$manhattan-\u003ebooks-\u003eremoveElement( $book3 );\n$manhattan-\u003ebooks-\u003eadd( $book1 );\n$manhattan-\u003esave();\n```\n\n### Interacting with WordPress Models\nModel's can have any of the four WordPress objects (Posts, Users, Comments, Terms) as attributes.\nThey will be saved or created whenever the Model is saved.\n\n```php\n$author = Author::get( 1 );\n$author-\u003euser-\u003edisplay_name = 'John Doe'; // This is a WP_User object\n$author-\u003epicture-\u003epost_title = 'John Doe'; // This is a WP_Post object\n$author-\u003esave(); // The User and Post were automatically saved\n\n// You can have WP objects inserted too\n\n$author = new Author(\n\t'picture' =\u003e new WP_Post( (object) array( \n\t\t'post_type'  =\u003e 'attachment',\n\t\t'post_title' =\u003e 'Jane Doe'\n\t) );\n);\n$author-\u003esave();\n```\n\n### Meta Support\n\n```php\nAuthor::create( array( \n \t'name' =\u003e 'John Doe'\n ) )-\u003eupdate_meta( 'favorite_color', 'blue' );\n\nAuthor::create( array( \n \t'name' =\u003e 'Jane Doe'\n ) )-\u003eupdate_meta( 'favorite_color', 'green' );\n \n$authors = Author::query()-\u003ewhere_meta( array( \n\tarray(\n\t\t'key' \t=\u003e 'favorite_color',\n\t\t'value' =\u003e 'blue'\n\t)\n) )-\u003eresults();\n```\n\n### Events\n\nWhen used with [WPEvents](https://github.com/iron-bound-designs/IronBound-WPEvents), IronBound DB will\nfire events (actions) when various state changes occur.\n\n```php\nBook::created( function( GenericEvent $event ) {\n\t\n\t$book = $event-\u003eget_subject();\n\t// do custom processing...\n} );\n\nBook::saving( function( GenericEvent $event ) {\n\n\t$book = $event-\u003eget_subject();\n\t\n\tif ( $book-\u003eprice === 0 ) {\n\t\tthrow new UnexpectedValueException( 'Books are not allowed to be free!' );\n\t}\n} );\n```\n\n### Foreign Key Constrains\n\nIf the constraint behavior is set to `cascade`, this will delete both the Author and the Book. \n`deleted` events will be fired for each model. Constraints require `WPEvents` be configured.\n\n```php\n$author = Author::create( array( 'name' =\u003e 'John Doe' ) );\n$book \t= Book::create( array( 'title' =\u003e \"John's Book\", 'author' =\u003e $author ) );\n\n$author-\u003edelete();\n```\n\n## Defining Models\n\n```php\nclass Author extends \\IronBound\\DB\\Model\\ModelWithMeta {\n\n\tpublic function get_pk() {\n\t\treturn $this-\u003eid;\n\t}\n\t\n\tprotected function _books_relation() {\n\t\t\n\t\t$relation = new HasMany( 'author', get_class( new Book() ), $this, 'books' );\n\t\t$relation-\u003ekeep_synced();\n\t\t\n\t\treturn $relation;\n\t}\n\t\n\tprotected static function get_table() {\n\t\treturn static::$_db_manager-\u003eget( 'authors' );\n\t}\n\t\n\tpublic static function get_meta_table() {\n    \t\treturn static::$_db_manager-\u003eget( 'authors-meta' );\n    }\n}\n```\n\n## Defining Tables\n\nTables are defined in PHP. Each model is represented by a table. Tables are generally defined by a PHP class.\n\n### Basic Model Tables\n\n```php\nclass Authors extends \\IronBound\\DB\\Table\\BaseTable {\n\n\tpublic function get_table_name( \\wpdb $wpdb ) {\n\t\treturn \"{$wpdb-\u003eprefix}authors\";\n\t}\n\n\tpublic function get_slug() {\n\t\treturn 'authors';\n\t}\n\tpublic function get_columns() {\n\t\treturn array(\n\t\t\t'id'         =\u003e new \\IronBound\\DB\\Table\\Column\\IntegerBased( 'BIGINT', 'id', array( 'unsigned', 'auto_increment' ), array( 20 ) ),\n\t\t\t'name'       =\u003e new \\IronBound\\DB\\Table\\Column\\StringBased( 'VARCHAR', 'name', array(), array( 60 ) ),\n\t\t\t'birth_date' =\u003e new \\IronBound\\DB\\Table\\Column\\DateTime( 'birth_date' ),\n\t\t\t'bio'        =\u003e new \\IronBound\\DB\\Table\\Column\\StringBased( 'LONGTEXT', 'bio' ),\n\t\t\t'picture'    =\u003e new \\IronBound\\DB\\Table\\Column\\ForeignPost( 'picture', new \\IronBound\\DB\\Saver\\PostSaver() ),\n\t\t\t'user'    \t =\u003e new \\IronBound\\DB\\Table\\Column\\ForeignUser( 'user', new \\IronBound\\DB\\Saver\\UserSaver() )\n\t\t);\n\t}\n\n\tpublic function get_column_defaults() {\n\t\treturn array(\n\t\t\t'id'         =\u003e 0,\n\t\t\t'name'       =\u003e '',\n\t\t\t'birth_date' =\u003e '',\n\t\t\t'bio'        =\u003e '',\n\t\t\t'picture'    =\u003e 0,\n\t\t\t'user'\t\t =\u003e 0\n\t\t);\n\t}\n\n\tpublic function get_primary_key() {\n\t\treturn 'id';\n\t}\n\n\tpublic function get_version() {\n\t\treturn 1;\n\t}\n}\n\n\\IronBound\\DB\\Manager::register( new Authors() );\n\\IronBound\\DB\\Manager::register( new BaseMetaTable( new Authors() ) );\n\\IronBound\\DB\\Manager::maybe_install_table( new Authors() );\n\\IronBound\\DB\\Manager::maybe_install_table( new BaseMetaTable( new Authors() ) );\n```\n\n### Foreign Key Constrains\n\n```php\nclass Authors extends \\IronBound\\DB\\Table\\BaseTable implements \\IronBound\\DB\\Table\\ForeignKey\\DeleteConstrained {\n\n\t// ... other methods\n\n\tpublic function get_delete_constraints() {\n\t\treturn array(\n\t\t\t'user' \t  =\u003e self::RESTRICT    // Exception thrown when deleting a User if an author referencing it exists,\n\t\t\t'picture' =\u003e self::SET_DEFAULT // The column will be updated to its default value when its referenced post is deleted \n\t\t);\n\t}\n}\n```\n\n[Learn more about it](https://timothybjacobs.com/2016/07/27/ironbound-db-v2/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firon-bound-designs%2Fironbound-db","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Firon-bound-designs%2Fironbound-db","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Firon-bound-designs%2Fironbound-db/lists"}