{"id":18840043,"url":"https://github.com/alexsasharegan/model-framework","last_synced_at":"2026-04-17T06:33:43.507Z","repository":{"id":56944801,"uuid":"74925808","full_name":"alexsasharegan/model-framework","owner":"alexsasharegan","description":"Make getting started with data-driven apps easier by using some solid model classes and database abstractions.","archived":false,"fork":false,"pushed_at":"2017-11-06T21:50:15.000Z","size":76,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-27T14:31:46.248Z","etag":null,"topics":["database","dbal","framework","micro-framework","orm","php"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alexsasharegan.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-28T01:25:20.000Z","updated_at":"2016-11-30T05:31:57.000Z","dependencies_parsed_at":"2022-08-21T07:50:49.127Z","dependency_job_id":null,"html_url":"https://github.com/alexsasharegan/model-framework","commit_stats":null,"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"purl":"pkg:github/alexsasharegan/model-framework","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexsasharegan%2Fmodel-framework","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexsasharegan%2Fmodel-framework/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexsasharegan%2Fmodel-framework/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexsasharegan%2Fmodel-framework/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alexsasharegan","download_url":"https://codeload.github.com/alexsasharegan/model-framework/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alexsasharegan%2Fmodel-framework/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31918614,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T18:22:33.417Z","status":"online","status_checked_at":"2026-04-17T02:00:06.879Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["database","dbal","framework","micro-framework","orm","php"],"created_at":"2024-11-08T02:44:49.119Z","updated_at":"2026-04-17T06:33:43.490Z","avatar_url":"https://github.com/alexsasharegan.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PHP Model Framework\n\n[![Latest Stable Version](https://poser.pugx.org/alexsasharegan/model-framework/v/stable)](https://packagist.org/packages/alexsasharegan/model-framework)\n[![Total Downloads](https://poser.pugx.org/alexsasharegan/model-framework/downloads)](https://packagist.org/packages/alexsasharegan/model-framework)\n[![Latest Unstable Version](https://poser.pugx.org/alexsasharegan/model-framework/v/unstable)](https://packagist.org/packages/alexsasharegan/model-framework)\n[![License](https://poser.pugx.org/alexsasharegan/model-framework/license)](https://packagist.org/packages/alexsasharegan/model-framework)\n\nA lightweight, PDO \u0026 MySQL-based data modeling framework in PHP strongly inspired by Laravel \u0026 Underscore/Lodash.\n\n## Why Another Library\n\nThis library has been developed in a production environment to alleviate the difficulties of working with PHP and MySQL. \n\nDeveloping in the `[LMW]AMP` stack affords the benefits of a high availability of deployment environments. Unfortunately, this also means dealing with the quirks of an inconsistent PHP language API and poor MySQL data type translations to PHP.\n\nThis is where the **model-framework** comes in. You can work with models \u0026 collections that provide a more consistent API wrapper around PHP, and define casts the work under the hood to enforce the type integrity of the data on your models.\n\nPDO is a great way to interact with MySQL in PHP while simultaneously reducing the potential for injection attacks. This library uses prepared queries under the hood to help you get started with PDO right out of the box. Use simple methods like `Model::removePropsNotInDatabase()` to query your object's table and strip out any fields not defined in your table.\n\nNot familiar with *Object-Oriented PHP*? The model class implements interfaces like **ArrayAccess** and **Traversable** so you can interact with it like a normal associative array, while still getting all the internal data type enforcement.\n\n## Getting Started\nUsing Composer, load up the library:\n```bash\ncomposer require alexsasharegan/model-framework\n```\n\nThis library uses the `vlucas/phpdotenv` library to connect models to your database (_already included_).\nStart by creating a `.env` file with these database connection variables defined:\n```bash\nDB_HOST=localhost\nDB_DATABASE=sandbox\nDB_PORT=3306\nDB_CHARSET=utf8\nDB_USERNAME=root\nDB_PASSWORD=root\n```\nNow load the environment:\n```php\n\u003c?php\n// in your main entry point file, load the environment\n// this technique avoids any global variable pollution\ncall_user_func( function () {\n\t// constructed with the directory containing the `.env` file\n\t$environment = new \\Dotenv\\Dotenv( __DIR__ );\n\t$environment-\u003eload();\n  \n  // also a nice place to set things like timezone \u0026 session\n  date_default_timezone_set('America/Phoenix');\n  session_start();\n} );\n```\nNow extend the abstract `\\Framework\\Model` class to get all the functionality:\n```php\n\u003c?php\n\nuse Framework\\Model;\n\nclass Character extends Model {\n\t\n\t/*\n\t * This is the name of the database table\n\t */\n\tconst TABLE = 'characters';\n\t\n\t/*\n\t * Calling Model::delete() when this is set to true\n\t * will only set the softDeleteFieldName to TRUE and update the db model\n\t */\n\tprotected $useSoftDeletes = TRUE;\n\t\n\tprotected $softDeleteFieldName = 'deleted';\n\t\n\t/*\n\t * Use this to handle time-stamping model creation\n\t * (since MySQL handles updates automatically,\n\t *  but not creation automatically)\n\t */\n\tprotected $timestamp = TRUE;\n\t\n\tprotected $timestampFieldName = 'created_at';\n\t\n\tprotected $casts = [\n\t\t'default_value' =\u003e self::CAST_TO_INT,\n\t\t'deleted'       =\u003e self::CAST_TO_BOOL,\n\t\t'float'         =\u003e self::CAST_TO_FLOAT,\n\t\t'price'         =\u003e self::CAST_TO_PRICE,\n\t\t'special'       =\u003e self::CAST_FROM_JSON_TO_ARRAY,\n\t\t'specialObject' =\u003e self::CAST_FROM_JSON_TO_OBJECT,\n\t];\n}\n\n// Convenient static methods return instances\n// this returns a model instance with the data from this id\n// already parsed, or an empty model\n$character = Character::fetch( 65 );\n\n// Models implement ArrayAccess interface,\n// but they still enforce the internal cast instructions\n// as well as returning NULL when an index isn't defined\n// instead of throwing an exception\n$character['special'] = [\n\t'key'  =\u003e [\n\t\t'nested' =\u003e 'value',\n\t],\n\t'prop' =\u003e TRUE,\n];\n\necho $character-\u003etoJson( JSON_PRETTY_PRINT );\n\n$newCharacter = Character::fetch( 655 );\n$newCharacter-\u003emergeData( [\n\t'first_name' =\u003e 'Jamie',\n\t'last_name'  =\u003e 'Lanaster',\n\t'special'    =\u003e [\n\t\t'house' =\u003e [\n\t\t\t'name'  =\u003e 'Lanaster',\n\t\t\t'words' =\u003e 'A Lanaster always pays his debts.',\n\t\t\t'sigil' =\u003e 'Lion',\n\t\t],\n\t],\n\t'price'      =\u003e 12.3493046,\n\t'deleted'    =\u003e FALSE,\n\t'fakeField'  =\u003e \"I'm not in your database...\",\n] );\n\n\n// a handy dot notation syntax is available from the model as a __get magic method\necho $newCharacter['special.house.name'];\n// Lanaster\n\n// The benefits of the Traversable interface\nforeach ( $newCharacter as $key =\u003e $value )\n{\n\t$type = gettype( $value );\n\techo \"Prop: $key ($type): \" . print_r( $value, TRUE ) . PHP_EOL;\n}\n\necho PHP_EOL;\n// Retrieves the fields from the database,\n// then filters the properties on the object\n// so you can further prevent injection\n// (prepared queries are also used for data binding)\n$newCharacter-\u003eremovePropsNotInDatabase();\nforeach ( $newUser as $key =\u003e $value )\n{\n\t$type = gettype( $value );\n\techo \"Prop: $key ($type): \" . print_r( $value, TRUE ) . PHP_EOL;\n}\necho PHP_EOL;\n\n```\n\n## Dependencies\n\nThis library uses the following dependencies directly:\n\n- [vlucas/phpdotenv](https://github.com/vlucas/phpdotenv)\n\t- environment variable declaration\n- [twig/twig](https://github.com/twigphp/Twig)\n\t- template engine for container\n- [twig/extensions](https://github.com/twigphp/Twig-extensions)\n\t- extra features for template engine\n- [slim/pdo](https://github.com/FaaPz/Slim-PDO)\n\t- PDO extension classes for OOP queries\n\t- Mostly implemented under the hood in the model classes\n- [nesbot/carbon](https://github.com/briannesbitt/Carbon)\n\t- used for timestamping models\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexsasharegan%2Fmodel-framework","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falexsasharegan%2Fmodel-framework","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falexsasharegan%2Fmodel-framework/lists"}