{"action":"created","comment":{"url":"https://api.github.com/repos/nette/web-content/pulls/comments/31569655","id":31569655,"diff_hunk":"@@ -0,0 +1,121 @@\n+Model\n+**************\n+\n+S tím, jak aplikace roste, brzy zjistíme, že na různých místech, v různých presenterech, potřebujeme provádět podobné operace s databází, například získávat nejnovější publikované články. Když aplikaci vylepšíme třeba tím, že u článků přidáme příznak, zda je rozepsaný, musíme potom projít i všechna místa v aplikaci, kde se články z databáze získávají, a doplnit podmínku where, aby se vybíraly jen články nerozepsané.\n+\n+V ten moment se přímá práce s databází stává nedostatečnou a bude šikovnější si vypomoci novou funkcí, která nám publikované články bude vracet. A když později přidáme další podmínku, například že se nemají zobrazovat články s budoucím datem, upravíme kód jen na jednom místě.\n+\n+Funkci umístíme třeba do třídy `ArticleManager` a nazveme ji `getPublicArticles()`.\n+\n+\n+V adresáři `app/model/` vytvoříme naši modelovou třídu, která se nám bude starat o články, a pojmenujeme ji `ArticlesManager.php`.\n+\n+\n+/---php\n+<?php\n+\n+namespace App\\Model;\n+\n+use Nette;\n+\n+/**\n+ * Article management.\n+ */\n+class ArticleManager extends Nette\\Object\n+{\n+\t/**\n+\t * @var Nette\\Database\\Context\n+\t */\n+\tprivate $database;\n+\n+\tpublic function __construct(Nette\\Database\\Context $database)\n+\t{\n+\t\t$this->database = $database;\n+\t}\n+\n+\tpublic function getPublicArticles()\n+\t{\n+\t\treturn $this->database->table('posts')\n+\t\t\t->where('created_at < ', new \\DateTime())\n+\t\t\t->order('created_at DESC');\n+\t}\n+}\n+\n+\\--\n+\n+Třídu jsme vytvořili s konstruktorem, ve kterém si předáme databázový Context:[api:Nette\\Database\\Context] do managera.\n+\n+Pokud bychom nevyužívali frameworku a síly DI containeru, museli bychom v našem případě nejprve vytvořit instaci `Nette\\Database\\Connection`, `Nette\\Database\\Structure`, `Nette\\Database\\Conventions` a `Nette\\Caching\\Storages\\FileStorage` (které pro své instancování také potřebují nějaké závislosti, třeba jméno a heslo k databázi) pro to, abychom mohli vytvořit instanci `Nette\\Database\\Context`, kterou bychom následně použili pro vytvoření instance `ArticleManager`.\n+\n+/--php\n+\t...\n+\t// vyrobime vse co je treba pro Context\n+\t$dbConnection = new Nette\\Database\\Connection(...);\n+\t$dbStructure = new Nette\\Database\\Structure(...);\n+\t$dbConventions = new Nette\\Database\\Conventions\\DiscoveredConventions(...);\n+\t$dbCache = new Nette\\Caching\\Storages\\FileStorage(...);\n+\n+\t// vyrobime Context\n+\t$dbContext = new Nette\\Database\\Context($dbConnection, $dbStructure, $dbConventions, $dbCache);\n+\n+\t// vyrobime ArticleManager\n+\t$articleManager = new App\\Model\\ArticleManager($dbContext)\n+\t...\n+\\--\n+\n+Tolik psaní pro to, abychom dostali fungujicí ArticleManager? To asi nechceme, proto použijeme DI container, který berme jako skřínku co generuje tyto výrobní kódy a nám vrátí jen požadovaný objekt.\n+\n+Jediné co potřebujeme, aby nám DI container vrátil námi požadovaný objekt, je mu řící, že ho budeme chtít. To se dělá v konfiguraci aplikace. V souboru `app/config/config.neon` v sekci services přidáme řádek s celým názvem třídy.\n+\n+/--neon\n+services:\n+\t- App\\Forms\\SignFormFactory\n+\trouter: App\\RouterFactory::createRouter\n+\t- App\\Model\\ArticleManager\n+\\--\n+\n+\n+Tím Nette a jeho \"DI containeru\":[dependency-injection] říkáme, kdyby někdo chtěl instanci této třídy, tak víš kde ji hledat, dej ji vše co potřebuje k životu a připrav nám ji pro použití v dalších třídách. Takto registrovaná třída se nazývá **služba**.\n+\n+\n+Přepneme se do `HomepagePresenter.php`, který upravíme tak, že se zbavíme závislosti na `Nette\\Database\\Context` a nahradíme za novou závislost na naší nové třídě.\n+\n+\n+/--php\n+<?php\n+\n+namespace App\\Presenters;\n+\n+use Nette,\n+\tApp\\Model\\ArticleManager;\n+\n+/**\n+ * Homepage presenter.\n+ */\n+class HomepagePresenter extends BasePresenter\n+{\n+\t/** @var ArticleManager */\n+\tprivate $articleManager;\n+\n+\tpublic function __construct(ArticleManager $articleManager)\n+\t{\n+\t\t$this->articleManager = $articleManager;\n+\t}\n+\n+\tpublic function renderDefault()\n+\t{\n+\t\t$this->template->posts = $this->articleManager->getPublicArticles()->limit(5);\n+\t}\n+\n+}\n+\n+\n+\\--\n+.[tip]\n+V sekci use máme `App\\Model\\ArticleManager`, tak si můžeme zápis v php kódu zkrátit na ArticleManager (nebojte, i v komentářích to funguje a vaše chytré IDE by si s tím mělo poradit).\n+\n+\n+\n+V konstrukotu si požádáme o `ArticleManager`, který si přiřadíme do private property a v metodě renderDefault zavoláme metodu `getPublicArticles()` a nad výsledkem, co se nám vrátí, zavoláme ještě metodu `limit(5)`;","path":"cs/quickstart/refactoring-model.texy","position":119,"original_position":119,"commit_id":"a1b17219c396fefaae2cb05eb2fbdd0b3feacb50","original_commit_id":"a1b17219c396fefaae2cb05eb2fbdd0b3feacb50","user":{"login":"Majkl578","id":144181,"avatar_url":"https://avatars.githubusercontent.com/u/144181?v=3","gravatar_id":"","url":"https://api.github.com/users/Majkl578","html_url":"https://github.com/Majkl578","followers_url":"https://api.github.com/users/Majkl578/followers","following_url":"https://api.github.com/users/Majkl578/following{/other_user}","gists_url":"https://api.github.com/users/Majkl578/gists{/gist_id}","starred_url":"https://api.github.com/users/Majkl578/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Majkl578/subscriptions","organizations_url":"https://api.github.com/users/Majkl578/orgs","repos_url":"https://api.github.com/users/Majkl578/repos","events_url":"https://api.github.com/users/Majkl578/events{/privacy}","received_events_url":"https://api.github.com/users/Majkl578/received_events","type":"User","site_admin":false},"body":"A ještě bych tu větu rozdělil na dvě, u *nad výsledkem*.","created_at":"2015-06-02T20:51:34Z","updated_at":"2015-06-02T20:51:34Z","html_url":"https://github.com/nette/web-content/pull/271#discussion_r31569655","pull_request_url":"https://api.github.com/repos/nette/web-content/pulls/271","_links":{"self":{"href":"https://api.github.com/repos/nette/web-content/pulls/comments/31569655"},"html":{"href":"https://github.com/nette/web-content/pull/271#discussion_r31569655"},"pull_request":{"href":"https://api.github.com/repos/nette/web-content/pulls/271"}}},"pull_request":{"url":"https://api.github.com/repos/nette/web-content/pulls/271","id":31473739,"html_url":"https://github.com/nette/web-content/pull/271","diff_url":"https://github.com/nette/web-content/pull/271.diff","patch_url":"https://github.com/nette/web-content/pull/271.patch","issue_url":"https://api.github.com/repos/nette/web-content/issues/271","number":271,"state":"open","locked":false,"title":"Init model quick start","user":{"login":"chemix","id":42802,"avatar_url":"https://avatars.githubusercontent.com/u/42802?v=3","gravatar_id":"","url":"https://api.github.com/users/chemix","html_url":"https://github.com/chemix","followers_url":"https://api.github.com/users/chemix/followers","following_url":"https://api.github.com/users/chemix/following{/other_user}","gists_url":"https://api.github.com/users/chemix/gists{/gist_id}","starred_url":"https://api.github.com/users/chemix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chemix/subscriptions","organizations_url":"https://api.github.com/users/chemix/orgs","repos_url":"https://api.github.com/users/chemix/repos","events_url":"https://api.github.com/users/chemix/events{/privacy}","received_events_url":"https://api.github.com/users/chemix/received_events","type":"User","site_admin":false},"body":"prvni vykop.\r\n\r\nNeni to az moc polopaticke? spise reste obsahove bloky nez formu, tu doladim ve fialni fazi s clovekem co umi česky","created_at":"2015-03-18T21:23:49Z","updated_at":"2015-06-02T20:51:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"6f6b109e6451ebf6c9ed2bae4b594730694534b5","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/nette/web-content/pulls/271/commits","review_comments_url":"https://api.github.com/repos/nette/web-content/pulls/271/comments","review_comment_url":"https://api.github.com/repos/nette/web-content/pulls/comments{/number}","comments_url":"https://api.github.com/repos/nette/web-content/issues/271/comments","statuses_url":"https://api.github.com/repos/nette/web-content/statuses/a1b17219c396fefaae2cb05eb2fbdd0b3feacb50","head":{"label":"chemix:doc-2.3","ref":"doc-2.3","sha":"a1b17219c396fefaae2cb05eb2fbdd0b3feacb50","user":{"login":"chemix","id":42802,"avatar_url":"https://avatars.githubusercontent.com/u/42802?v=3","gravatar_id":"","url":"https://api.github.com/users/chemix","html_url":"https://github.com/chemix","followers_url":"https://api.github.com/users/chemix/followers","following_url":"https://api.github.com/users/chemix/following{/other_user}","gists_url":"https://api.github.com/users/chemix/gists{/gist_id}","starred_url":"https://api.github.com/users/chemix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chemix/subscriptions","organizations_url":"https://api.github.com/users/chemix/orgs","repos_url":"https://api.github.com/users/chemix/repos","events_url":"https://api.github.com/users/chemix/events{/privacy}","received_events_url":"https://api.github.com/users/chemix/received_events","type":"User","site_admin":false},"repo":{"id":31762251,"name":"web-content","full_name":"chemix/web-content","owner":{"login":"chemix","id":42802,"avatar_url":"https://avatars.githubusercontent.com/u/42802?v=3","gravatar_id":"","url":"https://api.github.com/users/chemix","html_url":"https://github.com/chemix","followers_url":"https://api.github.com/users/chemix/followers","following_url":"https://api.github.com/users/chemix/following{/other_user}","gists_url":"https://api.github.com/users/chemix/gists{/gist_id}","starred_url":"https://api.github.com/users/chemix/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/chemix/subscriptions","organizations_url":"https://api.github.com/users/chemix/orgs","repos_url":"https://api.github.com/users/chemix/repos","events_url":"https://api.github.com/users/chemix/events{/privacy}","received_events_url":"https://api.github.com/users/chemix/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/chemix/web-content","description":"Documentation and website content.","fork":true,"url":"https://api.github.com/repos/chemix/web-content","forks_url":"https://api.github.com/repos/chemix/web-content/forks","keys_url":"https://api.github.com/repos/chemix/web-content/keys{/key_id}","collaborators_url":"https://api.github.com/repos/chemix/web-content/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/chemix/web-content/teams","hooks_url":"https://api.github.com/repos/chemix/web-content/hooks","issue_events_url":"https://api.github.com/repos/chemix/web-content/issues/events{/number}","events_url":"https://api.github.com/repos/chemix/web-content/events","assignees_url":"https://api.github.com/repos/chemix/web-content/assignees{/user}","branches_url":"https://api.github.com/repos/chemix/web-content/branches{/branch}","tags_url":"https://api.github.com/repos/chemix/web-content/tags","blobs_url":"https://api.github.com/repos/chemix/web-content/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/chemix/web-content/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/chemix/web-content/git/refs{/sha}","trees_url":"https://api.github.com/repos/chemix/web-content/git/trees{/sha}","statuses_url":"https://api.github.com/repos/chemix/web-content/statuses/{sha}","languages_url":"https://api.github.com/repos/chemix/web-content/languages","stargazers_url":"https://api.github.com/repos/chemix/web-content/stargazers","contributors_url":"https://api.github.com/repos/chemix/web-content/contributors","subscribers_url":"https://api.github.com/repos/chemix/web-content/subscribers","subscription_url":"https://api.github.com/repos/chemix/web-content/subscription","commits_url":"https://api.github.com/repos/chemix/web-content/commits{/sha}","git_commits_url":"https://api.github.com/repos/chemix/web-content/git/commits{/sha}","comments_url":"https://api.github.com/repos/chemix/web-content/comments{/number}","issue_comment_url":"https://api.github.com/repos/chemix/web-content/issues/comments{/number}","contents_url":"https://api.github.com/repos/chemix/web-content/contents/{+path}","compare_url":"https://api.github.com/repos/chemix/web-content/compare/{base}...{head}","merges_url":"https://api.github.com/repos/chemix/web-content/merges","archive_url":"https://api.github.com/repos/chemix/web-content/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/chemix/web-content/downloads","issues_url":"https://api.github.com/repos/chemix/web-content/issues{/number}","pulls_url":"https://api.github.com/repos/chemix/web-content/pulls{/number}","milestones_url":"https://api.github.com/repos/chemix/web-content/milestones{/number}","notifications_url":"https://api.github.com/repos/chemix/web-content/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/chemix/web-content/labels{/name}","releases_url":"https://api.github.com/repos/chemix/web-content/releases{/id}","created_at":"2015-03-06T09:31:27Z","updated_at":"2015-04-24T10:58:09Z","pushed_at":"2015-06-02T19:34:13Z","git_url":"git://github.com/chemix/web-content.git","ssh_url":"git@github.com:chemix/web-content.git","clone_url":"https://github.com/chemix/web-content.git","svn_url":"https://github.com/chemix/web-content","homepage":"http://nette.org/en/writing","size":14475,"stargazers_count":0,"watchers_count":0,"language":"PHP","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":"doc-2.3"}},"base":{"label":"nette:doc-2.3","ref":"doc-2.3","sha":"8246c4ca88ad29ec3babcae789479bc5fb2768d4","user":{"login":"nette","id":99965,"avatar_url":"https://avatars.githubusercontent.com/u/99965?v=3","gravatar_id":"","url":"https://api.github.com/users/nette","html_url":"https://github.com/nette","followers_url":"https://api.github.com/users/nette/followers","following_url":"https://api.github.com/users/nette/following{/other_user}","gists_url":"https://api.github.com/users/nette/gists{/gist_id}","starred_url":"https://api.github.com/users/nette/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nette/subscriptions","organizations_url":"https://api.github.com/users/nette/orgs","repos_url":"https://api.github.com/users/nette/repos","events_url":"https://api.github.com/users/nette/events{/privacy}","received_events_url":"https://api.github.com/users/nette/received_events","type":"Organization","site_admin":false},"repo":{"id":10505034,"name":"web-content","full_name":"nette/web-content","owner":{"login":"nette","id":99965,"avatar_url":"https://avatars.githubusercontent.com/u/99965?v=3","gravatar_id":"","url":"https://api.github.com/users/nette","html_url":"https://github.com/nette","followers_url":"https://api.github.com/users/nette/followers","following_url":"https://api.github.com/users/nette/following{/other_user}","gists_url":"https://api.github.com/users/nette/gists{/gist_id}","starred_url":"https://api.github.com/users/nette/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/nette/subscriptions","organizations_url":"https://api.github.com/users/nette/orgs","repos_url":"https://api.github.com/users/nette/repos","events_url":"https://api.github.com/users/nette/events{/privacy}","received_events_url":"https://api.github.com/users/nette/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/nette/web-content","description":"Documentation and website content.","fork":false,"url":"https://api.github.com/repos/nette/web-content","forks_url":"https://api.github.com/repos/nette/web-content/forks","keys_url":"https://api.github.com/repos/nette/web-content/keys{/key_id}","collaborators_url":"https://api.github.com/repos/nette/web-content/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/nette/web-content/teams","hooks_url":"https://api.github.com/repos/nette/web-content/hooks","issue_events_url":"https://api.github.com/repos/nette/web-content/issues/events{/number}","events_url":"https://api.github.com/repos/nette/web-content/events","assignees_url":"https://api.github.com/repos/nette/web-content/assignees{/user}","branches_url":"https://api.github.com/repos/nette/web-content/branches{/branch}","tags_url":"https://api.github.com/repos/nette/web-content/tags","blobs_url":"https://api.github.com/repos/nette/web-content/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/nette/web-content/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/nette/web-content/git/refs{/sha}","trees_url":"https://api.github.com/repos/nette/web-content/git/trees{/sha}","statuses_url":"https://api.github.com/repos/nette/web-content/statuses/{sha}","languages_url":"https://api.github.com/repos/nette/web-content/languages","stargazers_url":"https://api.github.com/repos/nette/web-content/stargazers","contributors_url":"https://api.github.com/repos/nette/web-content/contributors","subscribers_url":"https://api.github.com/repos/nette/web-content/subscribers","subscription_url":"https://api.github.com/repos/nette/web-content/subscription","commits_url":"https://api.github.com/repos/nette/web-content/commits{/sha}","git_commits_url":"https://api.github.com/repos/nette/web-content/git/commits{/sha}","comments_url":"https://api.github.com/repos/nette/web-content/comments{/number}","issue_comment_url":"https://api.github.com/repos/nette/web-content/issues/comments{/number}","contents_url":"https://api.github.com/repos/nette/web-content/contents/{+path}","compare_url":"https://api.github.com/repos/nette/web-content/compare/{base}...{head}","merges_url":"https://api.github.com/repos/nette/web-content/merges","archive_url":"https://api.github.com/repos/nette/web-content/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/nette/web-content/downloads","issues_url":"https://api.github.com/repos/nette/web-content/issues{/number}","pulls_url":"https://api.github.com/repos/nette/web-content/pulls{/number}","milestones_url":"https://api.github.com/repos/nette/web-content/milestones{/number}","notifications_url":"https://api.github.com/repos/nette/web-content/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/nette/web-content/labels{/name}","releases_url":"https://api.github.com/repos/nette/web-content/releases{/id}","created_at":"2013-06-05T14:51:53Z","updated_at":"2015-06-02T20:12:24Z","pushed_at":"2015-06-02T20:09:49Z","git_url":"git://github.com/nette/web-content.git","ssh_url":"git@github.com:nette/web-content.git","clone_url":"https://github.com/nette/web-content.git","svn_url":"https://github.com/nette/web-content","homepage":"http://nette.org/en/writing","size":25044,"stargazers_count":53,"watchers_count":53,"language":"PHP","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":116,"mirror_url":null,"open_issues_count":83,"forks":116,"open_issues":83,"watchers":53,"default_branch":"doc-2.3"}},"_links":{"self":{"href":"https://api.github.com/repos/nette/web-content/pulls/271"},"html":{"href":"https://github.com/nette/web-content/pull/271"},"issue":{"href":"https://api.github.com/repos/nette/web-content/issues/271"},"comments":{"href":"https://api.github.com/repos/nette/web-content/issues/271/comments"},"review_comments":{"href":"https://api.github.com/repos/nette/web-content/pulls/271/comments"},"review_comment":{"href":"https://api.github.com/repos/nette/web-content/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/nette/web-content/pulls/271/commits"},"statuses":{"href":"https://api.github.com/repos/nette/web-content/statuses/a1b17219c396fefaae2cb05eb2fbdd0b3feacb50"}}}}