{"id":19039608,"url":"https://github.com/windwalker-io/record","last_synced_at":"2026-04-22T16:34:44.672Z","repository":{"id":21234721,"uuid":"24550026","full_name":"windwalker-io/record","owner":"windwalker-io","description":"[READ ONLY] Simple ActiveRecord to operate database data. ","archived":false,"fork":false,"pushed_at":"2021-02-18T07:04:35.000Z","size":139,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2026-04-21T12:45:37.623Z","etag":null,"topics":["active-record","activerecord","database","orm"],"latest_commit_sha":null,"homepage":"https://github.com/ventoviro/windwalker","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/windwalker-io.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":"2014-09-28T04:18:51.000Z","updated_at":"2024-12-16T13:47:43.000Z","dependencies_parsed_at":"2022-09-09T04:50:08.829Z","dependency_job_id":null,"html_url":"https://github.com/windwalker-io/record","commit_stats":null,"previous_names":["ventoviro/windwalker-record"],"tags_count":79,"template":false,"template_full_name":null,"purl":"pkg:github/windwalker-io/record","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windwalker-io%2Frecord","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windwalker-io%2Frecord/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windwalker-io%2Frecord/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windwalker-io%2Frecord/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/windwalker-io","download_url":"https://codeload.github.com/windwalker-io/record/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/windwalker-io%2Frecord/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32145866,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-22T15:33:03.595Z","status":"ssl_error","status_checked_at":"2026-04-22T15:30:42.712Z","response_time":58,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"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":["active-record","activerecord","database","orm"],"created_at":"2024-11-08T22:17:53.956Z","updated_at":"2026-04-22T16:34:44.634Z","avatar_url":"https://github.com/windwalker-io.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Windwalker Record\n\nWindwalker Record is a simple ActiveRecord to operate database row.\n\n## Installation via Composer\n\nAdd this to the require block in your `composer.json`.\n\n``` json\n{\n    \"require\": {\n        \"windwalker/record\": \"~3.0\"\n    }\n}\n```\n\n## Use Record\n\nNew a instance.\n\n``` php\nuse Windwalker\\Record\\Record;\n\n// Record object for users table\n$user = new Record('users');\n```\n\nOr create a class:\n\n``` php\nuse Windwalker\\Record\\Record;\n\nclass UserRecord extends Record\n{\n    protected $table = 'users';\n\n    protected $keys = 'id';\n}\n\n$user = new UserRecord;\n```\n\n### Load A Record\n\n``` php\n$user-\u003eload(25); // Load by primary key\n\n$user-\u003eload(array('alias' =\u003e $alias)); // Load by field name.\n```\n\nCheck row exists\n\n``` php\ntry\n{\n\t$record-\u003eload(25);\n}\ncatch (NoResultException $e)\n{\n\t// Handle error\n}\n```\n\n### Bind Data\n\n``` php\n$data = array(\n    'name'     =\u003e 'Sakura',\n    'username' =\u003e 'sakura',\n    'alias'    =\u003e 'sakura',\n    'password' =\u003e '1234',\n    'desc'     =\u003e 'foo bar.'\n);\n\n$user-\u003ebind($data);\n\n$user-\u003ename; // Sakura\n```\n\nIf we have a table with only 3 columns:\n\n| Name |\n| ---- |\n| name |\n| username |\n| password |\n\nThe fields which not in this table will be remove after binding data.\n\n``` php\n$user-\u003ealias; // null\n```\n\nThat makes our fields in Record will always same as DB table.\n\n### Store\n\n#### Create A New Row\n\nIf primary not exists, Record will create a new row in table.\n\n``` php\n$data = array(\n    'name'     =\u003e 'Sakura',\n    'username' =\u003e 'sakura',\n    'password' =\u003e '1234'\n);\n\n$user-\u003ebind($data);\n\n$user-\u003estore();\n\necho $user-\u003eid; // Auto generated id\n```\n\n#### Update A Existing Row\n\nIf primary key exists, Record will update it.\n\n``` php\n$data = array(\n    'id'       =\u003e 30,\n    'name'     =\u003e 'Sakura',\n    'username' =\u003e 'sakura',\n    'password' =\u003e '1234'\n);\n\n$user-\u003ebind($data);\n\n$user-\u003estore();\n```\n\n### Validate\n\nCheck method help you validate data.\n\n``` php\nclass UserRecord extends Record\n{\n    // ...\n\n    public function validate()\n    {\n        if (!$this['name'])\n        {\n            throw new InvalidArgumentException('Name empty.');\n        }\n\n        return true;\n    }\n}\n```\n\nThen we call `validate()` before `store()`.\n\n``` php\n$user-\u003ebind($data)\n    -\u003evalidate()\n    -\u003estore();\n```\n\n### Delete\n\n``` php\n$user-\u003eload(30);\n$result = $user-\u003edelete(); // boolean\n\n// OR delete by conditions\n\n$result = $user-\u003edelete(30); // boolean\n$result = $user-\u003edelete(array('username' =\u003e $username)); // boolean\n```\n\n### Mutator and Accessor\n\nMutator and accessor is a setter and getter to do some extra modification when you access value via magic methods.\n\nThis is an example of mutator:\n\n``` php\nclass ArticleRecord extends Record\n{\n    protected function setCreatedDateValue($value)\n    {\n        if ($value instanceof \\DateTime)\n        {\n            $value = $value-\u003eformat('Y-m-d H:i:s');\n        }\n\n        $this-\u003edata['created_date'] = $value;\n    }\n}\n```\n\nUse camel-case style to define a method, then when you access the `created_date` field, this method will\n be auto executed.\n\n``` php\n$articleRecord-\u003ecreated_date = new \\DateTime('now');\n\necho $articleRecord-\u003ecreated_date; // 2016-03-02 12:30:29\n```\n\nAnd an example of accessor:\n\n``` php\nclass ArticleRecord extends Record\n{\n    protected function getCreatedDateValue($value)\n    {\n        return new \\DateTime($value);\n    }\n}\n```\n\nAnd now you can get `DateTime` object back:\n\n``` php\necho $articleRecord-\u003ecreated_date-\u003eformat('Y-m-d H:i:s'); // 2016-03-02 12:30:29\n```\n\n### Casts\n\nAdd casts to auto convert value type after read from DB:\n\n```php\n\u003c?\nclass SakuraRecord extends Record\n{\n    protected $casts = [\n        'id' =\u003e 'int',\n        'price' =\u003e 'string',\n        'created' =\u003e 'datetime',\n        'modified' =\u003e 'timestamp',\n        'images' =\u003e 'object', // or array will be json decoded\n        'params' =\u003e \\Windwalker\\Structure\\Structure::class,\n        'other' =\u003e ['SomeClass', 'handle'] // Use callback\n    ];\n}\n\n$sakuraRecord-\u003eload(3);\n\n$sakuraRecord-\u003eid; // 3\n$sakuraRecord-\u003eprice; // '1200.00'\n$sakuraRecord-\u003ecreated-\u003eformat('Y/d/m'); // Auto convert to DateTime object\n$sakuraRecord-\u003emodified; // 1497067876\n$sakuraRecord-\u003eimages[0]-\u003eurl; // Store json in DB, can will auto decode to object.\n$sakuraRecord-\u003eparams-\u003eget('foo.bar'); // Use class name to store value to object\n```\n\nSupports casts:\n\n- int | integer\n- real | float | double\n- string\n- bool | boolean\n- object\n- array | json\n- date | datetime\n- timestamp\n- (Class name)\n- (Callback array)\n\n## NestedRecord\n\nNestedRecord is a tool help us handle [Nested Set Model](http://en.wikipedia.org/wiki/Nested_set_model).\n\n### Create Table\n\nName: `categories`\n\n| Name | Type | Description | Need For NestedRecord |\n| ---- | ---- | ----------- | --------------------- |\n| id | int | Primary Key |  |\n| parent_id | int | ID of Parent Node | V |\n| title | varchar | Category title |  |\n| path | varchar | Node path | V |\n| lft | int | Left key | V |\n| rgt | int | Right key | V |\n| level | int | Node level | V |\n\n### Initialise\n\nEvery nested set should have a root node.\n\n``` php\n$cat = new NestedRecord('categories');\n\n$cat-\u003ecreateRoot();\n```\n\nNOTE: The root node id is `1`.\n\n### Create Node\n\nSet as first child of ROOT\n\n``` php\n$cat-\u003ebind($data)\n    -\u003esetLocation(1, NestedRecord::LOCATION_FIRST_CHILD)\n    -\u003estore();\n```\n\nNow we will have a new node and it id is `2`. Create a new node as last child of `2`.\n\n``` php\n$cat-\u003ebind($data)\n    -\u003esetLocation(2, NestedRecord::LOCATION_LAST_CHILD)\n    -\u003estore();\n```\n\nAvailable positions:\n\n- LOCATION_FIRST_CHILD\n- LOCATION_LAST_CHILD\n- LOCATION_BEFORE\n- LOCATION_AFTER\n\n### Move Node\n\n#### Re Ordering\n\n``` php\n$cat-\u003emove(1); // move up\n$cat-\u003emove(-1); // Move down\n```\n\n#### Move To Other Node\n\nMove to node `3` as last child.\n\n``` php\n$cat-\u003emoveByReference(3, NestedRecord::LOCATION_LAST_CHILD);\n```\n\n### Rebuild\n\nIf a tree was not correct, using rebuild to reset all `lft`, `rgt` of this branch.\n\n``` php\n$cat-\u003eload(5);\n$cat-\u003erebuild(); // Rebuild node: 5 and it's children.\n```\n\n### getPath\n\nMethod to get an array of nodes from a given node to its root.\n\n``` php\n$path = $cat-\u003egetPath();\n```\n\n### getTree\n\nMethod to get a node and all its child nodes.\n\n``` php\n$records = $cat-\u003egetTree();\n```\n\n## Event\n\nRecord has an event system that we can process logic before \u0026 after every DB operation.\n\nAdd event methods in your Record class.\n\n``` php\nclass UserRecord extends Record\n{\n\tpublic function onAfterLoad(Event $event)\n\t{\n\t\t$this-\u003efoo = array('a', 'b', 'c');\n\t}\n}\n```\n\nOr add listeners to Dispatcher (You must install `windwalker/event` first).\n\n``` php\n// Use listener object\n$record-\u003egetDispatcher()-\u003eaddListener(new MyRecordListener);\n\n// Use callback\n$record-\u003egetDispatcher()-\u003elisten('onAfterStore', function (Event $event)\n{\n    // Process your logic\n});\n```\n\nAvailable events:\n\n- onBeforeLoad\n- onAfterLoad\n- onBeforeStore\n- onAfterStore\n- onBeforeDelete\n- onAfterDelete\n- onBeforeBind\n- onAfterBind\n- onBeforeCreate\n- onAfterCreate\n- onBeforeUpdate\n- onAfterUpdate\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwindwalker-io%2Frecord","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwindwalker-io%2Frecord","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwindwalker-io%2Frecord/lists"}