{"id":22007187,"url":"https://github.com/tekku-taro/php-ormapper","last_synced_at":"2026-04-16T04:31:53.253Z","repository":{"id":144672377,"uuid":"299263294","full_name":"tekku-taro/php-ormapper","owner":"tekku-taro","description":"PHPでリレーショナルデータベースのテーブル操作するための ActiveRecordタイプのORMapper ライブラリです。","archived":false,"fork":false,"pushed_at":"2020-11-13T10:46:20.000Z","size":87,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-28T13:49:41.436Z","etag":null,"topics":["library","ormapper","php"],"latest_commit_sha":null,"homepage":"","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/tekku-taro.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-09-28T09:49:40.000Z","updated_at":"2020-11-13T10:47:40.000Z","dependencies_parsed_at":null,"dependency_job_id":"1f570eb5-bf8f-422c-b88b-21c5fc6973f9","html_url":"https://github.com/tekku-taro/php-ormapper","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/tekku-taro%2Fphp-ormapper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tekku-taro%2Fphp-ormapper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tekku-taro%2Fphp-ormapper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tekku-taro%2Fphp-ormapper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tekku-taro","download_url":"https://codeload.github.com/tekku-taro/php-ormapper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245066676,"owners_count":20555427,"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":["library","ormapper","php"],"created_at":"2024-11-30T01:18:16.581Z","updated_at":"2026-04-16T04:31:48.224Z","avatar_url":"https://github.com/tekku-taro.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Php ORMapper\n\nPHPでリレーショナルデータベースのテーブル操作するための ActiveRecordタイプの**ORMapper** ライブラリです。モデルクラスを使って、レコードの保存、クエリビルダの作成と問い合わせができます。\n\n## 使い方\n\n## モデルクラス\n\n*\\ORM\\Model\\ActiveRecord\\Model* クラスを継承する\n\n```php\nclass Post extends Model\n{\n    // 操作するテーブル名\n    protected static $tableName = 'posts';\n\n    // createFromArray, editWithで挿入できるカラム名\n    protected static $insertable = ['title', 'body', 'user_id'];\n\n}\n```\n\n### リレーションの定義\n\n**$relations** プロパティに連想配列で指定する\n\n```php\nprotected static $relations = [\n    # belongsTo, hasMany,hasOne の場合\n\t# 'リレーション名'=\u003e [ リレーション先のクラス, 'belongsTo'/'hasMany'/'hasOne' , '外部キー' ]\n    'posts'=\u003e[Post::class,'hasMany' ,'user_id'],  \n    # belongsToMany の場合\n    # 'リレーション名'=\u003e [ リレーション先のクラス, 'belongsToMany' , '中間テーブル名', '自モデルの外部キー', '関連モデルの外部キー' ]\n    'favorites'=\u003e[Post::class,'belongsToMany', 'favorites', 'user_id', 'post_id']\n];\n```\n\n\n\n### レコードの新規作成\n\n1. インスタンスを作成し、プロパティに値を代入して保存\n\n   ```php\n   $post = new Post();\n   $post-\u003etitle = 'How to cook pizza';\n   $post-\u003econtent = 'pizza recipe content';\n   $post-\u003efinished = false;\n   $post-\u003esaveNew();\n   ```\n\n   \n\n2. **createFromArray**メソッドに、配列を渡して一括保存\n\n```php\n$post = new Post();\n$data = [\n    'title'=\u003e'How to cook pizza2',\n    'content'=\u003e'test create from array',\n    'finished'=\u003etrue\n];\n$post-\u003ecreateFromArray($data);\n\n```\n\n一括保存したいカラムはモデルクラスの**$insertable**プロパティに指定しておく\n\n```php\nprotected static $insertable = ['title', 'body', 'user_id'];\n```\n\n\n\n### レコードの更新\n\n1. インスタンスのプロパティに更新データを代入\n\n   ```php\n   $post = Post::where('id',1)-\u003efindFirst();\n   $post-\u003efinished = true;\n   $post-\u003esaveUpdate();\n   ```\n\n   \n\n2. インスタンスの**editWith**メソッドに、更新データを配列で渡して更新\n\n   ```php\n   $post = Post::where('id',1)-\u003efindFirst();\n   $data = [\n       'finished' =\u003e true\n   ];\n   $post-\u003eeditWith($data)-\u003esaveUpdate();\n   ```\n\n### レコードの削除\n\n```php\n$post = Post::where('id',1)-\u003efindFirst();\n$post-\u003edelete();\n```\n\n## クエリビルダとクエリの実行\n\nモデルクラスからクエリビルダ作成メソッドへ*static*にアクセスする\n\n### クエリビルダの作成\n\n必要なメソッドをメソッドチェーンで繋いでいく\n\n```php\n// where(カラム名, 値) 又は、 where(カラム名, oper, 値)\nPost::where('id', 'IN', [1,2])\nPost::where('id', '1')\n// orWhere(カラム名, 値) 又は、 orWhere(カラム名, oper, 値)\nPost::where('body', 'good')-\u003eorWhere('finished', 1)   \n\n// orderBy(カラム名, ['DESC' | 'ASC'])\nPost::orderBy('id', 'DESC')\n// limit(取得数)\nPost::limit(2)\n// groupBy(カラム名) having(カラム名_集計関数名, oper, 値)\nPost::groupBy('user_id')-\u003ehaving('views_sum', '\u003e', 4)\n```\n\n### クエリの実行\n\nクエリビルダを作成した後、あるいは単独で実行できるメソッド。\n\n```php\n// 複数のレコードを取得\n$posts = Post::where('id', '\u003e', '2')-\u003efindMany();\n// 最初のレコードを取得\n$posts = Post::where('id', '1')-\u003efindFirst();\n// モデルクラスから単独で実行できる\n$posts = Post::findMany();\n\n// 集計用のメソッド\n// count, max, min, sum\nPost::count();\nPost::count('views'); // カラムを指定\n\n// レコードの存在確認 \n$exists = Post::where('user_id', 1)-\u003eexists();\nprint($exists); // true or false\n```\n\n### ページネーション\n\nレコードを指定した数づつ取得して表示\n\n```php\n// paginate(一度に取得するレコード数)\n$posts = Post::paginate(10);\n?\u003e\n    \n\u003ctable\u003e\n    \u003cthead\u003e\n        \u003ctr\u003e\n        \u003cth\u003eID\u003c/th\u003e\n        \u003cth\u003etitle\u003c/th\u003e\n        \u003cth\u003ebody\u003c/th\u003e\n        \u003cth\u003eDate\u003c/th\u003e\n        \u003c/tr\u003e\n    \u003c/thead\u003e\n    \u003ctbody\u003e\n    \t\u003c?php foreach ($posts as $post): ?\u003e\n            \u003ctr\u003e\n            \u003ctd\u003e\u003c?= $post-\u003eid ?\u003e\u003c/td\u003e\n            \u003ctd\u003e\u003c?= $post-\u003etitle ?\u003e\u003c/td\u003e\n            \u003ctd\u003e\u003c?= $post-\u003ebody ?\u003e\u003c/td\u003e\n            \u003ctd\u003e\u003c?= $post-\u003edate ?\u003e\u003c/td\u003e\n            \u003c/tr\u003e\n        \u003c?php endforeach; ?\u003e\n    \u003c/tbody\u003e\n\u003c/table\u003e\n\u003c?php \n   // リンクを表示する\n   $posts-\u003eshowLinks(); \n?\u003e\n\n```\n\n\n\n### リレーション先のモデルの取得\n\n```php\n// 関連モデルのデータを取得\n$user = $post-\u003erelation('posts')-\u003efindFirst();\n$posts = $user-\u003erelation('posts')-\u003efindMany();\n// 関連モデルのカウント\n$user = $user-\u003ecountRelations(['posts','favorites']);\n// counts['リレーション名']　でカウントにアクセス\nprint $user-\u003ecounts['posts'];\n\n// 関連モデルがあるレコードのみ取得\n$users = User::hasRelations(['posts'])-\u003efindMany();\n\n// 動的プロパティ\n$posts = $user-\u003eposts; // $post-\u003erelation('posts')-\u003efindMany(); と同じ\n\n// eagerloading N+1問題の解決\n// with(['リレーション名'])\n$user = User::with(['posts'])-\u003efindFirst();\n$posts = $user-\u003eposts;\n```\n\n### 中間テーブル\n\n**newPivot(), updatePivot(), removePivot()** を使って、中間テーブルのレコードを操作\n\n```php\n// 関連モデルのレコードIDを引数として渡す\n$user = $user-\u003erelation('favorites')-\u003enewPivot(3);\n\n// 中間テーブルに追加データを保存したい\n$data = ['star'=\u003e4]; \n$user = $user-\u003erelation('favorites')-\u003enewPivot(3, $data);\n\n// 追加カラムを更新\n$data = ['star'=\u003e2];\n$user = $user-\u003erelation('favorites')-\u003eupdatePivot(2, $data);\n\n// レコードを削除\n$user-\u003erelation('favorites')-\u003eremovePivot(3);\n\n// 中間テーブルのカラムデータを一緒に取得\n// appendPivot(['追加取得するカラム名'])\n$posts= $user-\u003erelation('favorites')-\u003eappendPivot(['star'])-\u003efindMany();\n```\n\n\n\n## DB接続設定ファイル\n\n*src/config* フォルダ内のDB接続設定ファイル(**DbConfig.php**)内にデータベース接続情報を登録する。\n\n```php\n// $data の databasesに、接続名をキーとして、接続情報を指定する\npublic static $data = [\n    'default'=\u003e'mysql',\n    'databases'=\u003e[\n        'mysql'=\u003e[\n            'CONNECTION'=\u003e'mysql',\n            'HOST'=\u003e'localhost',\n            'DB_NAME'=\u003e'tasksdb',\n            'USERNAME'=\u003e'root',\n            'PASSWORD'=\u003e'pass',\n        ],\n    ]\n\n];\n```\n\n### データベースへ接続\n\nモデルクラスを使う前に、RDBAdapterクラスのinitメソッドでDBへ接続する\n\n```php\nuse ORM\\Model\\Adapter\\RDBAdapter;\n// 引数として利用する接続名を渡す（省略すると、default値が使われる）\nRDBAdapter::init('mysql');\n```\n\n\n\n## ライセンス (License)\n\n**Php ORMapper**は[MIT license](https://opensource.org/licenses/MIT)のもとで公開されています。\n\n**Php ORMapper** is open-source software licensed under the [MIT license](https://opensource.org/licenses/MIT).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftekku-taro%2Fphp-ormapper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftekku-taro%2Fphp-ormapper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftekku-taro%2Fphp-ormapper/lists"}