{"action":"created","comment":{"url":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/comments/62636993","id":62636993,"diff_hunk":"@@ -0,0 +1,201 @@\n+Working With Multiple Records\n+=============================\n+\n+Sometimes one request can create or affect several records in one or more tables\n+in such cases its advisable to follow [ACID](https://en.wikipedia.org/wiki/ACID)\n+rules. Yii2 supports transactions, isolation levels, and allow you to\n+validate data independently, so following the ACID rules is simple.\n+\n+For example when creating a credit, you also need to store the credit references\n+and file associated to it.\n+\n+```php\n+public function actionCreate()\n+{\n+    $model = new Credit();\n+    $modelReferences = [new CreditRefence()];\n+    $modelFiles = [new CreditFile()];\n+\n+    if (Yii::$app->request->isPost) {\n+        $model->load(Yii::$app->request->post());\n+\n+        foreach (Yii::$app->requestPost($modelReferences[0]->formName())\n+            as $i => $data\n+        ) {\n+            $newModel = new CreditReference();\n+            $newModel->load($data, '');\n+            $modelReferences[$i] = $newModel;\n+        }\n+\n+        foreach (Yii::$app->requestPost($modelFiles[0]->formName())\n+            as $i => $data\n+        ) {\n+            $newModel = new CreditFile();\n+            $newModel->load($data, '');\n+            $newModel->file = UploadedFile::getInstance($newModel, \"[$i]file\");\n+            $modelFiles[$i] = $newModel;\n+        }\n+\n+        $transaction = Yii::$app->db->beginTransaction(","path":"book/working-with-multiple-records.md","position":39,"original_position":39,"commit_id":"8c1c9657a4693462accf66c91ec87b6b2cc8037c","original_commit_id":"8c1c9657a4693462accf66c91ec87b6b2cc8037c","user":{"login":"Faryshta","id":2029247,"avatar_url":"https://avatars.githubusercontent.com/u/2029247?v=3","gravatar_id":"","url":"https://api.github.com/users/Faryshta","html_url":"https://github.com/Faryshta","followers_url":"https://api.github.com/users/Faryshta/followers","following_url":"https://api.github.com/users/Faryshta/following{/other_user}","gists_url":"https://api.github.com/users/Faryshta/gists{/gist_id}","starred_url":"https://api.github.com/users/Faryshta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Faryshta/subscriptions","organizations_url":"https://api.github.com/users/Faryshta/orgs","repos_url":"https://api.github.com/users/Faryshta/repos","events_url":"https://api.github.com/users/Faryshta/events{/privacy}","received_events_url":"https://api.github.com/users/Faryshta/received_events","type":"User","site_admin":false},"body":"before loading the data to the models? It can be done for the cookbook for sure, i personally don't want the transaction to stay open for too long since it might cause other requests using the same tables to wait till this transaction is completed.","created_at":"2016-05-10T09:15:46Z","updated_at":"2016-05-10T09:15:46Z","html_url":"https://github.com/samdark/yii2-cookbook/pull/110#discussion_r62636993","pull_request_url":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/110","_links":{"self":{"href":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/comments/62636993"},"html":{"href":"https://github.com/samdark/yii2-cookbook/pull/110#discussion_r62636993"},"pull_request":{"href":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/110"}}},"pull_request":{"url":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/110","id":69462626,"html_url":"https://github.com/samdark/yii2-cookbook/pull/110","diff_url":"https://github.com/samdark/yii2-cookbook/pull/110.diff","patch_url":"https://github.com/samdark/yii2-cookbook/pull/110.patch","issue_url":"https://api.github.com/repos/samdark/yii2-cookbook/issues/110","number":110,"state":"open","locked":false,"title":"Fix #109 Workin with multiple models","user":{"login":"Faryshta","id":2029247,"avatar_url":"https://avatars.githubusercontent.com/u/2029247?v=3","gravatar_id":"","url":"https://api.github.com/users/Faryshta","html_url":"https://github.com/Faryshta","followers_url":"https://api.github.com/users/Faryshta/followers","following_url":"https://api.github.com/users/Faryshta/following{/other_user}","gists_url":"https://api.github.com/users/Faryshta/gists{/gist_id}","starred_url":"https://api.github.com/users/Faryshta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Faryshta/subscriptions","organizations_url":"https://api.github.com/users/Faryshta/orgs","repos_url":"https://api.github.com/users/Faryshta/repos","events_url":"https://api.github.com/users/Faryshta/events{/privacy}","received_events_url":"https://api.github.com/users/Faryshta/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-05-10T07:52:55Z","updated_at":"2016-05-10T09:15:46Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e0e03aedf10fc823123c7f409b5d281b2319e132","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/110/commits","review_comments_url":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/110/comments","review_comment_url":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/comments{/number}","comments_url":"https://api.github.com/repos/samdark/yii2-cookbook/issues/110/comments","statuses_url":"https://api.github.com/repos/samdark/yii2-cookbook/statuses/8c1c9657a4693462accf66c91ec87b6b2cc8037c","head":{"label":"Faryshta:patch-1","ref":"patch-1","sha":"8c1c9657a4693462accf66c91ec87b6b2cc8037c","user":{"login":"Faryshta","id":2029247,"avatar_url":"https://avatars.githubusercontent.com/u/2029247?v=3","gravatar_id":"","url":"https://api.github.com/users/Faryshta","html_url":"https://github.com/Faryshta","followers_url":"https://api.github.com/users/Faryshta/followers","following_url":"https://api.github.com/users/Faryshta/following{/other_user}","gists_url":"https://api.github.com/users/Faryshta/gists{/gist_id}","starred_url":"https://api.github.com/users/Faryshta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Faryshta/subscriptions","organizations_url":"https://api.github.com/users/Faryshta/orgs","repos_url":"https://api.github.com/users/Faryshta/repos","events_url":"https://api.github.com/users/Faryshta/events{/privacy}","received_events_url":"https://api.github.com/users/Faryshta/received_events","type":"User","site_admin":false},"repo":{"id":58419681,"name":"yii2-cookbook","full_name":"Faryshta/yii2-cookbook","owner":{"login":"Faryshta","id":2029247,"avatar_url":"https://avatars.githubusercontent.com/u/2029247?v=3","gravatar_id":"","url":"https://api.github.com/users/Faryshta","html_url":"https://github.com/Faryshta","followers_url":"https://api.github.com/users/Faryshta/followers","following_url":"https://api.github.com/users/Faryshta/following{/other_user}","gists_url":"https://api.github.com/users/Faryshta/gists{/gist_id}","starred_url":"https://api.github.com/users/Faryshta/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Faryshta/subscriptions","organizations_url":"https://api.github.com/users/Faryshta/orgs","repos_url":"https://api.github.com/users/Faryshta/repos","events_url":"https://api.github.com/users/Faryshta/events{/privacy}","received_events_url":"https://api.github.com/users/Faryshta/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/Faryshta/yii2-cookbook","description":"Yii 2.0 Cookbook","fork":true,"url":"https://api.github.com/repos/Faryshta/yii2-cookbook","forks_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/forks","keys_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/keys{/key_id}","collaborators_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/teams","hooks_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/hooks","issue_events_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/issues/events{/number}","events_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/events","assignees_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/assignees{/user}","branches_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/branches{/branch}","tags_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/tags","blobs_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/git/refs{/sha}","trees_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/git/trees{/sha}","statuses_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/statuses/{sha}","languages_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/languages","stargazers_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/stargazers","contributors_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/contributors","subscribers_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/subscribers","subscription_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/subscription","commits_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/commits{/sha}","git_commits_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/git/commits{/sha}","comments_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/comments{/number}","issue_comment_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/issues/comments{/number}","contents_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/contents/{+path}","compare_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/compare/{base}...{head}","merges_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/merges","archive_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/downloads","issues_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/issues{/number}","pulls_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/pulls{/number}","milestones_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/milestones{/number}","notifications_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/labels{/name}","releases_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/releases{/id}","deployments_url":"https://api.github.com/repos/Faryshta/yii2-cookbook/deployments","created_at":"2016-05-10T01:23:52Z","updated_at":"2016-05-09T16:14:19Z","pushed_at":"2016-05-10T07:51:53Z","git_url":"git://github.com/Faryshta/yii2-cookbook.git","ssh_url":"git@github.com:Faryshta/yii2-cookbook.git","clone_url":"https://github.com/Faryshta/yii2-cookbook.git","svn_url":"https://github.com/Faryshta/yii2-cookbook","homepage":null,"size":224,"stargazers_count":0,"watchers_count":0,"language":null,"has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"samdark:master","ref":"master","sha":"0b05cf54a8ccb5f023d62223c8e0b81a680e1ca5","user":{"login":"samdark","id":47294,"avatar_url":"https://avatars.githubusercontent.com/u/47294?v=3","gravatar_id":"","url":"https://api.github.com/users/samdark","html_url":"https://github.com/samdark","followers_url":"https://api.github.com/users/samdark/followers","following_url":"https://api.github.com/users/samdark/following{/other_user}","gists_url":"https://api.github.com/users/samdark/gists{/gist_id}","starred_url":"https://api.github.com/users/samdark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samdark/subscriptions","organizations_url":"https://api.github.com/users/samdark/orgs","repos_url":"https://api.github.com/users/samdark/repos","events_url":"https://api.github.com/users/samdark/events{/privacy}","received_events_url":"https://api.github.com/users/samdark/received_events","type":"User","site_admin":false},"repo":{"id":18465445,"name":"yii2-cookbook","full_name":"samdark/yii2-cookbook","owner":{"login":"samdark","id":47294,"avatar_url":"https://avatars.githubusercontent.com/u/47294?v=3","gravatar_id":"","url":"https://api.github.com/users/samdark","html_url":"https://github.com/samdark","followers_url":"https://api.github.com/users/samdark/followers","following_url":"https://api.github.com/users/samdark/following{/other_user}","gists_url":"https://api.github.com/users/samdark/gists{/gist_id}","starred_url":"https://api.github.com/users/samdark/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/samdark/subscriptions","organizations_url":"https://api.github.com/users/samdark/orgs","repos_url":"https://api.github.com/users/samdark/repos","events_url":"https://api.github.com/users/samdark/events{/privacy}","received_events_url":"https://api.github.com/users/samdark/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/samdark/yii2-cookbook","description":"Yii 2.0 Cookbook","fork":false,"url":"https://api.github.com/repos/samdark/yii2-cookbook","forks_url":"https://api.github.com/repos/samdark/yii2-cookbook/forks","keys_url":"https://api.github.com/repos/samdark/yii2-cookbook/keys{/key_id}","collaborators_url":"https://api.github.com/repos/samdark/yii2-cookbook/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/samdark/yii2-cookbook/teams","hooks_url":"https://api.github.com/repos/samdark/yii2-cookbook/hooks","issue_events_url":"https://api.github.com/repos/samdark/yii2-cookbook/issues/events{/number}","events_url":"https://api.github.com/repos/samdark/yii2-cookbook/events","assignees_url":"https://api.github.com/repos/samdark/yii2-cookbook/assignees{/user}","branches_url":"https://api.github.com/repos/samdark/yii2-cookbook/branches{/branch}","tags_url":"https://api.github.com/repos/samdark/yii2-cookbook/tags","blobs_url":"https://api.github.com/repos/samdark/yii2-cookbook/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/samdark/yii2-cookbook/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/samdark/yii2-cookbook/git/refs{/sha}","trees_url":"https://api.github.com/repos/samdark/yii2-cookbook/git/trees{/sha}","statuses_url":"https://api.github.com/repos/samdark/yii2-cookbook/statuses/{sha}","languages_url":"https://api.github.com/repos/samdark/yii2-cookbook/languages","stargazers_url":"https://api.github.com/repos/samdark/yii2-cookbook/stargazers","contributors_url":"https://api.github.com/repos/samdark/yii2-cookbook/contributors","subscribers_url":"https://api.github.com/repos/samdark/yii2-cookbook/subscribers","subscription_url":"https://api.github.com/repos/samdark/yii2-cookbook/subscription","commits_url":"https://api.github.com/repos/samdark/yii2-cookbook/commits{/sha}","git_commits_url":"https://api.github.com/repos/samdark/yii2-cookbook/git/commits{/sha}","comments_url":"https://api.github.com/repos/samdark/yii2-cookbook/comments{/number}","issue_comment_url":"https://api.github.com/repos/samdark/yii2-cookbook/issues/comments{/number}","contents_url":"https://api.github.com/repos/samdark/yii2-cookbook/contents/{+path}","compare_url":"https://api.github.com/repos/samdark/yii2-cookbook/compare/{base}...{head}","merges_url":"https://api.github.com/repos/samdark/yii2-cookbook/merges","archive_url":"https://api.github.com/repos/samdark/yii2-cookbook/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/samdark/yii2-cookbook/downloads","issues_url":"https://api.github.com/repos/samdark/yii2-cookbook/issues{/number}","pulls_url":"https://api.github.com/repos/samdark/yii2-cookbook/pulls{/number}","milestones_url":"https://api.github.com/repos/samdark/yii2-cookbook/milestones{/number}","notifications_url":"https://api.github.com/repos/samdark/yii2-cookbook/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/samdark/yii2-cookbook/labels{/name}","releases_url":"https://api.github.com/repos/samdark/yii2-cookbook/releases{/id}","deployments_url":"https://api.github.com/repos/samdark/yii2-cookbook/deployments","created_at":"2014-04-05T11:25:19Z","updated_at":"2016-05-09T16:14:19Z","pushed_at":"2016-05-10T07:52:55Z","git_url":"git://github.com/samdark/yii2-cookbook.git","ssh_url":"git@github.com:samdark/yii2-cookbook.git","clone_url":"https://github.com/samdark/yii2-cookbook.git","svn_url":"https://github.com/samdark/yii2-cookbook","homepage":null,"size":282,"stargazers_count":646,"watchers_count":646,"language":null,"has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":true,"forks_count":189,"mirror_url":null,"open_issues_count":36,"forks":189,"open_issues":36,"watchers":646,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/110"},"html":{"href":"https://github.com/samdark/yii2-cookbook/pull/110"},"issue":{"href":"https://api.github.com/repos/samdark/yii2-cookbook/issues/110"},"comments":{"href":"https://api.github.com/repos/samdark/yii2-cookbook/issues/110/comments"},"review_comments":{"href":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/110/comments"},"review_comment":{"href":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/samdark/yii2-cookbook/pulls/110/commits"},"statuses":{"href":"https://api.github.com/repos/samdark/yii2-cookbook/statuses/8c1c9657a4693462accf66c91ec87b6b2cc8037c"}}}}