{"id":1214,"date":"2015-02-07T21:02:27","date_gmt":"2015-02-07T20:02:27","guid":{"rendered":"https:\/\/www.hutsky.cz\/blog\/?p=1214"},"modified":"2015-02-07T21:02:27","modified_gmt":"2015-02-07T20:02:27","slug":"yii-crud-generator-and-plural-forms","status":"publish","type":"post","link":"https:\/\/www.hutsky.cz\/blog\/2015\/02\/yii-crud-generator-and-plural-forms\/","title":{"rendered":"Yii CRUD generator and plural forms"},"content":{"rendered":"<p style=\"text-align: justify;\"><img loading=\"lazy\" decoding=\"async\" class=\"alignright size-full wp-image-1216\" src=\"https:\/\/www.hutsky.cz\/blog\/wp-content\/uploads\/logo.png\" alt=\"yii logo\" width=\"211\" height=\"48\" \/>Yii framework has a really nice CRUD generator that can speed up building back-end applications, which tend to be rather similar and therefore repetitive. If you prepare reasonable taxonomy of the underlying database tables, it also manages the page headers pretty well. If your table is called <em>album<\/em>, for instance, it will generate a page for editing albums with a header <em>&#8220;Edit Album #1&#8221;<\/em>, an index page called <em>&#8220;List Albums&#8221;<\/em> or the management page called <em>&#8220;Manage Albums&#8221;<\/em>. As you can see, it will even handle the plural forms. However, this works fine for English projects, but may not be desirable for other languages.<\/p>\n<p style=\"text-align: justify;\">What I usually do is find the method called <em>pluralize()<\/em> in <a class=\"sourceLink\" href=\"https:\/\/github.com\/yiisoft\/yii\/blob\/1.1.16\/framework\/gii\/CCodeModel.php\">framework\/gii\/CCodeModel.php<\/a> and basically disable the method by changing it like this:<\/p>\n<pre class=\"\">public function pluralize($name)\r\n\t{\r\n\t\treturn $name; # this disables the method no matter where it gets used\r\n\t\t$rules=array(\r\n\t\t\t'\/(m)ove$\/i' =&gt; '\\1oves',\r\n\t\t\t'\/(f)oot$\/i' =&gt; '\\1eet',\r\n\t\t\t'\/(c)hild$\/i' =&gt; '\\1hildren',\r\n\t\t\t'\/(h)uman$\/i' =&gt; '\\1umans',\r\n\t\t\t'\/(m)an$\/i' =&gt; '\\1en',\r\n\t\t\t'\/(s)taff$\/i' =&gt; '\\1taff',\r\n\t\t\t'\/(t)ooth$\/i' =&gt; '\\1eeth',\r\n\t\t\t'\/(p)erson$\/i' =&gt; '\\1eople',\r\n\t\t\t'\/([m|l])ouse$\/i' =&gt; '\\1ice',\r\n\t\t\t'\/(x|ch|ss|sh|us|as|is|os)$\/i' =&gt; '\\1es',\r\n\t\t\t'\/([^aeiouy]|qu)y$\/i' =&gt; '\\1ies',\r\n\t\t\t'\/(?:([^f])fe|([lr])f)$\/i' =&gt; '\\1\\2ves',\r\n\t\t\t'\/(shea|lea|loa|thie)f$\/i' =&gt; '\\1ves',\r\n\t\t\t'\/([ti])um$\/i' =&gt; '\\1a',\r\n\t\t\t'\/(tomat|potat|ech|her|vet)o$\/i' =&gt; '\\1oes',\r\n\t\t\t'\/(bu)s$\/i' =&gt; '\\1ses',\r\n\t\t\t'\/(ax|test)is$\/i' =&gt; '\\1es',\r\n\t\t\t'\/s$\/' =&gt; 's',\r\n\t\t);\r\n\t\tforeach($rules as $rule=&gt;$replacement)\r\n\t\t{\r\n\t\t\tif(preg_match($rule,$name))\r\n\t\t\t\treturn preg_replace($rule,$replacement,$name);\r\n\t\t}\r\n\t\treturn $name.'s';\r\n\t}\r\n<\/pre>\n<p style=\"text-align: justify;\">Another way of dealing with this is to remove the generated names based on the table names completely. You can edit the files like:<\/p>\n<p style=\"text-align: justify;\">framework\/gii\/generators\/crud\/templates\/default\/admin.php<br \/>\nframework\/gii\/generators\/crud\/templates\/default\/create.php<br \/>\nframework\/gii\/generators\/crud\/templates\/default\/update.php<br \/>\netc.<\/p>\n<p style=\"text-align: justify;\">prior to making use of the CRUD generator, and change the H1 headers and the labels to something more generic (i.e. simply <em>&#8220;Edit&#8221;<\/em>, <em>&#8220;View&#8221;<\/em>, <em>&#8220;Update row #1&#8221;<\/em>).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Yii framework has a really nice CRUD generator that can speed up building back-end applications, which tend to be rather similar and therefore repetitive. If you prepare reasonable taxonomy of the underlying database tables, it also manages the page headers &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.hutsky.cz\/blog\/2015\/02\/yii-crud-generator-and-plural-forms\/\"> <span class=\"screen-reader-text\">Yii CRUD generator and plural forms<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,22],"tags":[6,4,46],"class_list":["post-1214","post","type-post","status-publish","format-standard","hentry","category-programming","category-web-related","tag-framework","tag-php","tag-yii"],"_links":{"self":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/comments?post=1214"}],"version-history":[{"count":8,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1214\/revisions"}],"predecessor-version":[{"id":1223,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/posts\/1214\/revisions\/1223"}],"wp:attachment":[{"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/media?parent=1214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/categories?post=1214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hutsky.cz\/blog\/wp-json\/wp\/v2\/tags?post=1214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}