Define a singular rule in the bootstrap for Inflector

两盒软妹~` 提交于 2020-01-02 05:11:08

问题


I'm using CakePHP 2.1 and need to define an Inflector rule for the word "Software", because CakePHP is converting all references to the plural form "Softwares" which isn't correct. Cake is looking for SoftwaresController and a table named Softwares.

I do know to create the rule in the boot strap, and read this doc reference.

http://book.cakephp.org/2.0/en/development/configuration.html#inflection-configuration

I also took a look at the lib/Cake/Inflector.php file, but can't figure out the syntax for defining a rule. It looks kind of like regex. Here are a few rule examples.

        '/(s)tatus$/i' => '\1\2tatuses',
        '/(quiz)$/i' => '\1zes',
        '/^(ox)$/i' => '\1\2en',
        '/([m|l])ouse$/i' => '\1ice',
        '/(matr|vert|ind)(ix|ex)$/i' => '\1ices',
        '/(x|ch|ss|sh)$/i' => '\1es',

What would be the correct code to define a Software singular Inflector rule?

EDIT:

 Inflector::rules('singular', array('rules'=>array('/software/'=>'software'),'irregular'=>array('software'=>'software'),'uninflected'=>array('software')));

I tried adding this rule, which works for the SoftwareController but Cake is complaining that it can't find the Softwares table, which is actually named "Software". I feel I'm close, but still missing something about how this works.


回答1:


you just have to know where to look (or search) in the book: http://book.cakephp.org/2.0/en/development/configuration.html#inflection-configuration

in your case

Inflector::rules('singular', array(
    'uninflected' => array('software')
));
Inflector::rules('plural', array(
    'uninflected' => array('software')
));


来源:https://stackoverflow.com/questions/9959152/define-a-singular-rule-in-the-bootstrap-for-inflector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!