{"id":23806839,"url":"https://github.com/mattvb91/lightmodel","last_synced_at":"2025-11-10T21:34:12.106Z","repository":{"id":62525384,"uuid":"93873742","full_name":"mattvb91/LightModel","owner":"mattvb91","description":"A basic PHP ORM class","archived":false,"fork":false,"pushed_at":"2018-10-15T17:15:31.000Z","size":39,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-01T23:15:27.184Z","etag":null,"topics":["mysql","pdo","pdo-mysql","pdo-wrapper","php","php-library","php7"],"latest_commit_sha":null,"homepage":"","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/mattvb91.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-06-09T15:43:17.000Z","updated_at":"2022-02-12T08:58:15.000Z","dependencies_parsed_at":"2022-11-02T14:15:58.302Z","dependency_job_id":null,"html_url":"https://github.com/mattvb91/LightModel","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattvb91%2FLightModel","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattvb91%2FLightModel/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattvb91%2FLightModel/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mattvb91%2FLightModel/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mattvb91","download_url":"https://codeload.github.com/mattvb91/LightModel/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240059756,"owners_count":19741719,"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":["mysql","pdo","pdo-mysql","pdo-wrapper","php","php-library","php7"],"created_at":"2025-01-01T23:15:53.189Z","updated_at":"2025-11-10T21:34:12.061Z","avatar_url":"https://github.com/mattvb91.png","language":"PHP","readme":"\u003cp align=\"center\"\u003e\u003cb\u003eLightModel\u003c/b\u003e\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003cimg src='https://coveralls.io/repos/github/mattvb91/LightModel/badge.svg?branch=master'/\u003e\n  \u003cimg src=\"https://travis-ci.org/mattvb91/LightModel.svg?branch=master\"\u003e\n  \u003cimg class=\"latest_stable_version_img\" src=\"https://poser.pugx.org/mattvb91/lightmodel/v/stable\"\u003e\n  \u003cimg class=\"total_img\" src=\"https://poser.pugx.org/mattvb91/lightmodel/downloads\"\u003e\n  \u003cimg class=\"latest_unstable_version_img\" src=\"https://poser.pugx.org/mattvb91/lightmodel/v/unstable\"\u003e\n  \u003cimg class=\"license_img\" src=\"https://poser.pugx.org/mattvb91/lightmodel/license\"\u003e\n\u003c/p\u003e\n\n## What is LightModel?\n\nThe LightModel ORM is test project to build an ActiveRecord \nstyle implementation from scratch.\n\nUsing this in a live project is not recommended. Please look at Eloquent or Zend Model for \na robust and highly tested solution.\n\n## Usage\n\nTo initialize LightModel you need to call ```LightModel::init();``` and pass your instance\nof PDO to it.\n\n```php\n$pdo = new PDO(/* Your PDO params*/);\nLightModel::init($pdo);\n     \n```\n\nYou can also pass in an optional array for further configuration. For example:\n\n```php\n$options = [\n    LightModel::LightModel::OPTIONS_TYPECAST,\n];\n\nLightModel::init($pdo, $options);\n```\nCurrently the typecast option is the only option available. If used this will typecast Integer columns\ndefined in your MySQL database to be an integer attribute on your model.\n\n\nTo get started with a model, your class needs to extend ```mattvb91\\LightModel\\LightModel```\n\n### Creating a Model\n\n```php\nnamespace Project;\n\nuse mattvb91\\LightModel\\LightModel;\n\nclass User extends LightModel\n{\n\n    //\n}\n\n```\n\nYou will need to implement the ```getValues()``` function in which you define a key value \narray for the values associated with your columns.\n\nFor example a basic ```User``` table with with the following structure could be represented\nlike this:\n\n```mysql\n\n| id (int) | username (varchar) |\n```\n\n```php\nnamespace Project;\n\nuse mattvb91\\LightModel\\LightModel;\n\nclass User extends LightModel\n{\n    public $username;\n    \n    public function getValues()\n    {\n        return [\n            'username' =\u003e $this-\u003eusername,\n        ];\n    }\n}\n\n```\n\nYou do not need to manually bind the primary key column if it is set up as an auto increment\nvalue in your DB. \n\nYou can of course use your normal getters to access your column values too inside the \n```getValues()``` method as long as you bind it to your correct column.\n\nTo create a new row on your user table you would use the following:\n\n```php\n$user = new User();\n$user-\u003eusername = 'Name';\n$user-\u003esave();\n```\n\nTo override the table name or table key simply implement the following in your class:\n\n```php\n    protected $tableName = 'new_name';\n    protected $key = 'new_key';\n```\n\n### Fetch a row by key\n\nTo fetch a row by its key use the static ```Class::getOneByKey($key)``` on the class you want to fetch.\nFor example:\n\n```php\n$user = User::getOneByKey(1);\n//$user now loaded with values from DB\n```\n\n### Check a model exists\n\nTo check if a model actually exists in your database you can use the ```exists()``` method.\n\n```php\n$user-\u003eexists(); //Returns true or false\n```\n\n### Refresh a model\n\nYou may run into situations where you need to fetch the latest version of your row again.\nUse the ```refresh()``` method to update your current model. \n\nKeep in mind this will set your model back to whats currently in your DB.\n\n```php\n$user-\u003erefresh();\n```\n\n### Delete a model\n\nTo delete a model simply call the ```delete()``` method:\n\n```php\n$user-\u003edelete();\n```\n\n## Relationships\n\n### BelongsTo\n\nTo define a Belongs to relationship use the ```belongsTo($class, $foreignKey)``` method in your model.\nFor example if our User is associated with a Department you could do the following\ninside your User class.\n\nOnce a relationship has been queried once any further accesses to not hit the database again.\n\n```php\n\npublic function department() \n{\n    return $this-\u003ebelongsTo(Department::class, 'department_id');\n    //returns a loaded Department::class instance\n}\n\n```\n\n## Fetching multiple rows\n\nTo fetch multiple rows simply use the ```static::getItems()``` method.\n\n```php\n$users = User::getItems();\n```\n\n## Filtering\n\nYou can also filter data sets by passing an optional array int the ```static::getItems()```\nmethod. You must pass the correct table column name.\n\n```php\n$filter = [\n    'username' =\u003e 'joe'\n];\n\n$allJoeUsers = User::getItems($filter);\n\n```\n\nOptionally you can also pass in the operator you want to perform. \nThe order MUST be Table_Column =\u003e ['Operator', 'Value']\n```php\n$filter = [\n    'username' =\u003e ['\u003e=', 'joe']\n];\n\n$allJoeUsers = User::getItems($filter);\n\n```\n\nTo set the order or limit the results returned you can make use of the\n```LightModel::FILTER_ORDER``` and ```LightModel::FILTER_LIMIT``` constants\nand pass them in your options array:\n\n```php\n$filter = [\n    LightModel::FILTER_ORDER =\u003e 'id DESC',\n    LightModel::FILTER_LIMIT =\u003e 100;\n];\n\n$filteredUsers = User::getItems($filter);\n```\n\n## Fetching keys\n\nYou may sometimes run into situations where performing a ```static::getItems()``` brings back\ntoo large of a resultset. You can instead perform the same filters using ```static::getKeys()``` which\ninstead of returning an array of fully loaded Models it will now only return the unique column ID's that fit your\nfiltered criteria. You can then use that ID to individually load the full model manually:\n\n```php\n\n$filter = [\n    LightModel::FILTER_ORDER =\u003e 'id DESC',\n];\n\n$userKeys = User::getKeys($filter);\n//We now have an array consisting of all the primary keys that\n//match our criteria\n\nforeach($userKeys as $primaryKey) \n{\n    //Load the full individual record for further processing\n    $user = User::getOneByKey($primaryKey);\n}\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattvb91%2Flightmodel","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmattvb91%2Flightmodel","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmattvb91%2Flightmodel/lists"}