{"id":22957779,"url":"https://github.com/parvezmrobin/db-model","last_synced_at":"2026-05-08T10:33:31.738Z","repository":{"id":57036073,"uuid":"95146198","full_name":"parvezmrobin/DB-Model","owner":"parvezmrobin","description":"Retrieves MySQL database instances in an object oriented way","archived":false,"fork":false,"pushed_at":"2017-11-12T09:53:20.000Z","size":30,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-02T01:46:07.894Z","etag":null,"topics":["database","model","mysql","query"],"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/parvezmrobin.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":"2017-06-22T18:44:47.000Z","updated_at":"2017-06-22T18:56:24.000Z","dependencies_parsed_at":"2022-08-23T21:00:07.950Z","dependency_job_id":null,"html_url":"https://github.com/parvezmrobin/DB-Model","commit_stats":null,"previous_names":[],"tags_count":24,"template":false,"template_full_name":null,"purl":"pkg:github/parvezmrobin/DB-Model","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parvezmrobin%2FDB-Model","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parvezmrobin%2FDB-Model/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parvezmrobin%2FDB-Model/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parvezmrobin%2FDB-Model/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/parvezmrobin","download_url":"https://codeload.github.com/parvezmrobin/DB-Model/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/parvezmrobin%2FDB-Model/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261649843,"owners_count":23189748,"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":["database","model","mysql","query"],"created_at":"2024-12-14T17:20:13.885Z","updated_at":"2026-05-08T10:33:29.473Z","avatar_url":"https://github.com/parvezmrobin.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# DB-Model\n### Retrieves MySQL database instances SIMPLESTly in OOP style\n\u003chr\u003e\n\n#### Initialize\nSet the database name\n\n```\n\\DbModel\\Model::$database = 'database';\n```\n#### Retrieve All Instances\nGet all the instances\n```\n$users = \\DbModel\\Model::all('users')\n```\nNow you can access all attributes of \u003ckbd\u003eUser\u003c/kbd\u003e in an Object oriented fashion.\n```\n$user = $users[0];\necho $user-\u003ename;\necho $user-\u003eemail;\necho $users[2]-\u003ebirth_day;\n```\nDo not want to retrieve all the fields? Just add an additional array or string to \u003ckbd\u003eall()\u003c/kbd\u003e method to denote the fields you need.\n```\n$users = \\DbModel\\Model::all('users', 'name, email');\n$users = \\DbModel\\Model::all('users', ['name', 'email']); // Same as previous\necho $users[0]-\u003ebirth_day // Results error\n```\n#### Retrieve conditionally\nWant to retrieve instances on condition? Use \u003ckbd\u003ewhere()\u003c/kbd\u003e method and give the condition.\n```\n$adults = \\DbModel\\Model::where('users', 'age \u003e 18');\n$maleAdults = \\DbModel\\Model::where('users', 'age \u003e 18 AND sex = \"Male\"');\n$maleAdultNames = \\DbModel\\Model::where('users', 'age \u003e 18 AND sex = \"Male\"', 'name');\n```\n#### Retrieve single instance\nWant to retrieve a single user based on id? Use \u003ckbd\u003efind()\u003c/kbd\u003e method to simplify the action.\n```\n$user = \\DbModel\\Model::find('users', '1');\n$user = \\DbModel\\Model::find('users', 5);\n```\n\u003cblockquote\u003e\nNote that \u003ckbd\u003ewhere()\u003c/kbd\u003e and \u003ckbd\u003eall()\u003c/kbd\u003e method returns array of instances where \u003ckbd\u003efind()\u003c/kbd\u003e method returns a single instance.\n\u003c/blockquote\u003e\n\nIf your primary key is not named 'id', then you have to mention it.\n```\n$user = \\DbModel\\Model::find('users', '1', 'user_id');\n```\nAnd obviously the columns as the last parameter.\n```\n$user = \\DbModel\\Model::find('users', '1', 'user_id', 'first_name, last_name');\n\n$columns = ['first_name', 'last_name'];\n$user = \\DbModel\\Model::find('users', '1', 'user_id', $columns);\n```\n\u003chr\u003e\n\n### Retrieve using relation\n\u003ckbd\u003eDB-Model\u003c/kbd\u003e Supports \u003ckbd\u003eOne to Many\u003c/kbd\u003e, \u003ckbd\u003eMany to One\u003c/kbd\u003e and \u003ckbd\u003eMany to Many\u003c/kbd\u003e relationship. However, you can use these methods to retrieve your \u003ckbd\u003eOne to One\u003c/kbd\u003e relation as well.\n\u003cblockquote\u003e\nNote that \u003ckbd\u003eoneToMany()\u003c/kbd\u003e, \u003ckbd\u003emanyToOne()\u003c/kbd\u003e and \u003ckbd\u003emanytoMany()\u003c/kbd\u003e are instance method. Where \u003ckbd\u003eall()\u003c/kbd\u003e, \u003ckbd\u003ewhere()\u003c/kbd\u003e and \u003ckbd\u003efind()\u003c/kbd\u003e are static method.\n\u003c/blockquote\u003e\n\n#### One to Many\nSay, your \u003ckbd\u003eUser\u003c/kbd\u003e has many \u003ckbd\u003ePost\u003c/kbd\u003es. So, \u003ckbd\u003ePost\u003c/kbd\u003es must have a \u003ckbd\u003euser_id\u003c/kbd\u003e field to store primary key of \u003ckbd\u003eUser\u003c/kbd\u003e. The \u003ckbd\u003euser_id\u003c/kbd\u003e field in \u003c/kbd\u003ePost is said \u003ckbd\u003eForeign Key\u003c/kbd\u003e. Again, \u003ckbd\u003eid\u003c/kbd\u003e of \u003ckbd\u003eUser\u003c/kbd\u003e is said \u003ckbd\u003eReferenced Key\u003c/kbd\u003e as it is referenced by \u003ckbd\u003euser_id\u003c/kbd\u003e in \u003ckbd\u003ePost\u003c/kbd\u003e.\nYou can easily retrieve the \u003ckbd\u003ePost\u003c/kbd\u003es of \u003ckbd\u003eUser\u003c/kbd\u003e using \u003ckbd\u003eoneToMany()\u003c/kbd\u003e method. The simplest form is\n```\n$user = \\DbModel\\Model::find('users', '1');\n$posts = $user-\u003eoneToMany('posts', 'user_id');\n```\nIf primary key of \u003ckbd\u003eUser\u003c/kbd\u003e is not \u003ckbd\u003eid\u003c/kbd\u003e, then you should guess what to pass next.\n```\n$user = \\DbModel\\Model::find('users', '1', 'user_primary_key');\n$posts = $user-\u003eoneToMany('posts', 'user_id', 'user_primary_key');\n\n// To retrieve Post that contains 'ghost' in title\n\n$posts = $user-\u003eoneToMany('posts', 'user_id', 'user_primary_key',\n    'title LIKE \"%ghost%\"');\n\n// To retrieve title and body only\n$posts = $user-\u003eoneToMany('posts', 'user_id', 'user_primary_key',\n    'TRUE', 'title, body');\n```\nIf you are clever enough, you should understand that, \u003ckbd\u003eoneToMany()\u003c/kbd\u003e can also be used to store \u003ckbd\u003eone to one\u003c/kbd\u003e relationship. If \u003ckbd\u003eSettings\u003c/kbd\u003e has an \u003ckbd\u003eone to one\u003c/kbd\u003e relationship with \u003ckbd\u003eUser\u003c/kbd\u003e, then each \u003ckbd\u003eUser\u003c/kbd\u003e will have exactly one \u003ckbd\u003eSettings\u003c/kbd\u003e. So what are you waiting for?\n```\n$user = \\DbModel\\Model::find('users', '1');\n$settings = $user-\u003eoneToMany('settings', 'user_id')[0]; //Retrieves the only settings user have.\n```\nOf course, you can use the rest of parameters al well.\n#### Many to One\nWhat if you need to get the corresponding \u003ckbd\u003eUser\u003c/kbd\u003e instance from a \u003ckbd\u003ePost\u003c/kbd\u003e post instance? \u003ckbd\u003emanyToOne()\u003c/kbd\u003e is here for you with same signature.\n```\n$post = \\DbModel\\Model::find('posts', '1');\n$user = $post-\u003emanyToOne('user', 'user_id');\n```\nIf your \u003ckbd\u003eUser\u003c/kbd\u003e has a different primary key than \u003ckbd\u003eid\u003c/kbd\u003e, then mention that too.\n```\n$post = \\DbModel\\Model::find('posts', '1');\n$user = $post-\u003emanyToOne('user', 'user_id', 'user_primary_key');\n\n// To specify the columns\n\n$user = $post-\u003emanyToOne('user', 'user_id', 'user_primary_key', 'name, email');\n```\nAgain, if you want to retrieve using **inverse** \u003ckbd\u003eone to one\u003c/kbd\u003e relationship, use \u003ckbd\u003emanyToOne()\u003c/kbd\u003e method as well.\n```\n$settings = \\DbModel\\Model::find('settings', '1');\n$user = $post-\u003emanyToOne('user', 'user_id', 'user_primary_key');\n```\n#### Many to Many\nTo implement \u003ckbd\u003eMany to Many\u003c/kbd\u003e relationship in database you need an intermediate table which contains foreign key of both the related tables. Suppose, \u003ckbd\u003ePost\u003c/kbd\u003e has a many to many relationship with \u003ckbd\u003eTag\u003c/kbd\u003e. Then, you need an intermediate table, say \u003ckbd\u003epost_tag\u003c/kbd\u003e which contains foreign key of \u003ckbd\u003ePost\u003c/kbd\u003e and \u003ckbd\u003eTag\u003c/kbd\u003e, say \u003ckbd\u003epost_id\u003c/kbd\u003e and \u003ckbd\u003etag_id\u003c/kbd\u003e. Now, your code will be\n```\n$post = \\DbModel\\Model::find('posts', '1');\n$tags = $post-\u003emanytoMany('tags', 'post_tag', 'post_id', 'tag_id');\n\n// Or inversely\n$tag = \\DbModel\\Model::find('tags', '1');\n$posts = $tag-\u003emanytoMany('posts', 'post_tag', 'tag_id', 'post_id');\n```\nIf the models have different primary key than \u003ckbd\u003eid\u003c/kbd\u003e, then mention it next.\n```\n$post = \\DbModel\\Model::find('posts', '1', 'post_primary_key');\n$tags = $post-\u003emanytoMany('tags', 'post_tag', 'post_id', 'tag_id',\n    'post_primary_key', 'tag_primary_key');\n\n// Or inversely\n$tag = \\DbModel\\Model::find('tags', '1', 'tag_primary_key');\n$posts = $tag-\u003emanytoMany('posts', 'post_tag', 'tag_id', 'post_id',\n    'tag_primary_key', 'post_primary_key');\n    \n// Select conditionally\n$posts = $tag-\u003emanytoMany('posts', 'post_tag', 'tag_id', 'post_id',\n    'tag_primary_key', 'post_primary_key', 'title LIKE \"%ghost%\"');\n\n// Select columns as well\n$posts = $tag-\u003emanytoMany('posts', 'post_tag', 'tag_id', 'post_id',\n    'tag_primary_key', 'post_primary_key',\n     'title LIKE \"%ghost%\"', ['name', 'body']);\n```\n\n### Insertion\nTo insert a \u003ckbd\u003eModel\u003c/kbd\u003e into database, simple just create a \u003ckbd\u003eModel\u003c/kbd\u003e instance. Set the properties. Call the \u003ckbd\u003estore()\u003c/kbd\u003e method.\n```\n$user = new Model();\n$user-\u003ename = 'Dennis Ritchie';\n$user-\u003eemail = 'dennis@example.com';\n$user-\u003epassword = 'pa$$word';\n$user-\u003estore('users');\n```\n\n\u003e \u003ckbd\u003estore()\u003c/kbd\u003e method has two aliases namely \u003ckbd\u003einsert()\u003c/kbd\u003e and \u003ckbd\u003esave()\u003c/kbd\u003e with same signature.\n\n### Updating\nCreate a model. Set only properties to be updated. Call \u003ckbd\u003eupdate()\u003c/kbd\u003e method with table name and the condition on which the update will be performed. And you are done!\n\n```\n$user = new Model();\n$user-\u003ename = 'Christian Bale';\n$user-\u003eupdate('users', 'id = 5');\n```\n\u003eTo avoid mistakes, \u003ckbd\u003eupdate()\u003c/kbd\u003e method does not have a default \u003ckbd\u003e$condition\u003c/kbd\u003e parameter.\n\nWant to update using the primary key? Then you will prefer the \u003ckbd\u003eupdateById()\u003c/kbd\u003e method.\n```\n$user = new Model();\n$user-\u003eid = 5;\n$user-\u003ename = 'Christian Bale';\n$user-\u003eupdateById('users');\n```\nWhen your primary key is not \u003ckbd\u003eid\u003c/kbd\u003e then you have to pass the name of primary key as the next argument.\n```\n$user = new Model();\n$user-\u003eprimary_key = 5;\n$user-\u003ename = 'Christian Bale';\n$user-\u003eupdateById('users', 'primary_key');\n```\n\n### Customizing Connection Params\nThere are five public static field that are used to connect with database. These are \u003ckbd\u003e$host\u003c/kbd\u003e, \u003ckbd\u003e$database\u003c/kbd\u003e, \u003ckbd\u003e$username\u003c/kbd\u003e, \u003ckbd\u003e$password\u003c/kbd\u003e, \u003ckbd\u003e$port\u003c/kbd\u003e. The default values for these fields are\n```\npublic static $host = 'localhost';\npublic static $username = 'root';\npublic static $password = '';\npublic static $port = '3306';\n```\nYou can change these values according to your database.\n### Running Raw Queries\nAlthough, not suggested, you can run raw queries using \u003ckbd\u003e\\DbModel\\Query\u003c/kbd\u003e class. Simply make a \u003ckbd\u003e\\DbModel\\Query\u003c/kbd\u003e instance and run your query.\n```\n$query = \"SELECT * FROM table WHERE 1 = TRUE\"\n$result = (new \\DbModel\\Query('database_name'))-\u003erun($query);\n\nforeach($result as $row) {\n    foreach($row as $key =\u003e $value) {\n        echo $key, ': ', $value;\n    }\n    echo '\u003cbr\u003e';\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparvezmrobin%2Fdb-model","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fparvezmrobin%2Fdb-model","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fparvezmrobin%2Fdb-model/lists"}