{"id":16847304,"url":"https://github.com/graup/mongo-php-orm","last_synced_at":"2025-09-14T13:24:07.856Z","repository":{"id":6477746,"uuid":"7717843","full_name":"graup/mongo-php-orm","owner":"graup","description":"Simple and light-weight PHP ORM class for MongoDB","archived":false,"fork":false,"pushed_at":"2014-06-03T07:28:28.000Z","size":237,"stargazers_count":15,"open_issues_count":1,"forks_count":8,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-11T06:36:24.649Z","etag":null,"topics":[],"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/graup.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"license.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-01-20T15:30:25.000Z","updated_at":"2021-05-14T07:49:45.000Z","dependencies_parsed_at":"2022-08-29T16:22:23.952Z","dependency_job_id":null,"html_url":"https://github.com/graup/mongo-php-orm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/graup/mongo-php-orm","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graup%2Fmongo-php-orm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graup%2Fmongo-php-orm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graup%2Fmongo-php-orm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graup%2Fmongo-php-orm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/graup","download_url":"https://codeload.github.com/graup/mongo-php-orm/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/graup%2Fmongo-php-orm/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275111000,"owners_count":25407371,"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","status":"online","status_checked_at":"2025-09-14T02:00:10.474Z","response_time":75,"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":[],"created_at":"2024-10-13T13:07:26.662Z","updated_at":"2025-09-14T13:24:07.817Z","avatar_url":"https://github.com/graup.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MongoDB PHP ORM\n\nThis is a simple class mapping PHP models to MongoDB documents. This way, you can directly save PHP objects into your database, making development even faster.\n\nRequirements: PHP 5.3+\n\n## Motivation\n\nYou cannot directly insert arbitrary PHP objects into MongoDB because the driver cannot handle private or protected attributes. So, before saving, this class filters all public attributes via the Reflection extension available since PHP 5.\n\nIt also abstracts some other tasks which you might encounter while working with the database, for example auto-incrementing integer IDs.\n\nCompared to other MongoDB active-record PHP implementations this strives to be very light-weight.\nIt might not have a ton of features, but will enable you to get started super fast.\nThe class has merely 400 lines of code, including documentation.\n\n## Usage\n\n```php\nrequire_once('mongo_connect.php');\nrequire_once('class.dbobject.php');\n\n// Define model\nclass MyModel extends DBObject {\n\tconst collectionName = 'my_models';\n\tpublic $name;\n\tpublic $description;\n\tprivate $dont_save_this;\n}\n\n// Create model instance\n$obj = new MyModel();\n$obj-\u003ename = \"Foo\";\n$obj-\u003edescription = \"Bar\";\n$obj-\u003eupdate();\n\n// Update model instance\n$obj-\u003esome_other_public_var = \"Baz\";\n$obj-\u003eupdate();\n\n// Find model instances\n$objects = MyModel::search(array('name'=\u003e'Foo'));\nforeach($objects as $instance) {\n\techo is_a($instance, 'MyModel'); // true :)\n}\n```\n\nYou do not need to create any collections, everything is generated on the fly. Rapid development at its best.\n\n## Documentation\n\n### Constants\n\nYour model can define a couple of constants.\n\n* `const collectionName = 'aCollectionName';`\nDefines the MongoDB collection used for this model. Required.\n\n* `const use_sequence_id = true/false;`\nDefines if auto-incrementing, sequential IDs should be used instead of the default MongoID. Optional, defaults to false.\n\n* `const use_random_id = true/false;`\nDefines if a random ID should be used. This is an experimental feature as it is not concurrency proof. Optional, defaults to false.\n\n* `const random_id_length = (int);`\nIf random IDs are used, this defines the length of the generated IDs. 5 means IDs range from 10000 to 99999. Optional, defaults to 6.\n\n### MyModel::__construct( [$id] )\n\nThe constructor retrieves any saved document if an ID is passed. It essentially does a find with the condition being `array('_id' =\u003e $id);` You should pass MongoID objects if you use them, but the class will also automatically convert strings with a matching length to MongoIDs.\n\nOf course, if you have your own constructor, it should look like this:\n\n```php\nfunction __construct($id=null) {\n\tparent::__construct($id);\n\t...\n}\n```\n\t\nThrows a `NoDocumentException` if an ID was passed but no document was found. Suggested usage:\n\n```php\ntry {\n\t$obj = new MyModel($id);\n} catch(NoDocumentException $e) {\n\t// handle error\n}\n```\n\n### MyModel::count( [$query] )\n\nCounts the number of objects in the collection matching the query array.\n\n### MyModel::search( [$query, [$sort, [$opts]]] )\n\nReturns an iterator over MyModel objects matching the query array. Instances are only retrieved when accessed.\n\nFor details on the `$query`, check [MongoCollection::find](http://php.net/manual/en/mongocollection.find.php)\n\n`$sort` is an array just like the [default sort function](http://php.net/manual/en/mongocursor.sort.php) uses.\n\nOptions can be\n\n* `(int) $opts['skip']`: Skip n entries from the result\n* `(int) $opts['limit']`: Limit result to n entries\n\nIf you want all instances at once, use something like `iterator_to_array($iterator);`.\nObviously the iterator approach is preferred for memory reasons.\n\n### MyModel::searchOne( $query )\n\nReturns a single model instance matching the query. Behind the scenes this does the same as `new MyModel($id)`,\nsimply using a custom quert rather than `('_id'=\u003e$id)`.\nBe aware that this does not catch the case of more than one document matching the query. The driver will\nsimply return the first one.\n\nThrows a `NoDocumentException` if no document was found. Suggested usage:\n\n```php\ntry {\n\t$obj = MyModel::searchOne($my_query);\n} catch(NoDocumentException $e) {\n\t// handle error\n}\n```\n\n### $obj-\u003eupdate( [$options] )\n\nUpdates the document if already existing, otherwise performs an insert. Updates are handled asynchronously.\n\nAfter inserting, the `_id` field is added to the object.\nAll public attributes of the object are converted to the document.\n\nOptional parameters:\n\n* `(boolean) $options['force_insert']` (default: false) \nIf true, performs an insert even though the object contained an `_id`. This way you can use your own IDs instead of the default MongoID.\n* `(boolean) $options['preserve_id']` (default: false)\nSet this to true of you want to skip the added functionality of sequential or random IDs.\n\n### $obj-\u003edelete()\n\nRemoves the object from the collection. Returns true on success, false if the object has no ID or something else went wrong.\n\n### MyModel::ensureIndex($array)\n\nA wrapper for [ensureIndex()](http://php.net/manual/en/mongocollection.ensureindex.php) on the collection.\n\n### MyModel::getCollection()\n\nReturns the MongoDB collection object.\n\n### MyModel::getNextId()\n\nReturns the next usable sequential ID and handles auto increment.\nUses the collection name as key in a seperate 'counter' collection.\n\nThis is concurrency proof due to MongoDB's `findandmodify`.\n\nYou don't need to call this function manually, please refer to the Constants section.\n\n### MyModel::getRandomId($min, $max)\n\nReturns a random ID in the range of $min and $max. The function makes sure that the generated ID is not already taken, but is still not concurrency proof, so handle with care.\n\nYou don't need to call this function manually, please refer to the Constants section.\n\n### $obj-\u003egetCreated()\n\nIf MongoIDs are used as _id, this returns the timestamp of the creation date extracted form the ID.\n\n### $obj-\u003etoJSON()\n\nReturns a JSON representation of the object. MongoIDs are converted to Strings.\n\nThe iterator returned by `search()` implements this as well, so you can do something like `MyModel::search()-\u003etoJSON()`.\n\n## Development\n\nI have been using this class in a real application for about a year and never had any problems. I will of course update it whenever possible. In the meantime, I'm also happy to receive comments or pull requests to improve the class, especially since this is my first publicly available code.\n\n## Caveat\n\nOne more thing. Make sure your data is properly UTF-8 formatted as MongoDB cannot handle anything else.\n\n## License\n\n[MIT License](http://opensource.org/licenses/MIT)\n\n## Author\n\nPaul Grau\n\nTwitter: @graup\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraup%2Fmongo-php-orm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgraup%2Fmongo-php-orm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgraup%2Fmongo-php-orm/lists"}