{"id":20023977,"url":"https://github.com/thebojda/lworm","last_synced_at":"2026-05-13T03:32:09.542Z","repository":{"id":31747192,"uuid":"35313274","full_name":"TheBojda/lworm","owner":"TheBojda","description":"Automatically exported from code.google.com/p/lworm","archived":false,"fork":false,"pushed_at":"2015-05-09T14:04:47.000Z","size":156,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-12T16:43:05.833Z","etag":null,"topics":[],"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/TheBojda.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":"2015-05-09T03:24:03.000Z","updated_at":"2015-05-09T03:29:25.000Z","dependencies_parsed_at":"2022-08-29T16:00:53.604Z","dependency_job_id":null,"html_url":"https://github.com/TheBojda/lworm","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheBojda%2Flworm","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheBojda%2Flworm/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheBojda%2Flworm/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TheBojda%2Flworm/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TheBojda","download_url":"https://codeload.github.com/TheBojda/lworm/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241455592,"owners_count":19965637,"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":[],"created_at":"2024-11-13T08:48:50.430Z","updated_at":"2026-05-13T03:32:04.515Z","avatar_url":"https://github.com/TheBojda.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"lworm is a lightweight ORM (Object Relational Mapping) tool for PHP, supporting Google App Engine Datastore and simple SQL architectures.\n\n# Generating models\n\nFirstly, you need to define the entity structure in YAML format.\n\n```yaml\nUser: \n  fields: \n    name: text\n    password: text\n    active: boolean\n    \n  relations: \n    many-to-many: \n      roles: Role(user_roles)\n\n    many-to-one: \n      group: Group\n\nRole:\n  fields:\n    name: text\n  relations: \n    many-to-many: \n      users: User(user_roles)\n\nGroup: \n  fields: \n    name: text\n    \n  relations: \n    one-to-many: \n      users: User(group)\n```\n\nEvery entity is defined by the fields and relations. Fields are simple name-value pairs, defines the field name and type. Relations defines the relations between entities, which can be one-to-many, many-to-one and many-to-many. The relation is also defined by the name value pair, where the name is the name of the relation field, and the value is the referenced entity type.\n\nIf the relation type is one-to-many, you have to define a many-to-one relation in the referenced entity, and define the referring field in brackets. Look at the example:\n\n```yaml\nUser: \n  ...\n  relations: \n    many-to-one: \n      group: Group\n\nGroup: \n  ...\n  relations: \n    one-to-many: \n      users: User(group)\n``` \n\nThis example shows the definition of the 'group' relation field in the User entity, which points to the user's group. On the other side, Group has a 'users' field which points to the User entities where the group field is pointing to the Group.\n\nMany to many relations uses similar method. When you define a many to many relation, you have to define a unique relation name.\n\n```yaml\nUser: \n  ...\n  relations: \n    many-to-many: \n      roles: Role(user_roles)\n\nRole:\n  ...\n  relations: \n    many-to-many: \n      users: User(user_roles)\n```\n\nIn this example the User entity has more than one role. User entities are accessible from the Role, and the relation name is user_role.\n\nlworm has a model generator, which generates PHP entity class sources based on the YAML definition. \n\n```php\n  require_once \"lib/lworm/ModelGenerator.class.php\";\n\t\n  $mg = new ModelGenerator();\n  $mg-\u003egenerateModel('test.yaml', 'model');\n```\n\nThis code will generate the model classes to the model folder, based on the test.yaml file. After the model generation, the classes can be included in the standard way:\n\n```php\n require_once \"model/User.class.php\";\n require_once \"model/Role.class.php\";\n require_once \"model/Group.class.php\";\n```\n\n# Using the datastore\n\nFor saving, loading, removing and querying entities, you need to instantiate a datastore.\n\n```php\n  $ds = MySQLDataStoreFactory::getDataStore('localhost', 'lworm', 'root', '');\n  $ds-\u003ecreateSchema('test.yaml');\n```\n\nEvery datastore type has its own Factory class. This example instantiates a MySQL datastore, and creates the database schema based on the YAML configuration.\n\n# Using the entities\n\nThe entity usage is really simple. Simply instantiate the entity object and set the properties. When the entity is saved by the datastore, it gets an id. \n\n```php\n  $user = new User;\n  $user-\u003esetName('Test User');\n  $ds-\u003esave($user);\n```\n\nlworm entities are only memory objects (you can serialize them, store them in the session, transport them from/to the client side, etc.) For this reason if you change anything in the entity, you have to save it again to update the database.\n\n```php\n  $user = new User;\n  $user-\u003esetName('Test 2 User');\n  $ds-\u003esave($user);\n```\n\nThe datastore save method creates the entity if it hasn't id, and update it if the id is already set.\n\n# Relations\n\nRelations are accessible through special methods of the entity. The relation method needs the actual datastore as a parameter because entities not exactly attached to any datastore. lworm supports three relation type.\n\n## Many to One\n\nIn a many to one relation the source entity points to one foreign entity. This entity can be set/get by the relation methods.\n\n```php\n  $user-\u003egetGroupRelation($ds)-\u003esetEntity($group);\n  var_dump($user-\u003egetGroupRelation($ds)-\u003egetEntity());\n```\n\n## One to Many\n\nOne to many relation is the other side of the defined many to one relation. In this case it is only possible to get the related entities.\n\n```php\n  var_dump($group-\u003egetUsersRelation($ds)-\u003egetEntities());\n```\n\n## Many to Many\n\nIn the many to many relation, the entity has a list of accessed entities. You can add the referenced entity, delete it, and get the list of assigned entities.\n\n```php\n  $user-\u003egetRolesRelation($ds)-\u003eaddEntity($role);\n  var_dump($user-\u003egetRolesRelation($ds)-\u003egetEntities());\n  $user-\u003egetRolesRelation($ds)-\u003eremoveEntity($role);\n``` \n\n# Query support\n\nlworm supports simple database queries through the query interface. You can filter and sort the result list. Look at this simple example:\n\n```php\n  $ds-\u003ecreateQuery(User)\n     -\u003eaddFilter('active', Query::FILTER_OP_EQUAL, 1)\n     -\u003eaddSort('name', Query::SORT_DESC)\n     -\u003egetEntities()\n```\n\n# Google App Engine Datastore support\n\nIt is possible to run PHP on GAE (Google App Engine) with Caucho Quercus, which is a pure Java implementation of PHP. One of the biggest problem when you want to move PHP applications from a standard LAMP environment to GAE is database access. If you use lworm, you can use the same ORM architecture on a standard LAMP environment and on GAE. The only difference is the usage of GAEDatastoreFactory.\n\n```php\n  $ds = GAEDataStoreFactory::getDataStore();\n```    \n\nlworm using the GAE Datastore Low-level API, which makes it really powerful.\n\n# Extending lworm\n\nlworm defines 5 interfaces for datastore, query and relations. If you want to support other database architectures, you have to define an own factory, and implement these interfaces. If the architecture is SQL based, the extension is easier. In this case you have to only define a database adapter and an entity mapper which are only some lines of code.\n\n```php\n  class MySQLDataStoreFactory {\n\t\t\n    public static function getDataStore($db_host, $db_name, $db_user, $db_password) {\n      return new SQLDataStore(new MySQLDatabaseAdapter($db_host, $db_name, $db_user, $db_password), new MySQLEntityMapper);\n    }\n  \n  }\n```\n\nFor more examples, look at the code in the GIT repository. It's really simple.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthebojda%2Flworm","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthebojda%2Flworm","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthebojda%2Flworm/lists"}